QListWidget、QTreeWidget和QTableWidget用法详细说明

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/lvdepeng123/article/details/84994594

我们了解了 model/view 架构的基本概念。现在我们从最简单的QListWidgetQTreeWidgetQTableWidget三个类开始了解最简单的 model/view 的使用。这部分内容的确很难组织。首先,从最标准的 model/view 开始,往往会纠结于复杂的代码;但是,如果从简单的 QListWidgetQTreeWidgetQTableWidget开始,由于这三个类都是继承自各自的 view 类,很难避免 model/view 的相关内容。于是,我们这部分的组织是,首先进行简单的数据显示,更复杂的设置则放在后面的章节。

QListWidget

我们要介绍的第一个是QListWidget。先来看下面的代码示例:

 /**************************
     *
     * 下面是listwidget的显示方式
     *
    ***********************/



    label = new QLabel(nullptr);
    label->setFixedWidth(70);

    listWidget = new QListWidget(nullptr);
    listWidget->setViewMode(QListView::ListMode);//设置list的显示方式

    //三项中向列表中添加列表项的方式
    new QListWidgetItem(QIcon(QApplication::applicationDirPath()+"/8.jpg"), tr("Chrome"), listWidget);
    new QListWidgetItem(QIcon(QApplication::applicationDirPath()+"/9.jpg"), tr("Firefox"), listWidget);

    listWidget->addItem(new QListWidgetItem(QIcon( QApplication::applicationDirPath()+"/1.jpg"), tr("IE")));
    listWidget->addItem(new QListWidgetItem(QIcon(QApplication::applicationDirPath()+"/2.jpg"), tr("Netscape")));
    listWidget->addItem(new QListWidgetItem(QIcon(QApplication::applicationDirPath()+"/3.jpg"), tr("Opera")));
    listWidget->addItem(new QListWidgetItem(QIcon(QApplication::applicationDirPath()+"/4.jpg"), tr("Safari")));
    listWidget->addItem(new QListWidgetItem(QIcon(QApplication::applicationDirPath()+"/5.jpg"), tr("TheWorld")));
    listWidget->addItem(new QListWidgetItem(QIcon(QApplication::applicationDirPath()+"/6.jpg"), tr("Traveler")));

    QListWidgetItem *newItem = new QListWidgetItem;
    newItem->setIcon(QIcon(QApplication::applicationDirPath()+"/7.jpg"));
    newItem->setText(tr("Maxthon"));
    listWidget->insertItem(3, newItem); //调整位置大小

    int * a = new int(1);
    qDebug()<<"内存:"<<*a;

    QListWidgetItem *newItem1 = new QListWidgetItem(*newItem);//引用是传值
    newItem1->setText("字都是法国");

    QHBoxLayout *layout = new QHBoxLayout;
    layout->addWidget(label);
    layout->addWidget(listWidget);

    setLayout(layout);

    connect(listWidget, SIGNAL(currentTextChanged(QString)),
            label, SLOT(setText(QString)));

    qDebug()<<"exe dir:"<< QApplication::applicationDirPath();

QListWidget是简单的列表组件。当我们不需要复杂的列表时,可以选择QListWidgetQListWidget中可以添加QListWidgetItem类型作为列表项,QListWidgetItem即可以有文本,也可以有图标。上面的代码显示了三种向列表中添加列表项的方法(实际是两种,后两种其实是一样的),我们的列表组件是listWidget,那么,向listWidget添加列表项可以:第一,使用下面的语句

new QListWidgetItem(QIcon(":/Chrome.png"), tr("Chrome"), listWidget);

第二种

listWidget->addItem(new QListWidgetItem(QIcon(":/IE.png"), tr("IE")));
// 或者
QListWidgetItem *newItem = new QListWidgetItem;
newItem->setIcon(QIcon(":/Maxthon.png"));
newItem->setText(tr("Maxthon"));
listWidget->insertItem(3, newItem);

注意这两种添加方式的区别:第一种需要在构造时设置所要添加到的QListWidget对象;第二种方法不需要这样设置,而是要调用addItem()或者insertItem()自行添加。如果你仔细查阅QListWidgetItem的构造函数,会发现有一个默认的type参数。该参数有两个合法值:QListWidgetItem::Type(默认)和QListWidgetItem::UserType。如果我们继承QListWidgetItem,可以设置该参数,作为我们子类的一种区别,以便能够在QListWidget区别处理不同子类。

