1
votes

I have setup my QTreeWidget such that each cell is filled with comboboxes, however I would like to create a text edit widget next to the selected combobox (or overwrite the existing combobox), depending on which combobox item the user has selected.

I figured I could do this by adding the comboboxes parent as a property when its initially setup and then upon interaction with the combobox simply use setItemWidget to put a text edit Item in column after the selected combobox (using the same child). But it seems as though the parent is not being passed correctly or some other issue is causing the text edit widget to appear in the row below where it should be.

I've attached a photo and some code for clarification.

This is where I setup the comboboxes for the QTreeWidget (Specifically where rowType == 0)

void customMethodConstructorWindow::addChildRow(QTreeWidget *widgetParent,QTreeWidgetItem *itemParent,int rowType)
{
//widgetParent->setColumnCount(methodBlocks.at(rowType).size());
if(rowType == 0)
{
    QTreeWidgetItem *childItem = new QTreeWidgetItem(itemParent);

    QVariant itemParentVariant;
    itemParentVariant.setValue(itemParent);
    for(uint cycleSetup = 0;cycleSetup < methodBlocks.at(rowType).size();cycleSetup++)
    {

        QComboBox *itemComboBox = new QComboBox;
        itemComboBox->setProperty("rowType", rowType);
        itemComboBox->setProperty("row", 0);
        itemComboBox->setProperty("column",cycleSetup);
        itemComboBox->setProperty("itemParent",itemParentVariant);
        itemComboBox->addItems(methodBlocks.at(0).at(cycleSetup));
        QObject::connect(itemComboBox, SIGNAL(currentIndexChanged(const QString&)), this, SLOT(OnComboIndexChanged(const QString&)));
        widgetParent->setItemWidget(childItem,cycleSetup,itemComboBox);
        itemParent->addChild(childItem);
    }

}
else
{
    QTreeWidgetItem *childItem = new QTreeWidgetItem(itemParent);

    QComboBox *item0ComboBox = new QComboBox;
    item0ComboBox->setProperty("rowType", rowType);
    item0ComboBox->setProperty("row", 1);
    item0ComboBox->setProperty("column", 0);
    item0ComboBox->addItems(methodBlocks.at(1).at(0));
    QObject::connect(item0ComboBox, SIGNAL(currentIndexChanged(const QString&)), this, SLOT(OnComboIndexChanged(const QString&)));
    widgetParent->setItemWidget(childItem,0,item0ComboBox);
    itemParent->addChild(childItem);

    QComboBox *item1ComboBox = new QComboBox;
    item1ComboBox->setProperty("rowType", rowType);
    item1ComboBox->setProperty("row", 1);
    item1ComboBox->setProperty("column", 1);
    item1ComboBox->addItems(methodBlocks.at(1).at(rowType));
    QObject::connect(item1ComboBox, SIGNAL(currentIndexChanged(const QString&)), this, SLOT(OnComboIndexChanged(const QString&)));
    widgetParent->setItemWidget(childItem,1,item1ComboBox);
    itemParent->addChild(childItem);
}
}

And this is where I try to create a text edit widget over one of the comboboxes

void customMethodConstructorWindow::OnComboIndexChanged(const QString& text)
{
QComboBox* combo = qobject_cast<QComboBox*>(sender());
if (combo)
{

    int nRow = combo->property("row").toInt();
    int nCol = combo->property("column").toInt();
    switch (nRow) {
    case 0:
    {
        switch (nCol)
        {
        case 0:
        {
            //combo->setVisible(false);
            //testPoint = combo->pos();


        }
            break;
        case 1:
        {
            if(combo->currentIndex() != 0)
            {
                QTreeWidgetItem *childItem = new QTreeWidgetItem(combo->property("itemParent").value<QTreeWidgetItem*>());
                QTextEdit *textItemEdit = new QTextEdit;
                //QObject::connect(item1ComboBox, SIGNAL(currentIndexChanged(const QString&)), this, SLOT(OnComboIndexChanged(const QString&)));
                ui->methodSetupTreeWidget->setItemWidget(childItem,2,textItemEdit);
            }

        }
            break;
        case 2:
                //customObjectAttributes.comparisonType.push_back(combo->currentIndex());
            break;
        case 3:
        {
                //customObjectAttributes.valueB.push_back(combo->currentIndex());
        }
            break;
        case 4:

            break;
        case 5:
            break;
        case 6:
            break;
        case 7:
            //ui->logicSetupTableWidget->setRowCount(ui->logicSetupTableWidget->rowCount()+1);
            if(combo->currentIndex() != 3)
            {
                //addComboRow(ui->logicSetupTableWidget->rowCount()-1);
            }
              //customObjectAttributes.outputType.push_back(combo->currentIndex());
            break;
        }
    }
        break;
    case 1:
    {
        switch (combo->property("column").toInt())
        {
        case 0:

            break;
        case 1:
        {
                addChildRow(ui->methodSetupTreeWidget,ui->methodSetupTreeWidget->itemAt(QPoint(2,1)),0);
                addChildRow(ui->methodSetupTreeWidget,ui->methodSetupTreeWidget->itemAt(QPoint(3,1)),1);

        }
            break;
        case 2:
        {

        }
            break;
        case 3:
        {

        }
            break;
        case 4:
            break;
        case 5:
            break;
        case 6:
            break;
        case 7:
        {

            if(combo->currentIndex() != 3)
            {
                //addComboRow(ui->logicSetupTableWidget->rowCount()-1);
            }

        }
            break;
        }
    }
        break;
    }


}

}

ScreenShot of current situation

My issue here is that the text edit shows up below the less than (<) combobox rather than ontop (or replacing it).

1
Can't you solve this by just making the combobox editable? Or if thats not desirable derive a QWidget class that handles the required transition from combobox to lineedit by ways of a stackwidget or so - without messing with the treewidgetItems.Johannes Munk
I'd looked at making the combobox editable but I wasnt crazy about that way of handling it. After taking a close look at the QStackedWidget docs it seems there may be a workable solution. I'll see how that goes and update this after.Reginald Marr
So an issue thats keeping me from implementing the QStackedWidget work around is I am unable to cast a widget (which is holding the layout of a QCombobox) into a QComboboxReginald Marr

1 Answers

0
votes

I was able to find a solution using QStandardItem:setData() and QStackedWidget to get the functionality I was after.

This was done in the process of trying to solve a related issue here.