日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Qt模型、视图解读之视图

發布時間:2025/3/15 编程问答 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Qt模型、视图解读之视图 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

2、視圖類

視圖的概述:

視圖包含了模型中的數據項,并將它們呈現給用戶。

視圖通常管理從模型獲取數據的整體布局,它們可以自己渲染獨立的數據項,也可以使用委托來處理渲染和編輯。

視圖的特性:

視圖還可以處理項目間的導航,以及項目選擇的某些方面(選擇行為,選擇模式)。

視圖可以實現一些基本的用戶接口特性,如上下文菜單和拖放等

視圖可以提供默認的編輯實現,也可以和委托一起來提供一個自定義的編輯器。

QTableView和QTreeView,可以顯示表頭。

Qt提供了QListView、QTableView、QTreeView 三個現成的視圖。

視圖中的項目選擇:

視圖中被選擇的項目的信息存儲在一個QItemSelectionModel實例中,這樣被選擇的項目模型索引便保持在一個獨立的模型中,與所有的視圖都是獨立的。

選擇由選擇范圍指定,只需要記錄每一個選擇范圍開始和結束的模型索引即可。

選擇可以看作是在選擇模型中保存的一個模型索引集合,最近的項目選擇被稱為當前選擇。

1、當前項目和被選擇的項目

視圖中總是有一個當前項目和一個被選擇的項目,兩者是兩個獨立的狀態。

當操作選擇時,可以將QItemSeletionModel看作一個項目模型中所有項目的選擇狀態的一個記錄。

2、使用選擇模型

一個視圖的選擇模型可以使用selectionModel()函數獲得,在多個視圖之間可以使用setSelectionModel()函數來共享該選擇模型。

視圖類中提供了幾個比較方便的函數來選擇,如:

seletColumn()選擇指定的一列項目;

selectCoumns()選擇指定的多列項目;

selectRow()選擇指定的一行項目;

selectRows()選擇指定的多行項目;

下面看一個實例:

該實例,是表格模型的選擇處理。
相關類的定義如下,實現了表格數據項的訪問,數據項選擇的處理

#ifndef MAINWINDOW_H #define MAINWINDOW_H#include <QMainWindow> class QTableView; class QItemSelection; class QModelIndex;namespace Ui {class MainWindow; }class MainWindow : public QMainWindow {Q_OBJECTpublic:explicit MainWindow(QWidget *parent = 0);~MainWindow();public slots:void getCurrentItemData();void toggleSelection();void updateSelection(const QItemSelection &selected,const QItemSelection &deselected);void changeCurrent(const QModelIndex &current, const QModelIndex &previous);private:Ui::MainWindow *ui;QTableView *tableView;QTableView *tableView2; };#endif // MAINWINDOW_H

具體成員函數如下:

#include "mainwindow.h" #include "ui_mainwindow.h" #include <QStandardItemModel> #include <QTableView> #include <QDebug>MainWindow::MainWindow(QWidget *parent) :QMainWindow(parent),ui(new Ui::MainWindow) {ui->setupUi(this);QStandardItemModel *model = new QStandardItemModel(7, 4, this);for (int row = 0; row < 7; ++row) {for (int column = 0; column < 4; ++column) {QStandardItem *item = new QStandardItem(QString("%1").arg(row * 4 + column));model->setItem(row, column, item);}}tableView = new QTableView;tableView->setModel(model);setCentralWidget(tableView);// 獲取視圖的項目選擇模型QItemSelectionModel *selectionModel = tableView->selectionModel();// 定義左上角和右下角的索引,然后使用這兩個索引創建選擇QModelIndex topLeft;QModelIndex bottomRight;topLeft = model->index(1, 1, QModelIndex());bottomRight = model->index(5, 2, QModelIndex());QItemSelection selection(topLeft, bottomRight);// 使用指定的選擇模式來選擇項目selectionModel->select(selection, QItemSelectionModel::Select);ui->mainToolBar->addAction(tr("now items"), this, SLOT(getCurrentItemData()));ui->mainToolBar->addAction(tr("change selection"), this, SLOT(toggleSelection()));connect(selectionModel, SIGNAL(selectionChanged(QItemSelection,QItemSelection)),this, SLOT(updateSelection(QItemSelection,QItemSelection)));connect(selectionModel, SIGNAL(currentChanged(QModelIndex,QModelIndex)),this, SLOT(changeCurrent(QModelIndex,QModelIndex)));// 多個視圖共享選擇tableView2 = new QTableView;tableView2->setWindowTitle("tableView2");tableView2->resize(400, 300);tableView2->setModel(model);tableView2->setSelectionModel(selectionModel);tableView2->show(); }MainWindow::~MainWindow() {delete ui;delete tableView2; }// 輸出當前項目的內容 void MainWindow::getCurrentItemData() {qDebug() << tr("Items now is:")<< tableView->selectionModel()->currentIndex().data().toString(); }// 切換選擇的項目 void MainWindow::toggleSelection() {QModelIndex topLeft = tableView->model()->index(0, 0, QModelIndex());QModelIndex bottomRight = tableView->model()->index(tableView->model()->rowCount(QModelIndex())-1,tableView->model()->columnCount(QModelIndex())-1, QModelIndex());QItemSelection curSelection(topLeft, bottomRight);tableView->selectionModel()->select(curSelection, QItemSelectionModel::Toggle); }// 更新選擇 void MainWindow::updateSelection(const QItemSelection &selected,const QItemSelection &deselected) {QModelIndex index;QModelIndexList list = selected.indexes();// 為現在選擇的項目填充值foreach (index, list) {QString text = QString("(%1,%2)").arg(index.row()).arg(index.column());tableView->model()->setData(index, text);}list = deselected.indexes();// 清空上一次選擇的項目的內容foreach (index, list) {tableView->model()->setData(index, "");} }// 改變當前項目 void MainWindow::changeCurrent(const QModelIndex &current,const QModelIndex &previous) {qDebug() << tr("move(%1,%2) to (%3,%4)").arg(previous.row()).arg(previous.column()).arg(current.row()).arg(current.column()); }

C++ 的學習周期挺長的。Qt的學習周期也是很長,精通一門語言沒有三五年的積累,很難說,就此可以獨擋一面了。

總結

以上是生活随笔為你收集整理的Qt模型、视图解读之视图的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。