QList和QStringList的用法

.h
#ifndef MYLISTVIEW_H_
#define MYLISTVIEW_H_
#include
class QListView;
class QStandardItemModel;

class MyListView : public QWidget
{
public:
    MyListView(QWidget* parent = 0);
    virtual ~MyListView();
public:
    QListView*              iListView;
    QStandardItemModel*     iStandardItemModel;
 //   QList         
};
#endif /* MYLISTVIEW_H_ */
 
.cpp
#include "MyListView.h"
#include
#include
#include
#include
#include
#include
MyListView::MyListView(QWidget* parent) : QWidget(parent)
{
    iListView = new QListView(this);
    iStandardItemModel = new QStandardItemModel(this);
   
    QStringList strList;
    strList.append("string1");
    strList.append("string2");
    strList.append("string3");
    strList.append("string4");
    strList.append("string5");
    strList.append("string6");
    strList.append("string7");
    // 获得这个strList的个数
    int nCount = strList.size();
    for (int i=0; i     {
        // const T &    at ( int i ) const  这个是函数原型,所以需要强转一下
        QString string = static_cast(strList.at(i));
        QStandardItem* item = new QStandardItem(string);
        iStandardItemModel->appendRow(item);
    }
   
    QList  list;
    list << "item1";
    list << "item2";
    list << "item3";
    list << "item4";
    QString string1("a");
    list << string1;
    list.append("b");
    int mCount = list.size();
    for (int j=0; j     {
        QString string2 = static_cast(list.at(j));
        QStandardItem* item2 = new QStandardItem(string2);
        iStandardItemModel->appendRow(item2);
    }
   
    iListView->setModel(iStandardItemModel);
    iListView->setGeometry(QRect(0, 0, 360, 640));   
}
MyListView::~MyListView()
{
    // TODO Auto-generated destructor stub
}
main.cpp
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
 
    MyListView lv;
    lv.show();
   
    return a.exec();
}

你可能感兴趣的:(QT开发)