QTreeWidget中添加QTreeWidgetItem

记录一下QTreeWidget和QTreeWidgetItem在Qt中的应用,示例如下图所示

QTreeWidget中添加QTreeWidgetItem_第1张图片

首先需要建立QTreeWidget对象

QHBoxLayout	*hLayout = new QHBoxLayout();
QTreeWidget *CameraTree = new QTreeWidget(this);
CameraTree->setColumnCount(2);//设置列
CameraTree->setHeaderLabels(QStringList()<<"feature"<<"value");//设置标题
hLayout->addWidget(CameraTree);

然后使用    QList 类建立一个对象items,再使用QTreeWidgetItem建立first、second等等,想建立几个就建立几个,然后使用items.append(first)关联起来,最后使用 CameraTree->insertTopLevelItems(0, items)插入为顶端LevelItems

QList items;
//items.append(new QTreeWidgetItem((QTreeWidget*)0, QStringList(QString("first"))));

first = new QTreeWidgetItem;
first->setText(0,"first");
items.append(first);

second = new QTreeWidgetItem;   //添加第二个父节点
second->setText(0, tr("second"));
items.append(second);
CameraTree->insertTopLevelItems(0, items);
first->setHidden(false);//设置隐藏
second->setHidden(false);//设置隐藏

再继续就可以给每一个添加进去的QTreeWidgetItem再添加控件或者child,使用first->addChild(item_ImageFormatControl)中addchild是父对象添加子对象函数,可以一直下去,不断添加...,CameraTree->setItemWidget(itemWidth, 1, spBoxckBoxWidget)中setItemWidget是在CameraTree添加的itemWidth对象中的第一列添加控控件(可以添加多个控件,效果图所示,代码如下)

QCheckBox *ckBox = new QCheckBox;
ckBox->setText("CheckBox");
ckBox->setChecked(true);
//ckBox->setFixedHeight(20);
QSpinBox *spBox = new QSpinBox;
//spBox->setFixedHeight(20);
QHBoxLayout *spBoxckBoxHBoxLayout = new QHBoxLayout();
spBoxckBoxHBoxLayout->addWidget(ckBox);
spBoxckBoxHBoxLayout->addWidget(spBox);

QWidget *spBoxckBoxWidget = new QWidget();
spBoxckBoxWidget->setLayout(spBoxckBoxHBoxLayout);

QTreeWidgetItem *item_ImageFormatControl = new QTreeWidgetItem;
item_ImageFormatControl->setText(0, "Image Format Control");
QTreeWidgetItem *item_AnalogControl = new QTreeWidgetItem;
item_AnalogControl->setText(0,"Analog Control");
QTreeWidgetItem *item_ImageQualityControl = new QTreeWidgetItem;
item_ImageQualityControl->setText(0, "Image Quality Control");
first->addChild(item_ImageFormatControl);
first->addChild(item_AnalogControl);
first->addChild(item_ImageQualityControl);

QTreeWidgetItem *itemWidth = new QTreeWidgetItem;
itemWidth->setText(0, "Width");
	
item_ImageFormatControl->addChild(itemWidth);
//CameraTree->setItemWidget(item, 1, spBox);
CameraTree->setItemWidget(itemWidth, 1, spBoxckBoxWidget);                //设置ckBox为item的第二列
	
QTreeWidgetItem *itemPixelFormat = new QTreeWidgetItem;
itemPixelFormat->setText(0, "Pixel Format");
item_ImageFormatControl->addChild(itemPixelFormat);

QComboBox *comboBoxPixelFormat = new QComboBox;
comboBoxPixelFormat->addItem("Mono 8");
comboBoxPixelFormat->addItem("RGB 8");
CameraTree->setItemWidget(itemPixelFormat, 1, comboBoxPixelFormat);

QSpinBox *spinHeight = new QSpinBox();
spinHeight->setFixedWidth(100);
QSlider *sliderHeight = new QSlider(Qt::Horizontal);
QHBoxLayout *hboxlayoutHeight = new QHBoxLayout();
hboxlayoutHeight->addWidget(spinHeight,8);
hboxlayoutHeight->addWidget(sliderHeight);

QWidget *widgetHeight = new QWidget();
widgetHeight->setLayout(hboxlayoutHeight);

QTreeWidgetItem *itemHeight = new QTreeWidgetItem;
itemHeight->setText(0, "Height");
item_ImageFormatControl->addChild(itemHeight);
//CameraTree->setItemWidget(item, 1, spBox);
CameraTree->setItemWidget(itemHeight, 1, widgetHeight);                //设置ckBox为item的第二列
QTreeWidgetItem *itemGainAuto = new QTreeWidgetItem;
itemGainAuto->setText(0,"Gain Auto");
item_AnalogControl->addChild(itemGainAuto);
//CameraTree->setItemWidget(item_AnalogControl,);

QTreeWidgetItem *itemBalanceWhiteAuto = new QTreeWidgetItem;
itemBalanceWhiteAuto->setText(0, "Balance White Auto");
itemBalanceWhiteAuto->setToolTip(0, "Balance White Auto");
item_ImageQualityControl->addChild(itemBalanceWhiteAuto);

QTreeWidgetItem *itemGamma= new QTreeWidgetItem;
itemGamma->setText(0, "Gamma");
item_ImageQualityControl->addChild(itemGamma);

QSpinBox *spinGamma = new QSpinBox();
spinGamma->setFixedWidth(100);
QSlider *sliderGamma = new QSlider(Qt::Horizontal);
QHBoxLayout *hboxlayoutGamma = new QHBoxLayout();
hboxlayoutGamma->addWidget(spinGamma, 8);
hboxlayoutGamma->addWidget(sliderGamma);

QWidget *widgetGamma = new QWidget();
widgetGamma->setLayout(hboxlayoutGamma);
CameraTree->setItemWidget(itemGamma, 1, widgetGamma);                //设置ckBox为item的第二列

QWidget *widget = new QWidget();
widget->setLayout(hLayout);
dock_CameraSet->setWidget(widget);

QTreeWidgetItem *itemOutputAlignment = new QTreeWidgetItem;
itemOutputAlignment->setText(0,"Output Alignment");
second->addChild(itemOutputAlignment);

 实现效果如下图:

QTreeWidget中添加QTreeWidgetItem_第2张图片

 

QTreeWidget还有一些常见信号可用,可以用于一些软件中的点击QTreeWidget对象中添加的QTreeWidgeItem对象调用槽函数,常见信号如下

void itemPressed(QTreeWidgetItem *item, int column);
void itemClicked(QTreeWidgetItem *item, int column);
void itemDoubleClicked(QTreeWidgetItem *item, int column);
void itemActivated(QTreeWidgetItem *item, int column);

connect(CameraTree, SIGNAL(itemDoubleClicked(QTreeWidgetItem *, int)), this, SLOT(test(QTreeWidgetItem *, int)));//点击CameraTree中的QTreeWidgetItem对象即可调用槽函数

 

 

你可能感兴趣的:(Qt)