使用QTreeWidgetItems setData存储QStackedWidget或QVariant [英] Using QTreeWidgetItems setData to store a QStackedWidget or QVariant

查看:976
本文介绍了使用QTreeWidgetItems setData存储QStackedWidget或QVariant的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个QTreeWidget,以使每一行包含一系列组合框.根据用户与组合框的交互方式,我希望某些组合框成为行编辑,而某些组合框成为按钮.

I am trying to make a QTreeWidget such that each row contains a series of comboboxes. Depending on how the user interacts with the comboboxes I would like certain comboboxes to becomes line edits and some to become buttons.

此处建议使用QStackedWidget将满足我的需求,并且做得很好,除了现在,我需要一种方法来更改QStackedWidget,该方法位于包含组合框的QStackedWidget旁边,该组合框向我发送了indexChanged信号. (基本上我想更改相邻的QStackWidgets索引)

It was suggested here that a QStackedWidget would serve my needs and its done a pretty good job except now I need a way to alter the QStackedWidget that is next to the one that contains the combobox sending me an indexChanged signal. (Basically I want to change the neighboring QStackWidgets index)

我认为我可以使用setData将QStackWidget简单地存储在childItem中,然后在indexChanged插槽内检索它,但是由于某些原因,它似乎未将QStackWidget设置为childItems数据.

I thought that I would be able to simply store the QStackWidget in the childItem using setData and then retrieve it inside the indexChanged slot but for some reason it appears the QStackWidget is not set to the childItems data.

感谢您的帮助.

这是我最初设置QTreeWidget及其项的地方

This is where I orginally setup my QTreeWidget and its Items

    QTreeWidgetItem *childItem = new QTreeWidgetItem(itemParent);



    QVariant itemParentVariant,widgetParentVarient;
    widgetParentVarient.setValue(widgetParent);
    itemParentVariant.setValue(itemParent);
    QList<QVariant> stackWidgetList;
    uint cycleSetup;
    for(cycleSetup = 0;cycleSetup < methodBlocks.at(rowType).size()+2;cycleSetup++)
    {

            QComboBox *itemComboBox = new QComboBox;
            itemComboBox->setProperty("rowType", rowType);
            itemComboBox->setProperty("row", 0);
            itemComboBox->setProperty("column",cycleSetup);
            itemComboBox->setProperty("widgetParent",widgetParentVarient);
            itemComboBox->setProperty("itemParent",itemParentVariant);
            itemComboBox->addItems(methodBlocks.at(0).at(cycleSetup));
            QObject::connect(itemComboBox, SIGNAL(currentIndexChanged(const QString&)), this, SLOT(OnComboIndexChanged(const QString&)));
            QLineEdit *itemLineEdit = new QLineEdit;
            QPushButton *itemButton = new QPushButton;
            itemButton->setText("Reset");
            QComboBox *timeComboBox = new QComboBox;
            timeComboBox->setProperty("rowType", rowType);
            timeComboBox->setProperty("row", 0);
            timeComboBox->setProperty("column",cycleSetup);
            timeComboBox->setProperty("widgetParent",widgetParentVarient);
            timeComboBox->setProperty("itemParent",itemParentVariant);
            timeComboBox->addItems(QString("Seconds;MilliSeconds;Reset").split(";"));
            QStackedWidget *masterItemWidget = new QStackedWidget;
            masterItemWidget->addWidget(itemLineEdit);
            masterItemWidget->addWidget(itemComboBox);
            masterItemWidget->addWidget(itemButton);
            masterItemWidget->addWidget(timeComboBox);
            masterItemWidget->setCurrentIndex(1);
            QVariant stackParent;
            stackParent.setValue(masterItemWidget);
            itemComboBox->setProperty("stackParent",stackParent);
            childItem->setData(0,Qt::UserRole,stackParent);
            stackWidgetList.push_back(stackParent);
            widgetParent->setItemWidget(childItem,cycleSetup,masterItemWidget);
            itemParent->addChild(childItem);
    }

这是我试图检索数据的插槽内(QStackWidget)

And this is inside the slot where I am trying to retrieve the data (The QStackWidget)

                QStackedWidget *itemMaster = combo->property("stackParent").value<QStackedWidget*>(); //this works
                itemMaster->setCurrentIndex(0);
                QTreeWidget *widgetParent = combo->property("widgetParent").value<QTreeWidget*>();

                QTreeWidgetItem *parentItem = combo->property("itemParent").value<QTreeWidgetItem*>();
                QTreeWidgetItem *childItem = new QTreeWidgetItem(parentItem);


                QList<QVariant> stackList = childItem->data(0,Qt::UserRole).value<QList<QVariant>>(); //this doesnt
                QStackedWidget *itemsibMaster = childItem->data(0,Qt::UserRole).value<QStackedWidget*>(); //neither does this
                itemsibMaster->setCurrentIndex(2);

我试图像这样设置数据

            QFrame *stackFrame = new QFrame;
            QStackedWidget *masterItemWidget = new QStackedWidget(stackFrame);
            masterItemWidget->addWidget(itemLineEdit);
            masterItemWidget->addWidget(itemComboBox);
            masterItemWidget->addWidget(itemButton);
            masterItemWidget->addWidget(timeComboBox);
            masterItemWidget->setCurrentIndex(1);
            QVariant stackParent;
            stackParent.setValue(masterItemWidget);
            itemComboBox->setProperty("stackParent",stackParent);
            QVariant frameVariant;
            frameVariant.setValue(stackFrame);
            childItem->setData(0,Qt::UserRole,frameVariant);
            stackWidgetList.push_back(stackParent);
            widgetParent->setItemWidget(childItem,cycleSetup,masterItemWidget);
            itemParent->addChild(childItem);

