QListWidget的简单用法

  这一节我们来介绍QListWidget的用法,QListWidget、QTreeWidget和QTableWidget都是将模型和视图结合起来,QListWidget将其显示效果与MFC的ListCtrl控件是一样一样的,下面是其简单的使用。就一个main.cpp

复制代码
#include <QtGui/QApplication>
#include <QWidget>
#include <QLabel>
#include <QListWidget>
#include <QHBoxLayout>
#include <QIcon>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QLabel *label = new QLabel;
    label->setFixedWidth(100);
    QListWidget *listWidget = new QListWidget;
    listWidget->addItem(new QListWidgetItem(QIcon(QObject::tr("images/china.png")), QObject::tr("China")));
    listWidget->addItem(new QListWidgetItem(QIcon(QObject::tr("images/hk.png")), QObject::tr("Hong Kong")));
    listWidget->addItem(new QListWidgetItem(QIcon(QObject::tr("images/macau.png")), QObject::tr("Macau")));
    
    QHBoxLayout *mainlayout = new QHBoxLayout;
    mainlayout->addWidget(listWidget);
    mainlayout->addWidget(label);
    QObject::connect(listWidget, SIGNAL(currentTextChanged(QString)), label, SLOT(setText(QString)));
    
    QWidget *widget = new QWidget;
    widget->setLayout(mainlayout);
    widget->setWindowTitle(QObject::tr("QListWidget Demo"));
    widget->show();

    return a.exec();
}
复制代码

  运行界面如下:

                                         QListWidget的简单用法_第1张图片

你可能感兴趣的:(QListWidget的简单用法)