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();
}

  运行界面如下:

                                         

posted @ 2012-10-16 19:44  venow  阅读(47388)  评论(1编辑  收藏  举报