QT开发中文网

I want to add a comboBox as topLevelItem

Unfortunately, it is not possible to add a QComboBox as a topLevelItem in a QTreeWidget. The reason for this is that topLevelItems are meant to be headers or root items in the tree widget and do not have the capability to display widgets like child items.

However, you can achieve similar functionality by using a separate QWidget (e.g., a QGroupBox) as the parent widget for your QTreeWidget and adding the QComboBox as one of its child widgets. Here’s an example:

// create parent widget
QGroupBox* groupBox = new QGroupBox(this);
groupBox->setTitle("Top Level Item");
setCentralWidget(groupBox);

// create combo box
QComboBox* comboBox = new QComboBox(this);
comboBox->addItem("Option 1");
comboBox->addItem("Option 2");

// create tree widget
QTreeWidget* treeWidget = new QTreeWidget(groupBox);
treeWidget->setHeaderLabels({"Column 1", "Column 2"});

// add combo box and tree widget to parent widget
QVBoxLayout* layout = new QVBoxLayout(groupBox);
layout->addWidget(comboBox);
layout->addWidget(treeWidget);

// add child items to tree widget
QTreeWidgetItem* item1 = new QTreeWidgetItem(treeWidget);
item1->setText(0, "Item 1");
item1->setText(1, "Value 1");

QTreeWidgetItem* item2 = new QTreeWidgetItem(treeWidget);
item2->setText(0, "Item 2");
item2->setText(1, "Value 2");

In this example, we first create a parent QWidget (in this case, a QGroupBox) and set it as the central widget of our main window. We then create a QComboBox instance and add some options to it.

Next, we create the tree widget and set its header labels. We also create a QVBoxLayout instance for our parent widget and add the QComboBox and tree widget to it.

Finally, we add some child items to our tree widget. When you run your application, you should see a QGroupBox with a QComboBox and a QTreeWidget embedded within it.

本站部分文章来源于网络,版权归原作者所有,如有侵权请联系站长删除。
转载请注明出处:https://qt.0voice.com/?id=2886

分享:
扫描分享到社交APP
上一篇
下一篇
发表列表
游客 游客
此处应有掌声~
评论列表

还没有评论,快来说点什么吧~

联系我们

在线咨询: 点击这里给我发消息

微信号:3007537140

上班时间: 10:30-22:30

关注我们
x

注册

已经有帐号?