之前使用treeview想要添加标准控件发现好像不能添加,查找资料发现可以使用treewidget实现:
实现效果:
主要是使用setItemWidget()
方法实现添加控件,主要代码:
//treeWidget_rvizui->treeWidget_rviz->setWindowTitle("Displays");ui->treeWidget_rviz->setWindowIcon(QIcon("://images/display.png"));//header 设置ui->treeWidget_rviz->setHeaderHidden(true);ui->treeWidget_rviz->setHeaderLabels(QStringList()<<"key"<<"value");//Global optionsQTreeWidgetItem *Global=new QTreeWidgetItem(QStringList()<<"Global Options");Global->setIcon(0,QIcon("://images/setting2.png"));QTreeWidgetItem* FixedFrame=new QTreeWidgetItem(QStringList()<<"Fixed Frame");Global->addChild(FixedFrame);ui->treeWidget_rviz->addTopLevelItem(Global);//添加combox控件QComboBox *frame=new QComboBox();frame->addItem("map");frame->setMaximumWidth(150);ui->treeWidget_rviz->setItemWidget(FixedFrame,1,frame);QTreeWidgetItem* bcolor=new QTreeWidgetItem(QStringList()<<"Background Color");Global->addChild(bcolor);//添加lineedit控件QLineEdit *colorval=new QLineEdit("255;255;255");colorval->setMaximumWidth(150);ui->treeWidget_rviz->setItemWidget(bcolor,1,colorval);QSpinBox *framerateval=new QSpinBox();framerateval->setMaximumWidth(150);framerateval->setRange(10,50);QTreeWidgetItem* framerate=new QTreeWidgetItem(QStringList()<<"Frame Rate");Global->addChild(framerate);ui->treeWidget_rviz->setItemWidget(framerate,1,framerateval);//gridQTreeWidgetItem *grid=new QTreeWidgetItem(QStringList()<<"Grid");grid->setIcon(0,QIcon("://images/grid.png"));ui->treeWidget_rviz->addTopLevelItem(grid);QCheckBox* gridcheck=new QCheckBox;ui->treeWidget_rviz->setItemWidget(grid,1,gridcheck);QTreeWidgetItem *gridstatus=new QTreeWidgetItem(QStringList()<<"Statue:");gridstatus->setIcon(0,QIcon("://images/ok.png"));grid->addChild(gridstatus);QLabel *gridstatusvalue=new QLabel("ok");gridstatusvalue->setMaximumWidth(150);ui->treeWidget_rviz->setItemWidget(gridstatus,1,gridstatusvalue);
完整项目代码:giithub