C++ (Qt)#include "widget.h"#include "ui_widget.h" #include <QtGui/QHeaderView> Widget::Widget(QWidget *parent) : QDialog(parent), ui(new Ui::Widget){ ui->setupUi(this); ui->treeWidget->header()->setResizeMode(QHeaderView::ResizeToContents); connect(ui->checkBox, SIGNAL(toggled(bool)), SLOT(fill()));} Widget::~Widget(){ delete ui;} void Widget::fill(){ ui->treeWidget->clear(); new QTreeWidgetItem(ui->treeWidget, QStringList() << "1a" << "2a" << "3a"); new QTreeWidgetItem(ui->treeWidget, QStringList() << "1b" << "2b" << "3b"); new QTreeWidgetItem(ui->treeWidget, QStringList() << "1c" << "2c" << "3c"); if(ui->checkBox->isChecked()) { new QTreeWidgetItem(ui->treeWidget, QStringList() << "1e" << "2e" << "3e"); new QTreeWidgetItem(ui->treeWidget, QStringList() << "1f" << "2f" << "3f"); new QTreeWidgetItem(ui->treeWidget, QStringList() << "1g" << "2g" << "3g"); } // ui->treeWidget->resizeColumnToContents(0);}
C++ (Qt)connect(ui->checkBox, SIGNAL(clicked(bool)), SLOT(fill()));
C++ (Qt) QList<QTreeWidgetItem *> list; for (int i = 0; i < ui->treeWidget->topLevelItemCount(); ++i) list << ui->treeWidget->topLevelItem(i); qDeleteAll(list);