QT选择模型与信号的关联
QT选择模型与信号的关联.rar-QT文档类资源-CSDN下载QT选择模型与信号的关联.rarhttps://txwtech.blog.csdn.net/art更多下载资源、学习资料请访问CSDN下载频道.https://download.csdn.net/download/txwtech/86736370?spm=1001.2014.3001.5503
#ifndef MAINWINDOW16_6MYSELECTION_H
#define MAINWINDOW16_6MYSELECTION_H
#include
class QTableView;//new add
//section3 P365
//src16_8
class QItemSelection;
class QModelIndex;
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow16_6myselection; }
QT_END_NAMESPACE
class MainWindow16_6myselection : public QMainWindow
{
Q_OBJECT
public:
MainWindow16_6myselection(QWidget *parent = nullptr);
~MainWindow16_6myselection();
private:
Ui::MainWindow16_6myselection *ui;
//new add
QTableView *tableView;
//QItemSelectionModel::Toggle的效果
//section3 P366
QTableView *tableView2;
public slots:
void GetCurrentItemData();
void ToggleSelection();
//section3 P365
void UpdateSelection(const QItemSelection &selected,const QItemSelection &deselected);
void ChangeCurrent(const QModelIndex ¤t,const QModelIndex &previous);
};
#endif // MAINWINDOW16_6MYSELECTION_H
#include "mainwindow16_6myselection.h"
#include "ui_mainwindow16_6myselection.h"
#include
#include
#include
//#include
//使用选择模型
MainWindow16_6myselection::MainWindow16_6myselection(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow16_6myselection)
{
ui->setupUi(this);
//new add
QStandardItemModel *model2=new QStandardItemModel(7,4,this);
for(int row=0;rowsetModel(model2);//模型放入tableview表格视图
setCentralWidget(tableView);//表格视图放入到主界面的控件区域
//获取视图项目的选择模型
QItemSelectionModel *selectionModel2 = tableView->selectionModel();//定义左上角和右下角的索引,然后使用这两个索引创建选择
QModelIndex topLeft;
QModelIndex bottomRight;
topLeft=model2->index(1,1,QModelIndex());
bottomRight=model2->index(5,2,QModelIndex());
QItemSelection selection(topLeft,bottomRight);//选择模型
//使用指定的选择模式来选择项目
selectionModel2->select(selection,QItemSelectionModel::Select);
//添加工具栏动作:
//添加方法:https://blog.csdn.net/txwtech/article/details/126593636?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522166496282316782395354464%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fblog.%2522%257D&request_id=166496282316782395354464&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~blog~first_rank_ecpm_v1~rank_v31_ecpm-1-126593636-null-null.nonecase&utm_term=qt%20%E5%B7%A5%E5%85%B7%E6%A0%8F&spm=1018.2226.3001.4450
ui->toolBar->addAction(tr("当前项目"),this,&MainWindow16_6myselection::GetCurrentItemData);
ui->toolBar->addAction(tr("切换选择"),this,&MainWindow16_6myselection::ToggleSelection);
//section3 P365
connect(selectionModel2,&QItemSelectionModel::selectionChanged,this,&MainWindow16_6myselection::UpdateSelection);
connect(selectionModel2,&QItemSelectionModel::currentChanged,this,&MainWindow16_6myselection::ChangeCurrent);
//P366
tableView2=new QTableView;
tableView2->setWindowTitle("tableView2");
tableView2->resize(400,300);
tableView2->setModel(model2);
tableView2->setSelectionModel(selectionModel2);
tableView2->show();
}
MainWindow16_6myselection::~MainWindow16_6myselection()
{
delete ui;
delete tableView2;
}
void MainWindow16_6myselection::GetCurrentItemData()
{
qDebug()index(0,0,QModelIndex());
QModelIndex bottomRight=tableView->model()->index(tableView->model()->rowCount()-1,tableView->model()->columnCount(QModelIndex())-1,QModelIndex());
QItemSelection curSelection(topLeft,bottomRight);
tableView->selectionModel()->select(curSelection,QItemSelectionModel::Toggle);
}
void MainWindow16_6myselection::UpdateSelection(const QItemSelection &selected, const QItemSelection &deselected)
{
QModelIndex index2;
QModelIndexList list2=selected.indexes();
//为现在选择的项目填充值
foreach(index2,list2)
{
QString text2=QString("%1,%2").arg(index2.row()).arg(index2.column());
tableView->model()->setData(index2,text2);
}
list2=deselected.indexes();//获取所有选择项目的索引
//清空上一次选择的项目的内容
foreach(index2,list2)
{
tableView->model()->setData(index2,"");
}
}
void MainWindow16_6myselection::ChangeCurrent(const QModelIndex ¤t, const QModelIndex &previous)
{
qDebug()
关注
打赏
最近更新
- 深拷贝和浅拷贝的区别(重点)
- 【Vue】走进Vue框架世界
- 【云服务器】项目部署—搭建网站—vue电商后台管理系统
- 【React介绍】 一文带你深入React
- 【React】React组件实例的三大属性之state,props,refs(你学废了吗)
- 【脚手架VueCLI】从零开始,创建一个VUE项目
- 【React】深入理解React组件生命周期----图文详解(含代码)
- 【React】DOM的Diffing算法是什么?以及DOM中key的作用----经典面试题
- 【React】1_使用React脚手架创建项目步骤--------详解(含项目结构说明)
- 【React】2_如何使用react脚手架写一个简单的页面?