| 1 |
// Copyright (C) 2012 Thorbjørn Lund Martsum - tmartsum[at]gmail.com |
| 2 |
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
| 3 |
|
| 4 |
#include <QtWidgets/QtWidgets> |
| 5 |
|
| 6 |
const int rowCount = 2000; |
| 7 |
|
| 8 |
class TableDialog : public QDialog |
| 9 |
{ |
| 10 |
Q_OBJECT |
| 11 |
public: |
| 12 |
TableDialog() : model(rowCount, 3) { create(); } |
| 13 |
void create() |
| 14 |
{ |
| 15 |
resize(1000, 233); |
| 16 |
gridLayout = new QGridLayout(this); |
| 17 |
tableView = new QTableView(this); |
| 18 |
|
| 19 |
gridLayout->addWidget(tableView, 0, 0, 2, 1); |
| 20 |
spinPrecision = new QSpinBox(this); |
| 21 |
gridLayout->addWidget(spinPrecision, 0, 1, 1, 1); |
| 22 |
verticalSpacer = new QSpacerItem(20, 20, QSizePolicy::Minimum, QSizePolicy::Expanding); |
| 23 |
gridLayout->addItem(verticalSpacer, 1, 1, 1, 1); |
| 24 |
|
| 25 |
QString ii = QString::fromLatin1("ii"); |
| 26 |
QStringList is; |
| 27 |
spinPrecision->setMinimum(-1); |
| 28 |
spinPrecision->setMaximum(rowCount + 2); |
| 29 |
for (int u = 0; u < rowCount; ++u) { |
| 30 |
if (u % 25 == 0) |
| 31 |
ii += QString::fromLatin1("i"); |
| 32 |
else |
| 33 |
ii[ii.length() - 1] = QChar::fromLatin1('a' + (u % 25)); |
| 34 |
ii[ii.length() - 2] = QChar::fromLatin1('i'); |
| 35 |
is.append(ii); |
| 36 |
} |
| 37 |
|
| 38 |
for (int u = 0; u < rowCount; ++u) { |
| 39 |
QString col1; |
| 40 |
col1 = QString::fromLatin1("Row: %1").arg(u); |
| 41 |
model.setData(model.index(u, 0), col1); |
| 42 |
model.setData(model.index(u, 1), is[u]); |
| 43 |
model.setData(model.index(u, 2), is[rowCount - u -1]); |
| 44 |
} |
| 45 |
tableView->setModel(&model); |
| 46 |
|
| 47 |
tableView->horizontalHeader()->setSectionResizeMode(0, QHeaderView::ResizeToContents); |
| 48 |
tableView->horizontalHeader()->setSectionResizeMode(1, QHeaderView::ResizeToContents); |
| 49 |
tableView->horizontalHeader()->setSectionResizeMode(2, QHeaderView::ResizeToContents); |
| 50 |
spinPrecision->setValue(tableView->horizontalHeader()->resizeContentsPrecision()); |
| 51 |
connect(spinPrecision, SIGNAL(valueChanged(int)), this, SLOT(slotValueChanged(int))); |
| 52 |
} // setupUi |
| 53 |
protected slots: |
| 54 |
void slotValueChanged(int newval); |
| 55 |
protected: |
| 56 |
QGridLayout *gridLayout; |
| 57 |
QTableView *tableView; |
| 58 |
QSpinBox *spinPrecision; |
| 59 |
QSpacerItem *verticalSpacer; |
| 60 |
QStandardItemModel model; |
| 61 |
}; |
| 62 |
|
| 63 |
void TableDialog::slotValueChanged(int newval) |
| 64 |
{ |
| 65 |
tableView->horizontalHeader()->setResizeContentsPrecision(newval); |
| 66 |
tableView->resizeColumnsToContents(); |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
int main(int argc, char *argv[]) |
| 71 |
{ |
| 72 |
QApplication app(argc, argv); |
| 73 |
TableDialog d1; |
| 74 |
d1.show(); |
| 75 |
app.exec(); |
| 76 |
} |
| 77 |
|
| 78 |
#include "testtable1.moc" |