-
Notifications
You must be signed in to change notification settings - Fork 366
/
Copy pathmainwindow.cpp
73 lines (61 loc) · 1.99 KB
/
mainwindow.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "MyTableModel.h"
#include "MyStandardItemModel.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
initMyTableModel();
initMyStandardItemModel();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::initMyTableModel()
{
QTableView *view=ui->tableViewA;
MyTableModel *model=new MyTableModel;
view->setModel(model);
view->setSelectionMode(QAbstractItemView::SingleSelection); //不是必要的
//可以配合行/列选中,需要在Model中做相应处理
//view->setSelectionBehavior(QAbstractItemView::SelectRows);
view->setDragEnabled(true);
view->setDefaultDropAction(Qt::MoveAction); //不是必要的
view->setDragDropMode(QAbstractItemView::InternalMove);
}
void MainWindow::initMyStandardItemModel()
{
//QStandardItemModel类提供用于存储自定义数据的通用模型
MyStandardItemModel* model = new MyStandardItemModel(this);
const int row_count=10;
const int col_count=6;
//设置列表头
model->setColumnCount(col_count);
for(int col=0;col<col_count;col++){
model->setHeaderData(col,Qt::Horizontal, QString::number(col));
}
//设置行表头
model->setRowCount(row_count);
for(int row=0;row<row_count;row++){
model->setHeaderData(row,Qt::Vertical, QString::number(row));
}
//设置数据
for(int row=0;row<row_count;row++)
{
for(int col=0;col<col_count;col++)
{
QStandardItem *new_item=new QStandardItem(
QString("%1 %2").arg(row).arg(col));
model->setItem(row, col, new_item);
}
}
QTableView *view=ui->tableViewB;
view->setModel(model);
view->setSelectionMode(QAbstractItemView::SingleSelection);
view->setDragEnabled(true);
view->setDefaultDropAction(Qt::MoveAction);//不是必要的
view->setDragDropMode(QAbstractItemView::InternalMove);
}