-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainWindow.cpp
84 lines (66 loc) · 2.35 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
74
75
76
77
78
79
80
81
82
83
84
#include "MainWindow.h"
#include <MyTableItem.h>
#include <MyTableModel.h>
#include <QStandardItem>
#include <QTime>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent)
{
setupUi(this);
mSortModel.setSourceModel(&mTableModel);
tableView->setSortingEnabled(true);
on_comboBox_currentIndexChanged(comboBox->currentIndex());
}
void MainWindow::createStandardItems(int count)
{
mStandardModel.setRowCount(count);
mStandardModel.setColumnCount(2);
tableView->setUpdatesEnabled(false);
QStringList listOne = QStringLiteral("A B C D E F G H I J K L M N O P Q R S T U V W X Y Z")
.split(QLatin1Char(' '));
for ( int row = 0; row < count; row++ ) {
QList<QStandardItem*> columns;
columns << new QStandardItem( QString::fromUtf8("#%1").arg(row, 4, 10, QLatin1Char('0')) )
<< new QStandardItem( listOne[row % listOne.size()] );
for ( int col = 0; col < columns.size(); col++) {
mStandardModel.setItem( row, col, columns[col] );
}
}
tableView->setUpdatesEnabled(true);
tableView->update();
}
void MainWindow::createMyItems(int count)
{
QStringList listOne = QStringLiteral("A B C D E F G H I J K L M N O P Q R S T U V W X Y Z")
.split(QLatin1Char(' '));
QVector<MyTableItem> rows;
for ( int row = 0; row < count; row++ ) {
MyTableItem newRow;
newRow.setData( 0, QStringLiteral("#%1").arg(row, 6, 10, QLatin1Char('0')) );
newRow.setData( 1, listOne[row % listOne.size()] );
rows << newRow;
}
mTableModel.initModel( rows );
}
void MainWindow::on_btnCreateItems_clicked()
{
QAbstractItemModel* model = tableView->model();
const int count = spinBox->value();
QTime stopWatch;
stopWatch.start();
qDebug("Creating %d items ...", count);
if (model == &mStandardModel) { createStandardItems(count); }
else if (model == &mSortModel) { createMyItems(count); }
qDebug("Created %d items in %d ms", model->rowCount(), stopWatch.elapsed());
}
void MainWindow::on_comboBox_currentIndexChanged(int index)
{
Q_ASSERT(index < comboBox->count());
QAbstractItemModel* model = nullptr;
switch (index) {
case 0: model = &mStandardModel; break;
case 1: model = &mSortModel; break;
default: break;
}
tableView->setModel( model );
}