Qt C++ treeWidget设置checkbox,勾选的chexkbox作为tablewidget表头输出
时间: 2023-12-14 08:36:59 浏览: 531
首先,要在TreeWidget中添加CheckBox,需要在TreeWidget的setItemWidget() 函数中设置一个QCheckBox控件。你可以在TreeWidget的itemChanged()槽函数中实现勾选操作,并将勾选的数据保存在某个地方。
以下是一个实现的示例代码:
```cpp
void MainWindow::setUpTreeWidget()
{
// 添加列标题
QStringList headers;
headers << "Name" << "Type" << "Size" << "Date Modified";
ui->treeWidget->setHeaderLabels(headers);
// 添加根节点
QTreeWidgetItem *root = new QTreeWidgetItem(ui->treeWidget);
root->setText(0, "Root");
root->setFlags(Qt::ItemIsEnabled | Qt::ItemIsUserCheckable);
root->setCheckState(0, Qt::Unchecked);
// 添加子节点
QTreeWidgetItem *child1 = new QTreeWidgetItem(root);
child1->setText(0, "File1.txt");
child1->setText(1, "File");
child1->setText(2, "5 KB");
child1->setText(3, "2021-11-01");
child1->setFlags(Qt::ItemIsEnabled | Qt::ItemIsUserCheckable);
child1->setCheckState(0, Qt::Unchecked);
QTreeWidgetItem *child2 = new QTreeWidgetItem(root);
child2->setText(0, "Folder1");
child2->setText(1, "Folder");
child2->setText(2, "");
child2->setText(3, "2021-11-01");
child2->setFlags(Qt::ItemIsEnabled | Qt::ItemIsUserCheckable);
child2->setCheckState(0, Qt::Unchecked);
QTreeWidgetItem *subChild1 = new QTreeWidgetItem(child2);
subChild1->setText(0, "File2.txt");
subChild1->setText(1, "File");
subChild1->setText(2, "10 KB");
subChild1->setText(3, "2021-11-02");
subChild1->setFlags(Qt::ItemIsEnabled | Qt::ItemIsUserCheckable);
subChild1->setCheckState(0, Qt::Unchecked);
// 设置CheckBox
for(int i = 0; i < ui->treeWidget->topLevelItemCount(); i++){
QTreeWidgetItem *item = ui->treeWidget->topLevelItem(i);
QCheckBox *checkBox = new QCheckBox(ui->treeWidget);
ui->treeWidget->setItemWidget(item, 0, checkBox);
connect(checkBox, SIGNAL(stateChanged(int)), this, SLOT(onItemChecked()));
}
}
void MainWindow::onItemChecked()
{
// 获取所有勾选的数据
QList<QTreeWidgetItem*> checkedItems;
for(int i = 0; i < ui->treeWidget->topLevelItemCount(); i++){
QTreeWidgetItem *item = ui->treeWidget->topLevelItem(i);
QCheckBox *checkBox = qobject_cast<QCheckBox*>(ui->treeWidget->itemWidget(item, 0));
if(checkBox->isChecked()){
checkedItems.append(item);
}
}
// 将勾选的数据输出到TableWidget中
ui->tableWidget->clear();
ui->tableWidget->setColumnCount(4);
ui->tableWidget->setHorizontalHeaderLabels(QStringList() << "Name" << "Type" << "Size" << "Date Modified");
int rowCount = 0;
for(int i = 0; i < checkedItems.size(); i++){
QTreeWidgetItem *item = checkedItems.at(i);
QTableWidgetItem *nameItem = new QTableWidgetItem(item->text(0));
QTableWidgetItem *typeItem = new QTableWidgetItem(item->text(1));
QTableWidgetItem *sizeItem = new QTableWidgetItem(item->text(2));
QTableWidgetItem *dateItem = new QTableWidgetItem(item->text(3));
ui->tableWidget->insertRow(rowCount);
ui->tableWidget->setItem(rowCount, 0, nameItem);
ui->tableWidget->setItem(rowCount, 1, typeItem);
ui->tableWidget->setItem(rowCount, 2, sizeItem);
ui->tableWidget->setItem(rowCount, 3, dateItem);
rowCount++;
}
}
```
在这个示例中,我们在TreeWidget的根节点和子节点上添加了CheckBox,并将勾选的数据保存在了checkedItems列表中。当用户勾选或取消勾选CheckBox时,我们调用了onItemChecked()槽函数获取所有勾选的数据,并将其输出到TableWidget中。
注意,在设置CheckBox时,我们需要为每个CheckBox连接一个stateChanged()信号,并在槽函数中调用onItemChecked()函数。同时,在TableWidget中输出数据时,我们需要使用QTableWidgetItem类创建表格项并将其添加到TableWidget中。
阅读全文