QT项视图之QListWidget

QT的项视图类有:QListWidget,QTableWidget,QTreeWidget

QT的视图有:QListView,QTableView,QTreeView

QT的模型有:QSqlTableModel,QStringListModel,QDirModel..等

头文件:

#ifndef XLISTWIDGET_H

#define XLISTWIDGET_H

#include <QtGui/QDialog>

#include<QMap>

class QDialogButtonBox;

class QIcon;

class QListWidget;

class XListWidget : public QDialog

{

    Q_OBJECT

public:

    XListWidget(const QMap<int,QString>&symbolMap,QWidget *parent = 0);

    ~XListWidget();

private:

    QIcon iconForSymbol(const QString &symbolName);

    QListWidget *listWidget;

    QDialogButtonBox *buttonBox;

};

#endif // XLISTWIDGET_H

cpp文件:

#include<QDebug>

#include <QtGui>

#include "xlistwidget.h"

XListWidget::XListWidget(const QMap<int,QString>&symbolMap,

                         QWidget *parent)

    : QDialog(parent)

{

    listWidget = new QListWidget;

    listWidget->setIconSize(QSize(60,60));

    QMapIterator<int,QString>i(symbolMap);

    while(i.hasNext()){

        i.next();

        QListWidgetItem *item = new QListWidgetItem(i.value(),listWidget);

        item->setIcon(iconForSymbol(i.value()));

        item->setData(Qt::UserRole,i.key());

    }

    QVBoxLayout *mainLayout = new QVBoxLayout;

    mainLayout->addWidget(listWidget);

    setLayout(mainLayout);

}

XListWidget::~XListWidget()

{

}

QIcon XListWidget::iconForSymbol(const QString &symbolName)

{

    QString fileName = ":/images/" + symbolName.toLower();

    qDebug()<<"fileName"<<fileName;

    fileName.replace(' ', '-');

    return QIcon(fileName);

}

main.cpp

 

#include <QtGui/QApplication>

#include "xlistwidget.h"

int main(int argc, char *argv[])

{

    QApplication a(argc, argv);

    QMap<int, QString> symbolMap;

    symbolMap.insert(132, QObject::tr("Data.png"));

    symbolMap.insert(134, QObject::tr("Decision.png"));

    symbolMap.insert(137, QObject::tr("Document.png"));

    symbolMap.insert(138, QObject::tr("Manual Input.png"));

    symbolMap.insert(139, QObject::tr("Manual Operation.png"));

    symbolMap.insert(141, QObject::tr("On Page Reference.png"));

    symbolMap.insert(142, QObject::tr("Predefined Process.png"));

    symbolMap.insert(145, QObject::tr("Preparation.png"));

    symbolMap.insert(150, QObject::tr("Printer.png"));

    symbolMap.insert(152, QObject::tr("Process.png"));

    XListWidget w(symbolMap);

    w.show();

    return a.exec();

}

 

你可能感兴趣的:(widget)