• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            逛奔的蝸牛

            我不聰明,但我會很努力

               ::  :: 新隨筆 ::  ::  :: 管理 ::

            共有 main.cpp, Widget.h, Widget.cpp, Widget.ui, MyModel.h, MyModel.cpp 六個文件。

            可從此下載整個工程文件: /Files/biao/ModelDemo.zip

            關鍵在于Model中的數據是動態加載的,而不是一次性全加載到Model中。


            /***************************************************

             *              main.cpp

             ***************************************************/

            #include <QtGui/QApplication>

            #include "Widget.h"


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

                QApplication a(argc, argv);

                Widget w;

                w.show();


                return a.exec();

            }


            /***************************************************

             *              Widget.h

             ***************************************************/

            #ifndef WIDGET_H

            #define WIDGET_H


            #include <QWidget>


            namespace Ui {

                class Widget;

            }


            class Widget : public QWidget {

                Q_OBJECT


            public:

                explicit Widget(QWidget *parent = 0);

                ~Widget();


            private:

                Ui::Widget *ui;

            };


            #endif // WIDGET_H



            /***************************************************

             *              Widget.cpp

             ***************************************************/

            #include "Widget.h"

            #include "ui_Widget.h"


            #include "MyModel.h"


            Widget::Widget(QWidget *parent) : QWidget(parent), ui(new Ui::Widget) {

                ui->setupUi(this);


                MyModel *model = new MyModel(1000, 20, this);

                ui->tableView->setModel(model);

                ui->tableView->horizontalHeader()->setStretchLastSection(true);

            }


            Widget::~Widget() {

                delete ui;

            }



            /***************************************************

             *              MyModel.h

             ***************************************************/

            #ifndef MYMODEL_H

            #define MYMODEL_H


            #include <QAbstractTableModel>

            #include <QHash>

            #include <QList>


            class MyModel : public QAbstractTableModel {

            public:

                explicit MyModel(int rowCount = 1000, int pageSize = 10, QObject *parent = 0);


                virtual int columnCount(const QModelIndex &parent = QModelIndex()) const;

                virtual int rowCount(const QModelIndex &parent = QModelIndex()) const;

                virtual QVariant data( const QModelIndex &index, int role = Qt::DisplayRole) const;


            private:

                QHash<int, int> m_pageHash; // key頁碼值, value是此頁的第一項數據在dataList中的下標值

                QList<int> m_dataList; // 儲數據的容器


                int m_pageSize; // 頁顯示的數據量

                int m_rowCount; // 顯示的行數


                // 這個下標是有規律可尋的, 但是是無序的.

                // 同一頁碼中的數值的下標是有序的, 不同頁碼的數據之間是無序的.

                int indexOfRow(int row) const;


                // 讀取數據, 可是以模擬生成的, 實際工程中很可能是從數據庫中動態讀取

                void fetchData(int page);

            };


            #endif // MYMODEL_H



            /***************************************************

             *              MyModel.cpp

             ***************************************************/

            #include "MyModel.h"

            #include <QDebug>


            MyModel::MyModel(int rowCount, int pageSize, QObject *parent) : QAbstractTableModel(parent) {

                this->m_pageSize = pageSize;

                this->m_rowCount = rowCount;

            }


            int MyModel::columnCount(const QModelIndex &parent) const {

                Q_UNUSED(parent);


                return 1; // 以一列為示例, 如果是多列, data list保存的應該是對象

            }


            int MyModel::rowCount (const QModelIndex &parent) const {

                Q_UNUSED(parent);

                return m_rowCount;

            }


            QVariant MyModel::data( const QModelIndex &index, int role) const {

                if (!index.isValid()) { return QVariant(); }


                if (Qt::DisplayRole == role) {

                    int i = indexOfRow(index.row());

                    return m_dataList.at(i);

                }


                return QVariant();

            }


            int MyModel::indexOfRow(int row) const {

                int page = row / m_pageSize; // 頁顯示pageSize記錄


                if (!m_pageHash.contains(page)) {

                    // 如果此面的數據不存在, 則讀取數據到 data list

                    (const_cast<MyModel *>(this))->fetchData(page);

                }


                return m_pageHash.value(page) + row % m_pageSize;

            }


            // 每次加載數據時,如果是耗時任務, 可以使用進度條顯示加載進度

            // 也可以取消加, 但是這個時候data()函數中返回一個非有效的index

            void MyModel::fetchData(int page) {

                int pageStartIndex = m_dataList.count(); // 儲此頁碼與其所對應的開始下標值

                m_pageHash.insert(page, pageStartIndex);


                // 例如在這里使用分頁查詢, 從數據庫里讀取數據

                for (int i = 0; i < m_pageSize; ++i) {

                    int data = page * m_pageSize + i;

                    m_dataList.append(data);


                    // 快速拖動滾動條, 可以看到中間很多沒必要的數據沒有產生.

                    qDebug() << data;

                }

            }


            @import url(http://www.shnenglu.com/CuteSoft_Client/CuteEditor/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/css/cuteeditor.css);
            posted on 2011-10-27 09:10 逛奔的蝸牛 閱讀(6317) 評論(1)  編輯 收藏 引用 所屬分類: Qt

            評論

            # re: Qt:使用Model-View,動態的加載顯示數據 2012-08-10 15:43 人在深圳
            期待QTreeView代碼,樓主能不能辛苦下哈  回復  更多評論
              

            无码人妻久久一区二区三区免费 | 久久国产高潮流白浆免费观看| 久久九九有精品国产23百花影院| 人妻无码精品久久亚瑟影视| 国产成人综合久久精品尤物| 99精品久久久久中文字幕| 久久久久亚洲AV片无码下载蜜桃 | 99麻豆久久久国产精品免费| 人妻精品久久久久中文字幕69 | 精品久久久久久无码中文野结衣| 97久久国产亚洲精品超碰热| www.久久热.com| 狠狠久久亚洲欧美专区| 久久这里只有精品首页| 久久久久久狠狠丁香| 91麻精品国产91久久久久| 国产成人99久久亚洲综合精品| 久久亚洲欧美日本精品| 久久久久免费视频| 久久久午夜精品| 色婷婷综合久久久久中文| 久久亚洲中文字幕精品有坂深雪| 久久综合亚洲欧美成人| 久久久久四虎国产精品| 久久人人超碰精品CAOPOREN| 午夜精品久久久久久影视riav| 久久精品国产AV一区二区三区| 人妻久久久一区二区三区| 国产成人久久精品麻豆一区 | 亚洲综合精品香蕉久久网97 | 免费精品国产日韩热久久| 2021国内久久精品| 国产情侣久久久久aⅴ免费| 精品久久久久久国产牛牛app| 欧美成人免费观看久久| 国产午夜精品理论片久久影视 | 久久精品视频免费| 欧美国产精品久久高清| 久久精品无码午夜福利理论片| …久久精品99久久香蕉国产| 欧美久久亚洲精品|