Search code examples
qtpyqtpyqt4qt4

QComboBox inside QTreeWidgetItem


Is there something similar to the (PyQT) QTreeWidgetItem.setCheckState(0, Qt.Checked) but for the combo box?

I can't see anything in the reference, so how can I insert a custom QComboBox as one of the elements within QTreeWidgetItem?


Solution

  • Use QTreeWidget::setItemWidget ( QTreeWidgetItem * item, int column, QWidget * widget ) to put the combo box into the cells.

    For example, let's make all rows of the second column of a 2-column QTreeWidget to all be combo boxes:

    QTreeWidgetItemIterator it(ui->treeWidget);
    while (*it) {
        QComboBox *comboBox = new QComboBox(this);
        comboBox->addItems(QStringList() << "item1" << "item2");
        ui->treeWidget->setItemWidget(*it, 1, comboBox);
        ++it;
    }
    

    Our example widget now looks like this:

    enter image description here