QTreeWidget的简单用法

时间:2022-11-21 05:02:18

  QTreeWidget的显示效果与MFC的TreeCtrl控件是类似的,QTreeWidget与QTReeView的本质区别是QTreeWidget面向QTreeWidgetItem,而QTReeView是面向model的。

  一个简单的小例子用来阐述QTreeWidget的简单使用。

#include <QtGui/QApplication>
#include <QtCore/QTextCodec>
#include <QTreeWidget>
#include <QWidget>
#include <QHBoxLayout>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    QTextCodec::setCodecForTr(QTextCodec::codecForName("gb18030"));
    QTreeWidget *tree = new QTreeWidget;
    tree->setColumnCount(2);
    QStringList headers;
    headers << QObject::tr("科目") <<QObject::tr("分数");
    tree->setHeaderLabels(headers);

    QStringList zhangsan;
    zhangsan << QObject::tr("张三");
    QTreeWidgetItem *zhangsanroot = new QTreeWidgetItem(tree, zhangsan);
    QStringList zhangsanChinese;
    zhangsanChinese << QObject::tr("语文") << QObject::tr("80");
    QTreeWidgetItem *leaf1 = new QTreeWidgetItem(zhangsanroot, zhangsanChinese);
    zhangsanroot->addChild(leaf1);
    QStringList zhangsanMath;
    zhangsanMath << QObject::tr("数学") << QObject::tr("90");
    QTreeWidgetItem *leaf2 = new QTreeWidgetItem(zhangsanroot, zhangsanMath);
    zhangsanroot->addChild(leaf2);

    QStringList lisi;
    lisi << QObject::tr("李四");
    QTreeWidgetItem *lisiroot = new QTreeWidgetItem(tree, lisi);
    QStringList lisiChinese;
    lisiChinese << QObject::tr("语文") << QObject::tr("90");
    leaf1 = new QTreeWidgetItem(lisiroot, lisiChinese);
    lisiroot->addChild(leaf1);
    QStringList lisiMath;
    lisiMath << QObject::tr("数学") << QObject::tr("100");
    leaf2 = new QTreeWidgetItem(lisiroot, lisiMath);
    lisiroot->addChild(leaf2);

    tree->addTopLevelItem(zhangsanroot);
    tree->addTopLevelItem(lisiroot);

    QHBoxLayout *mainlayout = new QHBoxLayout;
    mainlayout->addWidget(tree);
    QWidget *widget = new QWidget;
    widget->setLayout(mainlayout);
    widget->setWindowTitle(QObject::tr("QTreeWidget Demo"));
    widget->show();
    return a.exec();
}

  运行结果:

                                     QTreeWidget的简单用法