简体   繁体   English

Qt将派生的QListwidgetitem添加到QListWidget

[英]Qt adding a derived QListwidgetitem to QListWidget

I have subclassed QTreeWidget, QTreeWidgetItem and QListWidgetItem. 我已经将QTreeWidget,QTreeWidgetItem和QListWidgetItem分为子类。 I am trying to add my custom QListWidgetItem to QListWidget but its not getting displayed in the view. 我试图将我的自定义QListWidgetItem添加到QListWidget,但是它没有显示在视图中。

I Have created a dialog. 我已经创建了一个对话框。 I have inserted a QTreeWidget and QListWidget. 我插入了一个QTreeWidget和QListWidget。 What I want is when I click a Item in QTreeWidget, The QListWidget should get populated with the my custom QListWidgetItems ie CustomListWidgetItems contained in the clicked CustomTreeWidgetItem (QTreeWidgetItem derived class). 我想要的是当我单击QTreeWidget中的项目时,QListWidget应该填充有我自定义的QListWidgetItems,即单击的CustomTreeWidgetItem(QTreeWidgetItem派生类)中包含的CustomListWidgetItems。 If I use QListWidgetItem instead of CustomListWidgetItems, every thing works fine. 如果我使用QListWidgetItem而不是CustomListWidgetItems,则一切正常。

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: 自定义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 ie Name, Description, etc. is present in "project->projects[i]". 在调试过程中,我可以看到“项目->项目[i]”中存在名称,描述等数据。 No error is shown during addItem function call still the items are not visible in view. 在addItem函数调用期间未显示任何错误,但该项目在视图中仍然不可见。

Few questions: - Why do I have the feeling you mix listwidgetitem and treewidgetitem? 几个问题:-为什么我觉得您混合使用listwidgetitem和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 创建一个添加到视图布局的qtreewidget
  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. 并发布结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM