QListView

 QT提供了model/view 结构来管理数据与展示数据。

    对于搞J2ee开发的,MVC是再熟悉不过了,Model,View,Controller,qt的model/view模式跟MvC差不多。

    

    model提供数据模型,view展示数据,delegate会对数据项进行渲染。model,view,delegate通过信号/槽机制通信。

    前面在QML中就学过ListView.

   

  1. import Qt 4.7  
  2.   
  3.  ListModel {  
  4.      id: fruitModel  
  5.   
  6.      ListElement {  
  7.          name: "Apple"  
  8.          cost: 2.45  
  9.      }  
  10.      ListElement {  
  11.          name: "Orange"  
  12.          cost: 3.25  
  13.      }  
  14.      ListElement {  
  15.          name: "Banana"  
  16.          cost: 1.95  
  17.      }  
  18.  }  

 

    

  1. Component {  
  2.        id: fruitDelegate  
  3.        Item {  
  4.            width200height50  
  5.            Text { text: name }  
  6.            Text { text: '$' + cost; anchors.right: parent.right }  
  7.   
  8.            // Double the price when clicked.  
  9.            MouseArea {  
  10.                anchors.fill: parent  
  11.                onClicked: fruitModel.setProperty(index, "cost", cost * 2)  
  12.            }  
  13.        }  
  14.    }  

 

    

[c-sharp] view plain copy print ?
  1. import Qt 4.7  
  2.   
  3. istView {  
  4.         anchors.fill: parent  
  5.         model: fruitModel  
  6.         delegate: fruitDelegate  
  7.     }  

 

   下面是一个简单的ListView的例子:

   

  1. #ifndef MAINWINDOW_H   
  2. #define MAINWINDOW_H   
  3. #include <QMainWindow>   
  4.   
  5. #include <QListView>   
  6. #include <QStandardItem>   
  7. #include <QStandardItemModel>   
  8. #include <QModelIndex>   
  9.   
  10. namespace Ui {  
  11.     class MainWindow;  
  12. }  
  13.   
  14. class MainWindow : public QMainWindow  
  15. {  
  16.     Q_OBJECT  
  17.   
  18. public:  
  19.     explicit MainWindow(QWidget *parent = 0);  
  20.     ~MainWindow();  
  21.   
  22. private:  
  23.     Ui::MainWindow *ui;  
  24.     QListView *listView;  
  25.     QStandardItemModel *standardItemModel;  
  26. private slots:  
  27.      void itemClicked(QModelIndex index);  
  28. };  
  29.   
  30. #endif // MAINWINDOW_H  

  

  1. #include "mainwindow.h"   
  2. #include "ui_mainwindow.h"   
  3. #include <QBrush>   
  4. #include <QRadialGradient>   
  5. #include <QDebug>   
  6.   
  7. MainWindow::MainWindow(QWidget *parent) :  
  8.     QMainWindow(parent),  
  9.     ui(new Ui::MainWindow)  
  10. {  
  11.     ui->setupUi(this);  
  12.   
  13.     listView = new QListView(this);  
  14.     standardItemModel = new QStandardItemModel(this);  
  15.   
  16.     QStringList strList;  
  17.     strList.append("string1");  
  18.     strList.append("string2");  
  19.     strList.append("string3");  
  20.     strList.append("string4");  
  21.     strList.append("string5");  
  22.     strList.append("string6");  
  23.     strList.append("string7");  
  24.     strList << "string8";  
  25.     strList += "string9";  
  26.     int nCount = strList.size();  
  27.     for(int i = 0; i < nCount; i++)  
  28.     {  
  29.         QString string = static_cast<QString>(strList.at(i));  
  30.         QStandardItem *item = new QStandardItem(string);  
  31.         if(i % 2 == 1)  
  32.         {  
  33.             QLinearGradient linearGrad(QPointF(0, 0), QPointF(200, 200));  
  34.             linearGrad.setColorAt(0, Qt::darkGreen);  
  35.             linearGrad.setColorAt(1, Qt::yellow);  
  36.             QBrush brush(linearGrad);  
  37.             item->setBackground(brush);  
  38.         }  
  39.         standardItemModel->appendRow(item);  
  40.     }  
  41.     listView->setModel(standardItemModel);  
  42.     listView->setFixedSize(200,300);  
  43.     connect(listView,SIGNAL(clicked(QModelIndex)),this,SLOT(itemClicked(QModelIndex)));  
  44. }  
  45.   
  46. MainWindow::~MainWindow()  
  47. {  
  48.     delete ui;  
  49. }  
  50.   
  51. void MainWindow::itemClicked(QModelIndex index)  
  52. {  
  53.     qDebug() << index.data().toString();  
  54. }  

 

   

   对于以上例子说明:

   QStringList用于提供了一个String的List集合.继承自QList<QString>.

   公共方法:

  


 QStringList
 ()
  QStringList ( const QString & str )
  QStringList ( const QStringList & other )
  QStringList ( const QList<QString> & other )
bool contains ( const QString & str, Qt::CaseSensitivity cs = Qt::CaseSensitive ) const
QStringList filter ( const QString & str, Qt::CaseSensitivity cs = Qt::CaseSensitive ) const
QStringList filter ( const QRegExp & rx ) const
int indexOf ( const QRegExp & rx, int from = 0 ) const
int indexOf ( const QString & value, int from = 0 ) const
int indexOf ( QRegExp & rx, int from = 0 ) const
QString join ( const QString & separator ) const
int lastIndexOf ( const QRegExp & rx, int from = -1 ) const
int lastIndexOf ( const QString & value, int from = -1 ) const
int lastIndexOf ( QRegExp & rx, int from = -1 ) const
int removeDuplicates ()
QStringList & replaceInStrings ( const QString & before, const QString & after, Qt::CaseSensitivity cs = Qt::CaseSensitive )
QStringList & replaceInStrings ( const QRegExp & rx, const QString & after )
void sort ()
QStringList operator+ ( const QStringList & other ) const
QStringList & operator<< ( const QString & str )
QStringList & operator<< ( const QStringList & other )

 添加元素可使用append(),+=,<<

 迭代元素有三种方式:

       

[c-sharp] view plain copy print ?
  1. 使用Java索引风格:  
  2. for (int i = 0; i < font.size(); ++i)  
  3.    cout << fonts.at(i).toLocal8Bit().constData() <<endl;  
  4.   
  5. //Java迭代器风格:   
  6. QStringListIterator iterator(fonts);  
  7. while(iterator.hasNext())  
  8.     count << iterator.next().toLocal8Bit().constData() << endl;  
  9.   
  10. //STL迭代器风格:   
  11. QStringList::const_iterator constIterator;  
  12. for (const_iterator = fonts.constBegin(); const_iterator != fonts.constEnd(); ++constIterator)  
  13.     cout << (*const_iterator).toLocal8Bit().constData() << endl;  
  14.   
  15.    

        还有QList,QString 的一大堆方法 ,呵呵.

      

        QStandardItemModel类提供了一个用来存储自定义数据的普通的model。       

        QStandardItemModel可以用来存储标准的QT数据类型。它是Model/View之一。提供了典型的item-base工作模型。它的Item是QStandardItem类型的.         

        QStandardItemModel实现了QAbstractItemModel接口,也就意味着这个model能被用于提供任何支持这个接口的的view(如QListView,QTableView,QTreeView,和你自己定义的View)

        QStandardItem类提供了用于QStandardItemModel的Item,通常包含文本,图标或复选框等.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值