[QT]QTreeWidget 的基本用法

QTreeWidget用法实例:

void MainWindow::QTreeWidgetTest()
{
ui->treeWidget->setColumnCount(2);
ui->treeWidget->setHeaderLabels(QStringList()<<"one"<<"two");
AddRoot("hello", "world");
AddChild(itm,"one","hello");
AddChild(itm,"two","hello");
}

void MainWindow::AddRoot(QString name, QString Description)
{
QTreeWidgetItem *itm = new QTreeWidgetItem(ui->treeWidget);
itm->setText(0,name);
itm->setText(1,Description);
ui->treeWidget->addTopLevelItem(itm);
}

void MainWindow::AddChild(QTreeWidgetItem *parent, QString name, QString Description)
{
QTreeWidgetItem *itm = new QTreeWidgetItem();
itm->setText(0,name);
itm->setText(1,Description);
parent->addChild(itm);
}

另一篇有关TreeWidget右键菜单用法的文章:http://blog.csdn.net/liukang325/article/details/23694585

形成一个树状列表,这样会不会更清晰!

    QTreeWidgetItem *rootItem_1 = new QTreeWidgetItem(ui->treeWidget);
    QTreeWidgetItem *childItem_1_1 = new QTreeWidgetItem();
    QTreeWidgetItem *childItem_1_2 = new QTreeWidgetItem();

    QTreeWidgetItem *childItem_1_2_1 = new QTreeWidgetItem();

    QTreeWidgetItem *rootItem_2 = new QTreeWidgetItem(ui->treeWidget);
    QTreeWidgetItem *childItem_2_1 = new QTreeWidgetItem();
    QTreeWidgetItem *childItem_2_2 = new QTreeWidgetItem();
    QTreeWidgetItem *childItem_2_3 = new QTreeWidgetItem();

    rootItem_1->setText(0,QObject::tr("常用文件夹"));
    childItem_1_1->setText(0,QObject::tr("所有未读"));
    childItem_1_2->setText(0,QObject::tr("置顶邮件"));
    childItem_1_2_1->setText(0,QObject::tr("测试"));
    childItem_1_2_1->setForeground(0,QBrush(QColor(Qt::blue)));

    rootItem_2->setText(0,QObject::tr("我的邮箱"));
    childItem_2_1->setText(0,QObject::tr("收件箱"));
    childItem_2_2->setText(0,QObject::tr("草稿箱"));
    childItem_2_3->setText(0,QObject::tr("发件箱"));

    ui->treeWidget->addTopLevelItem(rootItem_1);
    rootItem_1->addChild(childItem_1_1);
    rootItem_1->addChild(childItem_1_2);
    childItem_1_2->addChild(childItem_1_2_1);
    ui->treeWidget->addTopLevelItem(rootItem_2);
    rootItem_2->addChild(childItem_2_1);
    rootItem_2->addChild(childItem_2_2);
    rootItem_2->addChild(childItem_2_3);


你可能感兴趣的:(QT,C++)