• <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中選擇數據項

            概念

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

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


            讀取選擇狀態
            存儲在選擇模型中indexes可以用selectionIndexes()函數來讀取。它返回一個未排序的model indexes列表,我們可以遍歷它,如果我們知道他們關聯于哪個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);
                 }
            選擇模型在選擇發生變化時會發出信號。這用于通知別的組件包括整體與當前焦點項所發生的變化。我們可以連接selectionChanged()信號到一個槽,檢查當信號產生時哪些項被選擇或被取消選擇。這個槽被調用時帶有兩個參數,它們都是QItemSelection對象,一個包含新被選擇的項,另一個包含新近被取消選擇的項。下面的代碼演示了給新選擇的項添加數據內容,新近被取消選擇的項的內容被清空。
            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()信號來跟蹤當前焦點項.對應的槽就有兩個接收參數,一個表示之前的焦點,另一個表示當前的焦點。
            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標記,其意義一目了然。沿上面例子的結果執行以下代碼:
                 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中的所有項,必須先得創建一個選擇,它包括當前層次上的所有項:
                 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()函數來決定給定項是否是其它項的父項。

            posted on 2007-06-19 14:29 清源游民 閱讀(8390) 評論(0)  編輯 收藏 引用 所屬分類: Qt
            <2006年12月>
            262728293012
            3456789
            10111213141516
            17181920212223
            24252627282930
            31123456

            留言簿(35)

            隨筆分類(78)

            隨筆檔案(74)

            文章檔案(5)

            搜索

            •  

            最新評論

            閱讀排行榜

            評論排行榜

            999久久久无码国产精品| 久久成人永久免费播放| 欧美亚洲色综久久精品国产| 久久精品国产2020| 狠狠久久亚洲欧美专区| 久久er国产精品免费观看8| 国产亚洲精品久久久久秋霞| 国产成人久久精品一区二区三区| 久久91亚洲人成电影网站| 精品久久综合1区2区3区激情 | 中文成人久久久久影院免费观看| 久久综合色之久久综合| 久久久久久久久无码精品亚洲日韩| 亚洲狠狠久久综合一区77777| 亚洲va久久久久| 国产综合成人久久大片91| 无码人妻少妇久久中文字幕蜜桃| 国产精品激情综合久久| 亚洲国产精品无码久久久蜜芽 | 亚洲国产精品无码久久久不卡| 日本福利片国产午夜久久| 亚洲精品乱码久久久久久按摩| 久久久精品国产Sm最大网站| 精品永久久福利一区二区| 日韩欧美亚洲综合久久 | 99久久无色码中文字幕人妻 | 久久精品日日躁夜夜躁欧美| 国产精品亚洲综合专区片高清久久久 | 久久精品国产亚洲αv忘忧草| 久久91精品国产91久久小草| 无码人妻精品一区二区三区久久 | 亚洲精品白浆高清久久久久久| 久久久久婷婷| 国产成人久久精品麻豆一区| 久久久久女人精品毛片| 婷婷久久香蕉五月综合加勒比| 亚洲色欲久久久久综合网| 国产欧美久久久精品影院| 少妇熟女久久综合网色欲| 成人久久免费网站| 久久精品国产亚洲AV无码偷窥|