其他分享
首页 > 其他分享> > QTreeWidget

QTreeWidget

作者:互联网

对于QTreeWidget的简单理解,待补充...

目录

1、创建

2、添加节点

3、其他属性设置

4、添加右键菜单

5、修饰


1、创建

ui->treeWidget->setColumnCount(1);    //设置列数
ui->treeWidget->setHeaderLabel(tr("item tree"));    //默认列名 1,2...

2、添加节点

//创建节点
QTreeWidgetItem* item1 = new QTreeWidgetItem(ui->treeWidget,QStringList(QString("A")));
item1->setCheckState(0,Qt::Unchecked);  //为节点添加checkbox

QTreeWidgetItem* item1 = new QTreeWidgetItem;
item2->setText(0,tr("B"));
item2->setCheckState(0,Qt::Unchecked);   //为节点添加checkbox


//创建子节点
QTreeWidgetItem *pItem1 = new QTreeWidgetItem(item,QStringList(QString("AA")));
QTreeWidgetItem *pItem2 = new QTreeWidgetItem(item1,QStringList(QString("BB")));
item1->addChild(pItem1);
item2->addChild(pItem2);

//将节点插入,树中
ui->treeWidget->addTopLevelItem(item1);         //顺序加入
ui->treeWidget->insertTopLevelItem(0,item2);    //插入

3、其他属性设置

//其他属性设置
ui->treeWidget->expandAll();        //展开所有节点
ui->treeWidget->collapseAll();      //关闭所有节点
ui->treeWidget->selectedItems();    //获取选中的节点
ui->treeWidget->currentItem();      //返回当前节点
//ui->treeWidget->hideColumn(0);      //隐藏某一列

4、添加右键菜单

//添加右键菜单
m_pMenu = new QMenu(ui->treeWidget);
m_pMenu->addAction(QString::fromLocal8Bit(("One")),this,SLOT(on_Menu_clicked1()));
m_pMenu->addAction(QString::fromLocal8Bit("Two"),this,SLOT(on_Menu_clicked2()));
//右键点击信号  一般控件都可以设置
ui->treeWidget->setContextMenuPolicy(Qt::CustomContextMenu);
connect(ui->treeWidget,SIGNAL(customContextMenuRequested(const QPoint&)),this,SLOT(onTreeMenuPopup(const QPoint&)));

5、修饰

//设置背景色
ui->treeWidget->setStyleSheet("QHeaderView::section{background:rgb(85, 181, 210);}");
//设置表头前景色
QBrush brush = QBrush(QColor(135, 240, 0));
brush.setStyle(Qt::BrushStyle::SolidPattern);
ui->treeWidget->headerItem()->setForeground(0, brush);

 

标签:QTreeWidget,item1,QTreeWidgetItem,ui,QString,treeWidget,节点
来源: https://blog.csdn.net/Liu_Xiao_Ming/article/details/115799745