0
votes

I have subclassed QTreeWidget, QTreeWidgetItem and QListWidgetItem. I am trying to add my custom QListWidgetItem to QListWidget but its not getting displayed in the view.

I Have created a dialog. I have inserted a QTreeWidget and QListWidget. What I want is when I click a Item in QTreeWidget, The QListWidget should get populated with the my custom QListWidgetItems i.e. CustomListWidgetItems contained in the clicked CustomTreeWidgetItem (QTreeWidgetItem derived class). If I use QListWidgetItem instead of CustomListWidgetItems, every thing works fine.

Header file contents:

class CustomTreeWidgetItem : public QTreeWidgetItem
{
public:
    explicit CustomTreeWidgetItem(int type = Type);
public:
    QList<CustomListWidgetItem*> projects;//Items to populate ListWidget
};

class CustomListWidgetItem : public QListWidgetItem
{
public:
    explicit CustomListWidgetItem(const QIcon & icon, const QString & text, QListWidget * parent = 0, int type = Type);
    CustomListWidgetItem(const int& id,QString Name,QString Description,QString icon);

public:
    int Id;
    QString Name;
    QString Description;
    QString Icon;
};

Source contents of Custom Listwidgetitem:

CustomListWidgetItem::CustomListWidgetItem(const QIcon &icon, const QString &text, QListWidget *parent, int type) : QListWidgetItem(icon,text,parent,type)
{

}

CustomListWidgetItem::CustomListWidgetItem(const int& id,QString Name,QString Description,QString icon)
{
    this->Id = id;
    this->Name = Name;
    this->Description = Description;
    this->Icon = icon;

    QString iconPath = QCoreApplication::applicationDirPath() + "/" + icon + ".png";
    QIcon qIcon(iconPath);
    QListWidgetItem::QListWidgetItem(qIcon,Name);
}

Source contents:

void CustomTreeWidget::treeitemSelected(QTreeWidgetItem * item, int)//Tree item click event
{
    CustomTreeWidgetItem *project = static_cast<CustomTreeWidgetItem*>(item);

        if(LISTWIDGET)
        {
            m_listWidget->clear();
            m_listWidget->setSelectionMode(QAbstractItemView::SingleSelection);

            int count = project->projects.count();

            if(count == 0)
            {
                m_listWidget->addItem("No Projects Here");//This gets displayed
                m_listWidget->setSelectionMode(QAbstractItemView::NoSelection);
            }
            else
            {
                for (int i = 0; i < count; ++i)
                {
                    m_listWidget->addItem(project->projects[i]);//This does not get displayed
                }
            }
        }
    }

During Debugging I can see that the data i.e Name, Description, etc. is present in "project->projects[i]". No error is shown during addItem function call still the items are not visible in view.

1

1 Answers

0
votes

Few questions: - Why do I have the feeling you mix listwidgetitem and treewidgetitem? - Why do you fill your view everytime an item is selected? is it initialized? - Can you show where you create the view and start adding items?

Check you have the following:

  1. create a qtreewidget that you add to your view layout
  2. add a root node using something like

    QTreeWidgetItem *treeItemRoot = new QTreeWidgetItem(myTreeWidget);
    treeItemRoot->setText(0, projectName);
    
  3. add children nodes

    QTreeWidgetItem *treeItemChild = new QTreeWidgetItem();
    treeItemRoot->addChild(treeItem);
    

and post the results.