并像这样检索它

                QStackedWidget *itemMaster = combo->property("stackParent").value<QStackedWidget*>();
                itemMaster->setCurrentIndex(0);
                QTreeWidget *widgetParent = combo->property("widgetParent").value<QTreeWidget*>();

                QTreeWidgetItem *parentItem = combo->property("itemParent").value<QTreeWidgetItem*>();
                QTreeWidgetItem *childItem = new QTreeWidgetItem(parentItem);


                QFrame *frameObject = childItem->data(0,Qt::UserRole).value<QFrame*>();
                //QList<QVariant> stackList = childItem->data(0,Qt::UserRole).value<QList<QVariant>>();
                QStackedWidget *itemFrameMaster = frameObject->findChild<QStackedWidget*>();
                if(itemFrameMaster)
                {
                    qDebug() << "itemFrame Exists";

                }
                else
                {
                    qDebug() << "itemFrame is NULL";//It goes to here
                }

所以我仍然无法获得所需的功能.

So I'm still unable to get the desired functionality.

推荐答案

好的,因此我设法获得了我所追求的功能,因此,如果有人尝试这样做,这里就是解决方法.

Alright so I managed to get the functionality that I was after, so in case anyone else is trying to do this here's the fix.

基本上我之前所做的一切都是正确的,除了一个错误.

Essentially everything I had done before was correct except for one error.

当检索存储在组合框子项中的数据时,我最初是在这里得到那个子项的.

When retrieving the data stored in the comboboxes child I had originally gotten that child here.

            QTreeWidgetItem *parentItem = combo->property("itemParent").value<QTreeWidgetItem*>();
            QTreeWidgetItem *childItem = new QTreeWidgetItem(parentItem);

问题在于,由于parentItem有多个孩子(在这种情况下,我认为最小值是两个孩子),因此我没有引用正确的孩子.通过创建一个自定义的组合框属性可以解决此问题,该属性可以容纳孩子而不是父母(父母可以比签证更准确地从父母派生).

The issue was that since the parentItem had more than one child (I suppose in this case the minimum is two children) I was not referencing the correct child. This issue was solved by creating a custom combobox property which held the child rather than the parent (parent can be derived from the parent more accurately than visa versa).

现在我在其中用组合框项目填充QTreeWidget的地方看起来像这样.

Now where I populate the QTreeWidget with combobox items looks like this.

    QTreeWidgetItem *childItem = new QTreeWidgetItem(itemParent);

    QVariant childItemVariant,widgetParentVarient;
    widgetParentVarient.setValue(widgetParent);
    childItemVariant.setValue(childItem);
    uint cycleSetup;
    for(cycleSetup = 0;cycleSetup < methodBlocks.at(rowType).size();cycleSetup++)
    {
        if(cycleSetup < methodBlocks.at(rowType).size())
        {
            QComboBox *itemComboBox = new QComboBox;
            itemComboBox->setProperty("rowType", rowType);
            itemComboBox->setProperty("row", 0);
            itemComboBox->setProperty("column",cycleSetup);
            itemComboBox->setProperty("widgetParent",widgetParentVarient);
            itemComboBox->setProperty("childItem",childItemVariant);
            itemComboBox->addItems(methodBlocks.at(0).at(cycleSetup));
            QObject::connect(itemComboBox, SIGNAL(currentIndexChanged(const QString&)), this, SLOT(OnComboIndexChanged(const QString&)));
            QLineEdit *itemLineEdit = new QLineEdit;

            QFrame *stackFrame = new QFrame;
            QStackedWidget *masterItemWidget = new QStackedWidget(stackFrame);
            masterItemWidget->addWidget(itemLineEdit);
            masterItemWidget->addWidget(itemComboBox);
            //masterItemWidget->addWidget(timeComboBox);
            masterItemWidget->setCurrentIndex(1);
            QVariant stackParent;
            stackParent.setValue(masterItemWidget);
            itemComboBox->setProperty("stackParent",stackParent);
            itemComboBox->setProperty("cycleSetupIT",cycleSetup);
            QVariant frameVariant;
            frameVariant.setValue(stackFrame);
            childItem->setData(cycleSetup,Qt::UserRole,stackParent);
            widgetParent->setItemWidget(childItem,cycleSetup,masterItemWidget);
            itemParent->addChild(childItem);
        }

    }

}

我从childItem(保存组合框)中获取数据的插槽中的代码看起来像这样.

And the code in the slot where I get the data from the childItem (which holds the combobox) looks like this.

                QTreeWidget *widgetParent = combo->property("widgetParent").value<QTreeWidget*>();
                widgetParent->setColumnCount(6);
                QTreeWidgetItem *childItem = combo->property("childItem").value<QTreeWidgetItem*>();
                QTreeWidgetItem *parentItem = childItem->parent();

                int rowType = combo->property("rowType").toInt();
                int cycleIT = combo->property("cycleSetupIT").toInt();
                int offset = 0;

                QStackedWidget *itemFrameMaster = childItem->data(cycleIT+1,Qt::UserRole).value<QStackedWidget*>();
                itemFrameMaster->setCurrentIndex(0);

希望这会有所帮助.同样,如果有人知道如何在QStandardItem :: data()中获取数据项的数量,尽管不是很关键,那还是很好的.

Hope this helps. Also if anyone knows how to get the number of data items in a QStandardItem::data() that'd be great albeit not super critical.

干杯!

这篇关于使用QTreeWidgetItems setData存储QStackedWidget或QVariant的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