QT-纯代码控件-QListWidget(列表栏)

实现一个简单的列表栏功能

1.新建一个无ui界面的工程。其基类为widget

2.在工程中添加资源文件,用以存放ico图片文件

在这里插入图片描述
这里的ico图片文件是添加到ListWidget中的item中的图标,使列表栏展现图片化的效果

3.代码实现

list.h

添加所需控件的头文件即可

#include <QLabel>
#include <QListWidget>

list.cpp

#include "list.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
{
    lable = new QLabel(tr("ohuohuo"),this);         //创建标签
    lable->setFixedWidth(70);                       //设置标签宽度
    list = new QListWidget(this);                   //创建列表框
    //添加图片
    list->addItem(new QListWidgetItem(QIcon(":/png/png_file/cry.png"),tr("cry")));   //使用路径
    list->addItem(new QListWidgetItem(QIcon(":/png/png_file/laugh.png"),tr("laugh")));
    list->addItem(new QListWidgetItem(QIcon(":/png/png_file/laugh_cry.png"),tr("laugh and cry")));
    list->addItem(new QListWidgetItem(QIcon(":/png/png_file/o.png"),tr("o")));

    //this->resize(400,400);                        //设置窗体大小
    lable->setGeometry(40,40,80,20);                //设置标签大小
    list->setGeometry(20,20,200,100);               //设置列表框大小
    //关联列表和标签
    connect(list,SIGNAL(currentTextChanged(QString)),lable,
            SLOT(setText(QString)));
}

Widget::~Widget()
{

}

4.效果展示

在这里插入图片描述

5.不足

1.没有将列表栏做成一垂直导航栏的形式,如层叠窗口那样在第二页面显示内容
发布了43 篇原创文章 · 获赞 7 · 访问量 9052

猜你喜欢

转载自blog.csdn.net/qq_41488943/article/details/96430721