开发者

QComboBox inside QTreeWidgetItem

开发者 https://www.devze.com 2022-12-09 22:59 出处:网络
Is there something similar to the (PyQT) QTreeWidgetIte开发者_JAVA技巧m.setCheckState(0, Qt.Checked) but for the combo box?

Is there something similar to the (PyQT) QTreeWidgetIte开发者_JAVA技巧m.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?


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:

QComboBox inside QTreeWidgetItem


I know this is an old question but I think I have a more thorough answer. To get any functionality out of the QComboBox, you'll probably need to subclass it. Here's the solution that I came up with:


#ifndef COMBOBOXITEM_H
#define COMBOBOXITEM_H

#include 

class ComboBoxItem : public QComboBox
{
    Q_OBJECT

private:
    QTreeWidgetItem *item;
    int column;

public:
    ComboBoxItem(QTreeWidgetItem*, int);

public slots:
    void changeItem(int);

};

ComboBoxItem::ComboBoxItem(QTreeWidgetItem *item, int column)
{
    this->item = item;
    this->column = column;
    connect(this, SIGNAL(currentIndexChanged(int)), SLOT(changeItem(int)));
}

void ComboBoxItem::changeItem(int index)
{
    if(index >=0)
    {
        item->setData(this->column, Qt::UserRole, this->itemText(index));
        qDebug() item->data(this->column, Qt::UserRole).toString();
    }
}

#include "moc_ComboBoxItem.cpp"

#endif // COMBOBOXITEM_H

////// Sample implementation..

lst = new QTreeWidget;
// Snip
QTreeWidgetItem *itm = new QTreeWidgetItem;
// Snip
ComboBoxItem *cmb = new ComboBoxItem(itm, 1);
cmb->addItem("One");
cmb->addItem("Two");
cmb->addItem("Three");
cmb->addItem("Four");
lst->setItemWidget(itm, 1, cmb);

I hope that helps someone in need of a QComboBox inside of a QTreeWidgetItem!


Use

setItemWidget(QTreeWidgetItem( ), column, QWidget( ) )

.Just add your QComboBox() as a parameter, as it inherits QWidget() so it is compatible.

tree = QTreeWidget()

cmb = QComboBox()
cmb.addItem("Item1", 'value1')
cmb.addItem("Item2", 'value2')
cmb.addItem("Item3", 'value3')

item = QTreeWidgetItem(tree.invisibleRootItem())
column = 0
item.setData(column, Qt.EditRole, 'NameYouWant')
column += 1
tree.setItemWidget(item, column , cmb)


Here is small fix to the another posters method. I found that is uses Data to update the box How ever I made small change to setText updater for the method.

#ifndef COMBOBOXITEM_H
#define COMBOBOXITEM_H

#include <QtGui>

class ComboBoxItem : public QComboBox
{
    Q_OBJECT

private:
    QTreeWidgetItem *item;
    int column;

public:
    ComboBoxItem(QTreeWidgetItem*, int);

public slots:
    void changeItem(int);

};

ComboBoxItem::ComboBoxItem(QTreeWidgetItem *item, int column)
{
    this->item = item;
    this->column = column;
    connect(this, SIGNAL(currentIndexChanged(int)), SLOT(changeItem(int)));
}

void ComboBoxItem::changeItem(int index)
{
    if(index >=0)
    {
        this->item->setText(this->column, this->currentText());

    }
}

#include "moc_ComboBoxItem.cpp"



#endif // COMBOBOXITEM_H

////// Sample implementation..

lst = new QTreeWidget;
// Snip
QTreeWidgetItem *itm = new QTreeWidgetItem;
// Snip
ComboBoxItem *cmb = new ComboBoxItem(itm, 1);
cmb->addItem("One");
cmb->addItem("Two");
cmb->addItem("Three");
cmb->addItem("Four");
lst->setItemWidget(itm, 1, cmb);


This is easiest method:

QComboBox *cb = new QComboBox(this);
QStringList cbTexts;
cbTexts << tr("First") << tr("Second") << tr("Third");
cb->addItems(cbTexts);

QTreeWidgetItem *item = new QTreeWidgetItem(ui->treeWidget);
ui->treeWidget->addTopLevelItem(item);
ui->treeWidget->setItemWidget(item, [colum here], cb);
for (int col = 0; col < [num colums]; ++col) ui->treeWidget->resizeColumnToContents(col);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号