我们的程序的运行结果如下:

图片的位置可以放在任意文件夹下,可以路径写的正确,本文中的图片是放在程序运行的debug的文件夹下的

QTreeWidget

我们要介绍的第二个组件是QTreeWidget。顾名思义,这是用来展示树型结构(也就是层次结构)的。同前面说的QListWidget类似,这个类需要同另外一个辅助类QTreeWidgetItem一起使用。不过,既然是提供方面的封装类,即便是看上去很复杂的树,在使用这个类的时候也是显得比较简单的。当不需要使用复杂的QTreeView特性的时候,我们可以直接使用QTreeWidget代替。

下面我们使用代码构造一棵树:

扫描二维码关注公众号,回复: 4833885 查看本文章
 QTreeWidget *treeWidget = new QTreeWidget(nullptr);
   // treeWidget->setColumnCount(3);//设置是那棵树下
    QStringList listLabels;
    listLabels<<"文件夹"<<"显示";
    treeWidget->setHeaderLabels(listLabels);
    treeWidget->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOn);//默认是适当显示滚动条,现状总是显示
    treeWidget->setColumnWidth(0,150);
    treeWidget->setColumnWidth(1,150);
    //设置滚动轮
    treeWidget->setAutoScroll(true);
    treeWidget->hasAutoScroll();
    QScrollBar *scrollBar = new QScrollBar(Qt::Horizontal,nullptr);
    scrollBar->setMouseTracking(true);
    scrollBar->setTracking(true);
    scrollBar->triggerAction(QAbstractSlider::SliderPageStepSub);
    treeWidget->setHorizontalScrollBar(scrollBar);
    treeWidget->setHorizontalScrollMode(QAbstractItemView::ScrollPerPixel);
    treeWidget->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
   // treeWidget->setColumnHidden(1,true);

   // treeWidget->setAllColumnsShowFocus(true);显示键盘焦点
    QTreeWidgetItem *root = new QTreeWidgetItem(treeWidget,QStringList()<<QString("Root1")<<"目录1");
    root->setIcon(0,QIcon( QApplication::applicationDirPath()+"/dir.jpg"));
    QTreeWidgetItem *root1 = new QTreeWidgetItem(treeWidget,QStringList()<<QString("Root2")<<"目录2");
    root1->setIcon(0,QIcon( QApplication::applicationDirPath()+"/dir.jpg"));
    QTreeWidgetItem *root2 = new QTreeWidgetItem(treeWidget,QStringList()<<QString("Root3")<<"目录3");
    root2->setIcon(0,QIcon( QApplication::applicationDirPath()+"/dir.jpg"));

    //new QTreeWidgetItem(root, QStringList(QString("Leaf 1")));
    QTreeWidgetItem *leaf1 = new QTreeWidgetItem(root,QStringList()<<QString("Leaf 1")<<"叶子1.1");
    leaf1->setIcon(0,QIcon( QApplication::applicationDirPath()+"/dir.jpg"));

    QTreeWidgetItem *leaf2 = new QTreeWidgetItem(root,QStringList()<<QString("Leaf 2")<<"叶子1.2");
    leaf2->setIcon(0,QIcon( QApplication::applicationDirPath()+"/dir.jpg"));

    QTreeWidgetItem *leaf3 = new QTreeWidgetItem(root,QStringList()<<QString("Leaf 3")<<"叶子1.3");
    leaf3->setIcon(0,QIcon( QApplication::applicationDirPath()+"/dir.jpg"));


    QTreeWidgetItem *leaf4 = new QTreeWidgetItem(root1,QStringList()<<QString("Leaf 1")<<"叶子2.1");
    leaf4->setIcon(0,QIcon( QApplication::applicationDirPath()+"/dir.jpg"));
    QTreeWidgetItem *leaf5 = new QTreeWidgetItem(root1,QStringList()<<QString("Leaf 2")<<"叶子2.2");
    leaf5->setIcon(0,QIcon( QApplication::applicationDirPath()+"/dir.jpg"));
    QTreeWidgetItem *leaf6 = new QTreeWidgetItem(root1,QStringList()<<QString("Leaf 3")<<"叶子2.3");
    leaf6->setIcon(0,QIcon( QApplication::applicationDirPath()+"/dir.jpg"));


    QTreeWidgetItem *leaf7 = new QTreeWidgetItem(root2,QStringList()<<QString("Leaf 1")<<"叶子3.1");
    leaf7->setIcon(0,QIcon( QApplication::applicationDirPath()+"/dir.jpg"));
    QTreeWidgetItem *leaf8 = new QTreeWidgetItem(root2,QStringList()<<QString("Leaf 2")<<"叶子3.1");
    leaf8->setIcon(0,QIcon( QApplication::applicationDirPath()+"/dir.jpg"));
    QTreeWidgetItem *leaf9 = new QTreeWidgetItem(root2,QStringList()<<QString("Leaf 3")<<"叶子3.3");
    leaf9->setIcon(0,QIcon( QApplication::applicationDirPath()+"/dir.jpg"));





    leaf1->setCheckState(0, Qt::Checked);
    leaf2->setCheckState(0, Qt::Checked);
    leaf3->setCheckState(0,Qt::Checked);
    leaf4->setCheckState(0, Qt::Checked);
    leaf5->setCheckState(0, Qt::Checked);
    leaf6->setCheckState(0,Qt::Checked);
    leaf7->setCheckState(0, Qt::Checked);
    leaf8->setCheckState(0, Qt::Checked);
    leaf9->setCheckState(0,Qt::Checked);

    QList<QTreeWidgetItem *> rootList,rootList1,rootList2;
    rootList << root;
    rootList1<< root1;
    rootList2<< root2;
    treeWidget->insertTopLevelItems(0, rootList);
    treeWidget->insertTopLevelItems(1, rootList1);
    treeWidget->insertTopLevelItems(2, rootList2);
   // treeWidget->setHeaderItem(root);
    int nCount = treeWidget->columnCount();

    qDebug()<<"nCount "<<nCount;
    treeWidget->setWindowTitle("视图 widget");
    treeWidget->show();
   // treeWidget->clear();

    int h = treeWidget->height();
    int w = treeWidget->width();
    qDebug()<<"h w"<<h<<w;

 首先,我们创建了一个QTreeWidget实例。然后我们调用setColumnCount()函数设定栏数。这个函数的效果我们会在下文了解到。最后,我们向QTreeWidget添加QTreeWidgetItemQTreeWidgetItem有很多重载的构造函数。我们在这里看看其中的一个,其余的请自行查阅文档。这个构造函数的签名如下:

