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.