QT Clickable widget (maybe a button) inside a QTreeWidget?

I have a table which is basically a QTreeWidget and I want to put an interactive widget in it, maybe a button inside it. Each line is a QTreeWidgetItem, but I can't see how to add a button with QTreeWidgetItem :: setData p>

+3


source to share


1 answer


Following is a modification of the example provided in the Qt documentation to QTreeWidget

add a QPushButton to the second element.

 ui->treeWidget->setColumnCount(1);
 QList<QTreeWidgetItem *> items;
 for (int i = 0; i < 10; ++i)
    items.append(new QTreeWidgetItem((QTreeWidget*)0, QStringList(QString("item: %1").arg(i))));
 ui->treeWidget->insertTopLevelItems(0, items);

 ui->treeWidget->setItemWidget(items.value(1),0,new QPushButton("Click Me")); // Solution for your problem 

      

For two buttons next to each other inside an element you can use this approach



QWidget *dualPushButtons = new QWidget();
QHBoxLayout *hLayout = new QHBoxLayout();
hLayout->addWidget(new QPushButton("Button1"));
hLayout->addWidget(new QPushButton("Button2"));
dualPushButtons->setLayout(hLayout);

ui->treeWidget->setItemWidget(items.value(1),0,dualPushButtons);

      

You can adapt this by adding properties to buttons, etc.

+5


source







All Articles