一、基本要点
1、QTreeWidgetItem 是 Qt 开发框架中的一个重要组件,它主要用于表示树形视图中的节点。在 QTreeWidget 这样的控件中,每个节点都是 QTreeWidgetItem 的实例,可以包含文本、图标以及其他数据。
2、connect 是Qt中的一个重要函数,用于在Python版本的Qt应用中建立连接(Connection),使得当某个信号(Signal)发出时,会自动调用相应的槽(Slot)。这是事件驱动编程的核心,允许组件之间的通信,比如按钮点击触发回调函数处理。
QObject.connect(sender, signal, receiver, slot)
的基本语法如下:
sender
: 发出信号的对象,也称为源对象。signal
: 需要监听的信号,通常是对象方法返回前后的状态改变,如按钮的clicked()信号。receiver
: 接受信号的对象,也称为目标对象。slot
: 指定在接收信号时要执行的槽函数,通常是目标对象的方法。- 3、在Qt 的设计器中拖曳...出一个QTreeWidget.
-
运行效果:MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); init(); connect(ui->treeWidget,SIGNAL(itemChanged(QTreeWidgetItem*,int)),this,SLOT(treeItemChanged(QTreeWidgetItem *,int))); } MainWindow::~MainWindow() { delete ui; } void MainWindow::init() { ui->treeWidget->clear(); //清空widgettree QTreeWidgetItem *group1 = new QTreeWidgetItem(ui->treeWidget); group1->setFlags(Qt::ItemIsUserCheckable|Qt::ItemIsEnabled|Qt::ItemIsSelectable); group1->setCheckState(0,Qt::Unchecked); group1->setText(0,"group1"); QTreeWidgetItem *subItem11 = new QTreeWidgetItem(group1); subItem11->setFlags(Qt::ItemIsUserCheckable|Qt::ItemIsEnabled|Qt::ItemIsSelectable); subItem11->setCheckState(0,Qt::Unchecked); subItem11->setText(0,"老普"); QFont font; font.setPointSize(32);//通过设置字体大小来自动设置高度 subItem11->setFont(0,font); QTreeWidgetItem *subItem12 = new QTreeWidgetItem(group1); subItem12->setFlags(Qt::ItemIsUserCheckable|Qt::ItemIsEnabled|Qt::ItemIsSelectable); subItem12->setCheckState(0,Qt::Unchecked); subItem12->setText(0,"大毛"); QTreeWidgetItem *group2 = new QTreeWidgetItem(ui->treeWidget); group2->setFlags(Qt::ItemIsUserCheckable|Qt::ItemIsEnabled|Qt::ItemIsSelectable); group2->setCheckState(0,Qt::Unchecked); group2->setText(0,"group2"); QTreeWidgetItem *subItem21 = new QTreeWidgetItem(group2); subItem21->setFlags(Qt::ItemIsUserCheckable|Qt::ItemIsEnabled|Qt::ItemIsSelectable); subItem21->setCheckState(0,Qt::Unchecked); subItem21->setText(0,"司机"); QTreeWidgetItem *subItem22 = new QTreeWidgetItem(group2); subItem22->setFlags(Qt::ItemIsUserCheckable|Qt::ItemIsEnabled|Qt::ItemIsSelectable); subItem22->setCheckState(0,Qt::Unchecked); subItem22->setText(0,"二毛"); QTreeWidgetItem *subItem23 = new QTreeWidgetItem(group2); subItem23->setFlags(Qt::ItemIsUserCheckable|Qt::ItemIsEnabled|Qt::ItemIsSelectable); subItem23->setCheckState(0,Qt::Unchecked); subItem23->setText(0,"炮灰"); } void MainWindow::updateParentItem(QTreeWidgetItem *item) { //子节点选择情况来更新父节点的选框情况 QTreeWidgetItem *parent = item->parent(); if(parent==NULL) { return ; } int selectedCount = 0; int childCount = parent->childCount(); for(int i=0;i<childCount;i++) { QTreeWidgetItem *childItem = parent->child(i); if(childItem->checkState(0)==Qt::Checked) selectedCount++; } if(selectedCount<=0) { parent->setCheckState(0,Qt::Unchecked); } else if(selectedCount>0 && selectedCount<childCount) parent->setCheckState(0,Qt::PartiallyChecked); else parent->setCheckState(0,Qt::Checked); } void MainWindow::treeItemChanged(QTreeWidgetItem *item,int column) { qDebug()<<" item change column:"<<column<<endl; QString itemText = item->text(0); if(Qt::Checked == item->checkState(0)) { QTreeWidgetItem *parent = item->parent(); int count = item->childCount(); qDebug()<<"child count:"<<count<<endl; if(count>0) { for(int i=0; i< count;i++) item->child(i)->setCheckState(0,Qt::Checked); } else updateParentItem(item); } else if(Qt::Unchecked == item->checkState(0)) { QTreeWidgetItem *parent = item->parent(); int count = item->childCount(); qDebug()<<"child count:"<<count<<endl; if(count>0) { for(int i=0; i< count;i++) item->child(i)->setCheckState(0,Qt::Unchecked); } else { updateParentItem(item); } } }