简体   繁体   English

使用QTreeWidgetItems setData存储QStackedWidget或QVariant

[英]Using QTreeWidgetItems setData to store a QStackedWidget or QVariant

I am trying to make a QTreeWidget such that each row contains a series of comboboxes. 我试图做一个QTreeWidget,使每一行包含一系列的组合框。 Depending on how the user interacts with the comboboxes I would like certain comboboxes to becomes line edits and some to become buttons. 根据用户与组合框的交互方式,我希望某些组合框成为行编辑,而某些组合框成为按钮。

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. 这里建议一个QStackedWidget可以满足我的需求,并且做得很好,只是现在我需要一种方法来更改QStackedWidget,该方法位于包含组合框的旁边,该组合框向我发送了indexChanged信号。 (Basically I want to change the neighboring QStackWidgets index) (基本上我想更改相邻的QStackWidgets索引)

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. 我以为我可以使用setData将QStackWidget简单地存储在childItem中,然后在indexChanged插槽内检索它,但是由于某种原因,它似乎没有将QStackWidget设置为childItems数据。

Any help is appreciated. 任何帮助表示赞赏。

This is where I orginally setup my QTreeWidget and its Items 这是我最初设置QTreeWidget及其项目的地方

    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);
    }

And this is inside the slot where I am trying to retrieve the data (The QStackWidget) 这是我试图检索数据的插槽内(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);

EDIT: 编辑:

I've tried to set the data like this 我试图像这样设置数据

            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);

And retrieve it like this 然后像这样检索

                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. 因此,我仍然无法获得所需的功能。

Check if it works. 检查它是否有效。

As stacked widget QStackedWidget derived from QFrame , Create a Frame object and add your stacked widget to it. 作为从QFrame派生的堆叠小部件QStackedWidget ,创建一个Frame对象并将堆叠小部件添加到其中。 Set the frame to your child item. 将框架设置为您的子项。

QFrame *stackFrame = new QFrame(Parent);

QStackedWidget *masterItemWidget  = new QStackedWidget(stackFrame);

While querying first query for the frame and get the stacked widget child from it. 在查询框架的第一个查询时,从中获取堆叠的小部件子代。

QStackedWidget *stackedWidget = FrameObject->findChild<QStackedWidget *>();

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);

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. 问题在于,由于parentItem有一个以上的孩子(我想在这种情况下,最少是两个孩子),因此我没有引用正确的孩子。 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). 通过创建一个自定义的组合框属性可以解决此问题,该属性可以容纳孩子而不是父母(父母可以比签证更准确地从父母派生)。

Now where I populate the QTreeWidget with combobox items looks like this. 现在,我用组合框项目填充QTreeWidget的地方看起来像这样。

    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);
        }

    }

}

And the code in the slot where I get the data from the childItem (which holds the combobox) looks like this. 我从childItem(用于保存组合框)中获取数据的插槽中的代码如下所示。

                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);

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. 另外,如果有人知道如何在QStandardItem :: data()中获取数据项的数量,那将是很好的,尽管这不是非常关键的。

Cheers! 干杯!

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

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