QTreeWidgetItem(QTreeWidget *parent, const QStringList &strings, int type = Type);

在这段代码中,我们创建了作为根的QTreeWidgetItemroot。然后添加了第一个叶节点,之后又添加一个,而这个则设置了可选标记。最后,我们将这个 root 添加到一个QTreeWidgetItem的列表,作为QTreeWidget的数据项。此时你应该想到,既然QTreeWidget接受QList作为项的数据,它就能够支持多棵树的一起显示,而不仅仅是单根树

这次我们没有使用setColumnCount(),而是直接使用QStringList设置了 headers,也就是树的表头。接下来我们使用的还是QStringList设置数据。这样,我们实现的是带有层次结构的树状表格。利用这一属性,我们可以比较简单地实现类似 Windows 资源管理器的界面。唯一的缺点就是无法在第二列在创建相关的树结构,并没有提供相关的api可以操作,并且从现有的api来看,只能实现单列,无法实现多列insertTopLevelItems运行结果如下

QTableWidget

我们要介绍的最后一个是 QTableWidgetQTableWidget并不比前面的两个复杂到哪里去,这点我们可以从代码看出来:

首先我们创建了QTableWidget对象,然后设置列数和行数。接下来使用一个QStringList,设置每一列的标题。我们可以通过调用setItem()函数来设置表格的单元格的数据。这个函数前两个参数分别是行索引和列索引,这两个值都是从 0 开始的,第三个参数则是一个QTableWidgetItem对象。Qt 会将这个对象放在第 row 行第 col 列的单元格中。有关QTableWidgetItem的介绍完全可以参见上面的QListWidgetItemQTreeWidgetItem

具体代码的应用点此资源文件在此

猜你喜欢

转载自blog.csdn.net/lvdepeng123/article/details/84994594