• <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>

            清源游民 gameogre@gmail.com

            在views中選擇數(shù)據(jù)項

            概念

            用于新的view類中的選擇模型比Qt3中的模型有了很大的改進。它為基于model/view架構的選擇提供了更為全面的描述。盡管對提供了的views來說,負責操縱選擇的標準類已經足以應付,但是你也可以創(chuàng)建特定的選擇模型來滿足你特殊的需求。
            關于在view被選擇的數(shù)據(jù)項的信息保持在QItemSelectionModel類的實例中。它也為每個獨立的model中的數(shù)據(jù)項維護model indexes信息,與任何views都關聯(lián)關系。既然一個model可用于多個views,那么在多個views之間共享選擇信息也是可以做到的,這使得多個views可以以一致的方式進行顯示。
            選擇由多個選擇范圍組成。通過僅僅記錄開始model indexes與結束model indexes,最大化地記錄了可以選擇的范圍。非連續(xù)選擇數(shù)據(jù)項由多個選擇范圍來描述。選擇模型記錄model indexes的集合來描述一個選擇。最近選擇的數(shù)據(jù)項被稱為current selection。應用程序可以通過使用某種類型的選擇命令來修改選擇的效果。
            在進行選擇操作時,可以把QItemSelectionModel看成是model中所有數(shù)據(jù)項選擇狀態(tài)的一個記錄。一旦建立一個選擇模型,所有項的集合都可以選擇,撤消選擇,或者選擇狀態(tài)進行切換而不需要知道哪個數(shù)據(jù)項是否已經被選擇過。所有被選擇的項的indexes在任何時候都可以得到,通過信號槽機制可以通知別的組件發(fā)生的變化。

            使用選擇模型
            標準view類提供了缺省的選擇模型,它們可以在大次數(shù)程序中使用。一個view中的選擇模型可以通過調用view的函數(shù)selectionModel()取得,也可以通過setSelectionModel()在多個views之間共享選擇模型,因此總的來說構建一個新的模型一般情況不太必要。
            通過給QItemSelection指定一個model,一對model indexes,可以創(chuàng)建一個選擇。indexes的用法依賴于給定的model,這兩個indexes被解釋成選擇的區(qū)塊中的左上角項和右下角項。model中的項的選擇服從于選擇模型。

            選擇項
            構建一個table model ,它有32個項,用一個table view進行顯示:
                 TableModel *model = new TableModel(8, 4, &app);

                 QTableView *table = new QTableView(0);
                 table->setModel(model);

                 QItemSelectionModel *selectionModel = table->selectionModel();
                 QModelIndex topLeft;
                 QModelIndex bottomRight;

                 topLeft = model->index(0, 0, QModelIndex());
                 bottomRight = model->index(5, 2, QModelIndex());
                
                  QItemSelection selection(topLeft, bottomRight);
                 selectionModel->select(selection, QItemSelectionModel::Select);
            結果如下:


            讀取選擇狀態(tài)
            存儲在選擇模型中indexes可以用selectionIndexes()函數(shù)來讀取。它返回一個未排序的model indexes列表,我們可以遍歷它,如果我們知道他們關聯(lián)于哪個model的話。
                QModelIndexList indexes = selectionModel->selectedIndexes();
                 QModelIndex index;

                 foreach(index, indexes) {
                     QString text = QString("(%1,%2)").arg(index.row()).arg(index.column());
                     model->setData(index, text);
                 }
            選擇模型在選擇發(fā)生變化時會發(fā)出信號。這用于通知別的組件包括整體與當前焦點項所發(fā)生的變化。我們可以連接selectionChanged()信號到一個槽,檢查當信號產生時哪些項被選擇或被取消選擇。這個槽被調用時帶有兩個參數(shù),它們都是QItemSelection對象,一個包含新被選擇的項,另一個包含新近被取消選擇的項。下面的代碼演示了給新選擇的項添加數(shù)據(jù)內容,新近被取消選擇的項的內容被清空。
            void MainWindow::updateSelection(const QItemSelection &selected,
                 const QItemSelection &deselected)
             {
                 QModelIndex index;
                 QModelIndexList items = selected.indexes();

                 foreach (index, items) {
                     QString text = QString("(%1,%2)").arg(index.row()).arg(index.column());
                     model->setData(index, text);
                 }

                 items = deselected.indexes();

                 foreach (index, items)
                  model->setData(index, "");
             }
            也可以通過響應currentChanged()信號來跟蹤當前焦點項.對應的槽就有兩個接收參數(shù),一個表示之前的焦點,另一個表示當前的焦點。
            void MainWindow::changeCurrent(const QModelIndex &current,
                 const QModelIndex &previous)
             {
                 statusBar()->showMessage(
                     tr("Moved from (%1,%2) to (%3,%4)")
                         .arg(previous.row()).arg(previous.column())
                         .arg(current.row()).arg(current.column()));
             }

            更新選擇
            選擇指令是通過選擇標志提供的,它被定義在QItemSelectionModel::SelectionFlag中。常用的有Select標記,Toggle標記,Deselect標記,Current標記,Clear標記,其意義一目了然。沿上面例子的結果執(zhí)行以下代碼:
                 QItemSelection toggleSelection;

                 topLeft = model->index(2, 1, QModelIndex());
                 bottomRight = model->index(7, 3, QModelIndex());
                 toggleSelection.select(topLeft, bottomRight);

                 selectionModel->select(toggleSelection, QItemSelectionModel::Toggle);
            結果如下:


            缺省情況下,選擇指令只針對單個項(由model indexes指定)。然而,選擇指令可以通過與另外標記的結合來改變整行和整列。舉例來說,假如你只使用一個index來調用select(),但是用Select標記與Rows標記的組合,那么包括那個項的整行都將被選擇。看以下示例:
                 QItemSelection columnSelection;

                 topLeft = model->index(0, 1, QModelIndex());
                 bottomRight = model->index(0, 2, QModelIndex());

                 columnSelection.select(topLeft, bottomRight);

                 selectionModel->select(columnSelection,
                 QItemSelectionModel::Select | QItemSelectionModel::Columns);

                 QItemSelection rowSelection;

                 topLeft = model->index(0, 0, QModelIndex());
                 bottomRight = model->index(1, 0, QModelIndex());

                 rowSelection.select(topLeft, bottomRight);

                 selectionModel->select(rowSelection,
                 QItemSelectionModel::Select | QItemSelectionModel::Rows);
            結果如下


            選擇模型中所有項
            為了選擇model中的所有項,必須先得創(chuàng)建一個選擇,它包括當前層次上的所有項:
                 QModelIndex topLeft = model->index(0, 0, parent);
                 QModelIndex bottomRight = model->index(model->rowCount(parent)-1,
                  model->columnCount(parent)-1, parent);

                QItemSelection selection(topLeft, bottomRight);
                 selectionModel->select(selection, QItemSelectionModel::Select);
            頂級index可以這樣:
            QModelIndex parent = QModelIndex();
            對具有層次結構的model來說,可以使用hasChildren()函數(shù)來決定給定項是否是其它項的父項。

            posted on 2007-06-19 14:29 清源游民 閱讀(8373) 評論(0)  編輯 收藏 引用 所屬分類: Qt
            <2009年10月>
            27282930123
            45678910
            11121314151617
            18192021222324
            25262728293031
            1234567

            留言簿(35)

            隨筆分類(78)

            隨筆檔案(74)

            文章檔案(5)

            搜索

            •  

            最新評論

            閱讀排行榜

            評論排行榜

            亚洲七七久久精品中文国产| 久久黄视频| 久久精品九九亚洲精品| 精品久久久久久中文字幕| 草草久久久无码国产专区| 女人高潮久久久叫人喷水| 无码人妻精品一区二区三区久久久| 久久婷婷成人综合色综合| 久久96国产精品久久久| 青草久久久国产线免观| 亚洲伊人久久精品影院| 久久婷婷人人澡人人| 2021久久精品国产99国产精品| 久久精品国产亚洲精品| 成人国内精品久久久久影院| 久久性精品| 久久久久一本毛久久久| 狠狠色丁香久久婷婷综合五月| 欧美精品一区二区久久| 99久久精品国产一区二区三区| 伊人久久大香线蕉AV色婷婷色| 久久亚洲中文字幕精品一区四 | 久久青青草原国产精品免费 | 久久影院久久香蕉国产线看观看| 精品无码久久久久国产动漫3d| 久久99精品国产麻豆婷婷| 久久久噜噜噜久久熟女AA片| 久久毛片一区二区| 久久久99精品成人片中文字幕| 久久精品国产一区| 狠狠色婷婷久久一区二区三区| 久久人人爽人人爽人人片AV东京热 | 亚洲人成网亚洲欧洲无码久久| 久久久精品波多野结衣| 91精品日韩人妻无码久久不卡| 91精品国产色综合久久| 久久精品国产清高在天天线| 久久精品国产亚洲av麻豆小说 | 亚洲欧美精品伊人久久| 久久精品人成免费| 精品久久久久久国产潘金莲|