Itemviews: Cleanup examples
Cleanup some minor issues in the chart example: - remove unused members - use initializer list for members - pass a proper role to dataChanged() - honor roles parameter in PieView::dataChanged() - use nullptr instead 0 - use new-style connect - fix indentation and other whitespaces Change-Id: Idb212b07c006fe3ae31bee9cd9b1ba4d03043b5e Reviewed-by: André Hartmann <aha_1980@gmx.de> Reviewed-by: Paul Wicking <paul.wicking@qt.io>
This commit is contained in:
parent
1c957bb8e5
commit
8c685b765b
@ -164,7 +164,7 @@ bool TableModel::setData(const QModelIndex &index, const QVariant &value, int ro
|
||||
return false;
|
||||
|
||||
contacts.replace(row, contact);
|
||||
emit(dataChanged(index, index));
|
||||
emit dataChanged(index, index, {role});
|
||||
|
||||
return true;
|
||||
}
|
||||
|
@ -48,12 +48,13 @@
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#include <QtWidgets>
|
||||
|
||||
#include "pieview.h"
|
||||
#include "mainwindow.h"
|
||||
|
||||
MainWindow::MainWindow()
|
||||
#include <QtWidgets>
|
||||
|
||||
MainWindow::MainWindow(QWidget *parent)
|
||||
: QMainWindow(parent)
|
||||
{
|
||||
QMenu *fileMenu = new QMenu(tr("&File"), this);
|
||||
QAction *openAction = fileMenu->addAction(tr("&Open..."));
|
||||
@ -124,17 +125,18 @@ void MainWindow::loadFile(const QString &fileName)
|
||||
return;
|
||||
|
||||
QTextStream stream(&file);
|
||||
QString line;
|
||||
|
||||
model->removeRows(0, model->rowCount(QModelIndex()), QModelIndex());
|
||||
|
||||
int row = 0;
|
||||
do {
|
||||
line = stream.readLine();
|
||||
while (!stream.atEnd()) {
|
||||
const QString line = stream.readLine();
|
||||
if (!line.isEmpty()) {
|
||||
model->insertRows(row, 1, QModelIndex());
|
||||
|
||||
QStringList pieces = line.split(',', QString::SkipEmptyParts);
|
||||
const QStringList pieces = line.split(',', QString::SkipEmptyParts);
|
||||
if (pieces.size() < 3)
|
||||
continue;
|
||||
model->setData(model->index(row, 0, QModelIndex()),
|
||||
pieces.value(0));
|
||||
model->setData(model->index(row, 1, QModelIndex()),
|
||||
@ -143,7 +145,7 @@ void MainWindow::loadFile(const QString &fileName)
|
||||
QColor(pieces.value(2)), Qt::DecorationRole);
|
||||
row++;
|
||||
}
|
||||
} while (!line.isEmpty());
|
||||
};
|
||||
|
||||
file.close();
|
||||
statusBar()->showMessage(tr("Loaded %1").arg(fileName), 2000);
|
||||
|
@ -56,7 +56,6 @@
|
||||
QT_BEGIN_NAMESPACE
|
||||
class QAbstractItemModel;
|
||||
class QAbstractItemView;
|
||||
class QItemSelectionModel;
|
||||
QT_END_NAMESPACE
|
||||
|
||||
class MainWindow : public QMainWindow
|
||||
@ -64,7 +63,7 @@ class MainWindow : public QMainWindow
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
MainWindow();
|
||||
MainWindow(QWidget *parent = nullptr);
|
||||
|
||||
private slots:
|
||||
void openFile();
|
||||
@ -75,9 +74,8 @@ private:
|
||||
void setupViews();
|
||||
void loadFile(const QString &path);
|
||||
|
||||
QAbstractItemModel *model;
|
||||
QAbstractItemView *pieChart;
|
||||
QItemSelectionModel *selectionModel;
|
||||
QAbstractItemModel *model = nullptr;
|
||||
QAbstractItemView *pieChart = nullptr;
|
||||
};
|
||||
|
||||
#endif // MAINWINDOW_H
|
||||
|
@ -48,30 +48,25 @@
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#include <QtWidgets>
|
||||
#include <qmath.h>
|
||||
#include <cmath>
|
||||
#include "pieview.h"
|
||||
|
||||
#include <QtWidgets>
|
||||
|
||||
PieView::PieView(QWidget *parent)
|
||||
: QAbstractItemView(parent)
|
||||
{
|
||||
horizontalScrollBar()->setRange(0, 0);
|
||||
verticalScrollBar()->setRange(0, 0);
|
||||
|
||||
margin = 8;
|
||||
totalSize = 300;
|
||||
pieSize = totalSize - 2 * margin;
|
||||
validItems = 0;
|
||||
totalValue = 0.0;
|
||||
rubberBand = 0;
|
||||
}
|
||||
|
||||
void PieView::dataChanged(const QModelIndex &topLeft,
|
||||
const QModelIndex &bottomRight,
|
||||
const QVector<int> &)
|
||||
const QVector<int> &roles)
|
||||
{
|
||||
QAbstractItemView::dataChanged(topLeft, bottomRight);
|
||||
QAbstractItemView::dataChanged(topLeft, bottomRight, roles);
|
||||
|
||||
if (!roles.contains(Qt::DisplayRole))
|
||||
return;
|
||||
|
||||
validItems = 0;
|
||||
totalValue = 0.0;
|
||||
@ -79,7 +74,7 @@ void PieView::dataChanged(const QModelIndex &topLeft,
|
||||
for (int row = 0; row < model()->rowCount(rootIndex()); ++row) {
|
||||
|
||||
QModelIndex index = model()->index(row, 1, rootIndex());
|
||||
double value = model()->data(index).toDouble();
|
||||
double value = model()->data(index, Qt::DisplayRole).toDouble();
|
||||
|
||||
if (value > 0.0) {
|
||||
totalValue += value;
|
||||
@ -197,15 +192,14 @@ QRect PieView::itemRect(const QModelIndex &index) const
|
||||
listItem++;
|
||||
}
|
||||
|
||||
double itemHeight;
|
||||
|
||||
switch (index.column()) {
|
||||
case 0:
|
||||
itemHeight = QFontMetrics(viewOptions().font).height();
|
||||
case 0: {
|
||||
const qreal itemHeight = QFontMetricsF(viewOptions().font).height();
|
||||
|
||||
return QRect(totalSize,
|
||||
int(margin + listItem*itemHeight),
|
||||
totalSize - margin, int(itemHeight));
|
||||
qRound(margin + listItem * itemHeight),
|
||||
totalSize - margin, qRound(itemHeight));
|
||||
}
|
||||
case 1:
|
||||
return viewport()->rect();
|
||||
}
|
||||
@ -235,7 +229,7 @@ QRegion PieView::itemRegion(const QModelIndex &index) const
|
||||
if (sliceIndex == index) {
|
||||
QPainterPath slicePath;
|
||||
slicePath.moveTo(totalSize / 2, totalSize / 2);
|
||||
slicePath.arcTo(margin, margin, margin+pieSize, margin+pieSize,
|
||||
slicePath.arcTo(margin, margin, margin + pieSize, margin + pieSize,
|
||||
startAngle, angle);
|
||||
slicePath.closeSubpath();
|
||||
|
||||
@ -342,7 +336,7 @@ void PieView::paintEvent(QPaintEvent *event)
|
||||
double value = model()->data(index).toDouble();
|
||||
|
||||
if (value > 0.0) {
|
||||
double angle = 360*value/totalValue;
|
||||
double angle = 360 * value / totalValue;
|
||||
|
||||
QModelIndex colorIndex = model()->index(row, 0, rootIndex());
|
||||
QColor color = QColor(model()->data(colorIndex, Qt::DecorationRole).toString());
|
||||
@ -480,16 +474,16 @@ void PieView::setSelection(const QRect &rect, QItemSelectionModel::SelectionFlag
|
||||
}
|
||||
|
||||
if (indexes.size() > 0) {
|
||||
int firstRow = indexes[0].row();
|
||||
int lastRow = indexes[0].row();
|
||||
int firstColumn = indexes[0].column();
|
||||
int lastColumn = indexes[0].column();
|
||||
int firstRow = indexes.at(0).row();
|
||||
int lastRow = firstRow;
|
||||
int firstColumn = indexes.at(0).column();
|
||||
int lastColumn = firstColumn;
|
||||
|
||||
for (int i = 1; i < indexes.size(); ++i) {
|
||||
firstRow = qMin(firstRow, indexes[i].row());
|
||||
lastRow = qMax(lastRow, indexes[i].row());
|
||||
firstColumn = qMin(firstColumn, indexes[i].column());
|
||||
lastColumn = qMax(lastColumn, indexes[i].column());
|
||||
firstRow = qMin(firstRow, indexes.at(i).row());
|
||||
lastRow = qMax(lastRow, indexes.at(i).row());
|
||||
firstColumn = qMin(firstColumn, indexes.at(i).column());
|
||||
lastColumn = qMax(lastColumn, indexes.at(i).column());
|
||||
}
|
||||
|
||||
QItemSelection selection(
|
||||
@ -508,7 +502,7 @@ void PieView::setSelection(const QRect &rect, QItemSelectionModel::SelectionFlag
|
||||
void PieView::updateGeometries()
|
||||
{
|
||||
horizontalScrollBar()->setPageStep(viewport()->width());
|
||||
horizontalScrollBar()->setRange(0, qMax(0, 2*totalSize - viewport()->width()));
|
||||
horizontalScrollBar()->setRange(0, qMax(0, 2 * totalSize - viewport()->width()));
|
||||
verticalScrollBar()->setPageStep(viewport()->height());
|
||||
verticalScrollBar()->setRange(0, qMax(0, totalSize - viewport()->height()));
|
||||
}
|
||||
@ -546,7 +540,7 @@ QRegion PieView::visualRegionForSelection(const QItemSelection &selection) const
|
||||
|
||||
QRegion region;
|
||||
for (int i = 0; i < ranges; ++i) {
|
||||
QItemSelectionRange range = selection.at(i);
|
||||
const QItemSelectionRange &range = selection.at(i);
|
||||
for (int row = range.top(); row <= range.bottom(); ++row) {
|
||||
for (int col = range.left(); col <= range.right(); ++col) {
|
||||
QModelIndex index = model()->index(row, col, rootIndex());
|
||||
|
@ -59,7 +59,7 @@ class PieView : public QAbstractItemView
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
PieView(QWidget *parent = 0);
|
||||
PieView(QWidget *parent = nullptr);
|
||||
|
||||
QRect visualRect(const QModelIndex &index) const override;
|
||||
void scrollTo(const QModelIndex &index, ScrollHint hint = EnsureVisible) override;
|
||||
@ -100,13 +100,13 @@ private:
|
||||
int rows(const QModelIndex &index = QModelIndex()) const;
|
||||
void updateGeometries() override;
|
||||
|
||||
int margin;
|
||||
int totalSize;
|
||||
int pieSize;
|
||||
int validItems;
|
||||
double totalValue;
|
||||
int margin = 0;
|
||||
int totalSize = 300;
|
||||
int pieSize = totalSize - 2 * margin;
|
||||
int validItems = 0;
|
||||
double totalValue = 0.0;
|
||||
QRubberBand *rubberBand = nullptr;
|
||||
QPoint origin;
|
||||
QRubberBand *rubberBand;
|
||||
};
|
||||
//! [0]
|
||||
|
||||
|
@ -225,7 +225,7 @@ bool TreeModel::setData(const QModelIndex &index, const QVariant &value, int rol
|
||||
bool result = item->setData(index.column(), value);
|
||||
|
||||
if (result)
|
||||
emit dataChanged(index, index);
|
||||
emit dataChanged(index, index, {role});
|
||||
|
||||
return result;
|
||||
}
|
||||
|
@ -50,16 +50,16 @@
|
||||
|
||||
//! [Quoting ModelView Tutorial]
|
||||
// main.cpp
|
||||
#include <QtWidgets/QApplication>
|
||||
#include <QtWidgets/QTableView>
|
||||
#include <QApplication>
|
||||
#include <QTableView>
|
||||
#include "mymodel.h"
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
QApplication a(argc, argv);
|
||||
QTableView tableView;
|
||||
MyModel myModel(0);
|
||||
tableView.setModel( &myModel );
|
||||
MyModel myModel;
|
||||
tableView.setModel(&myModel);
|
||||
tableView.show();
|
||||
return a.exec();
|
||||
}
|
||||
|
@ -53,7 +53,7 @@
|
||||
#include "mymodel.h"
|
||||
|
||||
MyModel::MyModel(QObject *parent)
|
||||
:QAbstractTableModel(parent)
|
||||
: QAbstractTableModel(parent)
|
||||
{
|
||||
}
|
||||
|
||||
@ -70,11 +70,10 @@ int MyModel::columnCount(const QModelIndex & /*parent*/) const
|
||||
QVariant MyModel::data(const QModelIndex &index, int role) const
|
||||
{
|
||||
if (role == Qt::DisplayRole)
|
||||
{
|
||||
return QString("Row%1, Column%2")
|
||||
.arg(index.row() + 1)
|
||||
.arg(index.column() +1);
|
||||
}
|
||||
|
||||
return QVariant();
|
||||
}
|
||||
//! [Quoting ModelView Tutorial]
|
||||
|
@ -59,8 +59,8 @@ class MyModel : public QAbstractTableModel
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
MyModel(QObject *parent);
|
||||
int rowCount(const QModelIndex &parent = QModelIndex()) const override ;
|
||||
MyModel(QObject *parent = nullptr);
|
||||
int rowCount(const QModelIndex &parent = QModelIndex()) const override;
|
||||
int columnCount(const QModelIndex &parent = QModelIndex()) const override;
|
||||
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
|
||||
};
|
||||
|
@ -50,16 +50,16 @@
|
||||
|
||||
//! [Quoting ModelView Tutorial]
|
||||
// main.cpp
|
||||
#include <QtWidgets/QApplication>
|
||||
#include <QtWidgets/QTableView>
|
||||
#include <QApplication>
|
||||
#include <QTableView>
|
||||
#include "mymodel.h"
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
QApplication a(argc, argv);
|
||||
QTableView tableView;
|
||||
MyModel myModel(0);
|
||||
tableView.setModel( &myModel );
|
||||
MyModel myModel;
|
||||
tableView.setModel(&myModel);
|
||||
tableView.show();
|
||||
return a.exec();
|
||||
}
|
||||
|
@ -48,13 +48,14 @@
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#include "mymodel.h"
|
||||
|
||||
#include <QFont>
|
||||
#include <QBrush>
|
||||
#include "mymodel.h"
|
||||
#include <QDebug>
|
||||
|
||||
MyModel::MyModel(QObject *parent)
|
||||
:QAbstractTableModel(parent)
|
||||
: QAbstractTableModel(parent)
|
||||
{
|
||||
}
|
||||
|
||||
@ -78,7 +79,7 @@ QVariant MyModel::data(const QModelIndex &index, int role) const
|
||||
qDebug() << QString("row %1, col%2, role %3")
|
||||
.arg(row).arg(col).arg(role);
|
||||
|
||||
switch(role){
|
||||
switch (role) {
|
||||
case Qt::DisplayRole:
|
||||
if (row == 0 && col == 1) return QString("<--left");
|
||||
if (row == 1 && col == 1) return QString("right-->");
|
||||
@ -86,36 +87,25 @@ QVariant MyModel::data(const QModelIndex &index, int role) const
|
||||
return QString("Row%1, Column%2")
|
||||
.arg(row + 1)
|
||||
.arg(col +1);
|
||||
break;
|
||||
case Qt::FontRole:
|
||||
if (row == 0 && col == 0) //change font only for cell(0,0)
|
||||
{
|
||||
if (row == 0 && col == 0) { //change font only for cell(0,0)
|
||||
QFont boldFont;
|
||||
boldFont.setBold(true);
|
||||
return boldFont;
|
||||
}
|
||||
break;
|
||||
case Qt::BackgroundRole:
|
||||
|
||||
if (row == 1 && col == 2) //change background only for cell(1,2)
|
||||
{
|
||||
QBrush redBackground(Qt::red);
|
||||
return redBackground;
|
||||
}
|
||||
return QBrush(Qt::red);
|
||||
break;
|
||||
case Qt::TextAlignmentRole:
|
||||
|
||||
if (row == 1 && col == 1) //change text alignment only for cell(1,1)
|
||||
{
|
||||
return Qt::AlignRight + Qt::AlignVCenter;
|
||||
}
|
||||
break;
|
||||
case Qt::CheckStateRole:
|
||||
|
||||
if (row == 1 && col == 0) //add a checkbox to cell(1,0)
|
||||
{
|
||||
return Qt::Checked;
|
||||
}
|
||||
break;
|
||||
}
|
||||
return QVariant();
|
||||
}
|
||||
|
@ -57,8 +57,8 @@ class MyModel : public QAbstractTableModel
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
MyModel(QObject *parent);
|
||||
int rowCount(const QModelIndex &parent = QModelIndex()) const override ;
|
||||
MyModel(QObject *parent = nullptr);
|
||||
int rowCount(const QModelIndex &parent = QModelIndex()) const override;
|
||||
int columnCount(const QModelIndex &parent = QModelIndex()) const override;
|
||||
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
|
||||
};
|
||||
|
@ -48,8 +48,8 @@
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#include <QtWidgets/QApplication>
|
||||
#include <QtWidgets/QTableView>
|
||||
#include <QApplication>
|
||||
#include <QTableView>
|
||||
#include "mymodel.h"
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
@ -57,7 +57,7 @@ int main(int argc, char *argv[])
|
||||
QApplication a(argc, argv);
|
||||
QTableView tableView;
|
||||
MyModel myModel(0);
|
||||
tableView.setModel( &myModel );
|
||||
tableView.setModel(&myModel);
|
||||
tableView.show();
|
||||
return a.exec();
|
||||
}
|
||||
|
@ -48,18 +48,17 @@
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#include <QBrush>
|
||||
#include <QTime>
|
||||
#include "mymodel.h"
|
||||
|
||||
#include <QTime>
|
||||
|
||||
//! [quoting mymodel_a]
|
||||
MyModel::MyModel(QObject *parent)
|
||||
:QAbstractTableModel(parent)
|
||||
: QAbstractTableModel(parent)
|
||||
, timer(new QTimer(this))
|
||||
{
|
||||
// selectedCell = 0;
|
||||
timer = new QTimer(this);
|
||||
timer->setInterval(1000);
|
||||
connect(timer, SIGNAL(timeout()) , this, SLOT(timerHit()));
|
||||
connect(timer, &QTimer::timeout , this, &MyModel::timerHit);
|
||||
timer->start();
|
||||
}
|
||||
//! [quoting mymodel_a]
|
||||
@ -82,13 +81,9 @@ QVariant MyModel::data(const QModelIndex &index, int role) const
|
||||
int row = index.row();
|
||||
int col = index.column();
|
||||
|
||||
if (role == Qt::DisplayRole)
|
||||
{
|
||||
if (row == 0 && col == 0)
|
||||
{
|
||||
return QTime::currentTime().toString();
|
||||
}
|
||||
}
|
||||
if (role == Qt::DisplayRole && row == 0 && col == 0)
|
||||
return QTime::currentTime().toString();
|
||||
|
||||
return QVariant();
|
||||
}
|
||||
//! [quoting mymodel_QVariant ]
|
||||
@ -99,6 +94,6 @@ void MyModel::timerHit()
|
||||
//we identify the top left cell
|
||||
QModelIndex topLeft = createIndex(0,0);
|
||||
//emit a signal to make the view reread identified data
|
||||
emit dataChanged(topLeft, topLeft);
|
||||
emit dataChanged(topLeft, topLeft, {Qt::DisplayRole});
|
||||
}
|
||||
//! [quoting mymodel_b ]
|
||||
|
@ -58,13 +58,12 @@ class MyModel : public QAbstractTableModel
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
MyModel(QObject *parent);
|
||||
int rowCount(const QModelIndex &parent = QModelIndex()) const override ;
|
||||
MyModel(QObject *parent = nullptr);
|
||||
int rowCount(const QModelIndex &parent = QModelIndex()) const override;
|
||||
int columnCount(const QModelIndex &parent = QModelIndex()) const override;
|
||||
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
|
||||
QTimer *timer;
|
||||
private:
|
||||
int selectedCell;
|
||||
QTimer *timer;
|
||||
private slots:
|
||||
void timerHit();
|
||||
};
|
||||
|
@ -48,16 +48,16 @@
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#include <QtWidgets/QApplication>
|
||||
#include <QtWidgets/QTableView>
|
||||
#include <QApplication>
|
||||
#include <QTableView>
|
||||
#include "mymodel.h"
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
QApplication a(argc, argv);
|
||||
QTableView tableView;
|
||||
MyModel myModel(0);
|
||||
tableView.setModel( &myModel );
|
||||
MyModel myModel;
|
||||
tableView.setModel(&myModel);
|
||||
tableView.show();
|
||||
return a.exec();
|
||||
}
|
||||
}
|
||||
|
@ -51,7 +51,7 @@
|
||||
#include "mymodel.h"
|
||||
|
||||
MyModel::MyModel(QObject *parent)
|
||||
:QAbstractTableModel(parent)
|
||||
: QAbstractTableModel(parent)
|
||||
{
|
||||
}
|
||||
|
||||
@ -70,8 +70,7 @@ int MyModel::columnCount(const QModelIndex & /*parent*/) const
|
||||
//-------------------------------------------------------
|
||||
QVariant MyModel::data(const QModelIndex &index, int role) const
|
||||
{
|
||||
if (role == Qt::DisplayRole)
|
||||
{
|
||||
if (role == Qt::DisplayRole) {
|
||||
return QString("Row%1, Column%2")
|
||||
.arg(index.row() + 1)
|
||||
.arg(index.column() +1);
|
||||
@ -82,18 +81,14 @@ QVariant MyModel::data(const QModelIndex &index, int role) const
|
||||
//! [quoting mymodel_c]
|
||||
QVariant MyModel::headerData(int section, Qt::Orientation orientation, int role) const
|
||||
{
|
||||
if (role == Qt::DisplayRole)
|
||||
{
|
||||
if (orientation == Qt::Horizontal) {
|
||||
switch (section)
|
||||
{
|
||||
case 0:
|
||||
return QString("first");
|
||||
case 1:
|
||||
return QString("second");
|
||||
case 2:
|
||||
return QString("third");
|
||||
}
|
||||
if (role == Qt::DisplayRole && orientation == Qt::Horizontal) {
|
||||
switch (section) {
|
||||
case 0:
|
||||
return QString("first");
|
||||
case 1:
|
||||
return QString("second");
|
||||
case 2:
|
||||
return QString("third");
|
||||
}
|
||||
}
|
||||
return QVariant();
|
||||
|
@ -57,8 +57,8 @@ class MyModel : public QAbstractTableModel
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
MyModel(QObject *parent);
|
||||
int rowCount(const QModelIndex &parent = QModelIndex()) const override ;
|
||||
MyModel(QObject *parent = nullptr);
|
||||
int rowCount(const QModelIndex &parent = QModelIndex()) const override;
|
||||
int columnCount(const QModelIndex &parent = QModelIndex()) const override;
|
||||
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
|
||||
QVariant headerData(int section, Qt::Orientation orientation, int role) const override;
|
||||
|
@ -48,7 +48,7 @@
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#include <QtWidgets/QApplication>
|
||||
#include <QApplication>
|
||||
#include "mainwindow.h"
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
|
@ -48,23 +48,25 @@
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#include <QTableView>
|
||||
#include "mainwindow.h"
|
||||
#include "mymodel.h"
|
||||
|
||||
#include <QTableView>
|
||||
|
||||
MainWindow::MainWindow(QWidget *parent)
|
||||
: QMainWindow(parent)
|
||||
, tableView(new QTableView(this))
|
||||
{
|
||||
tableView = new QTableView(this);
|
||||
setCentralWidget(tableView);
|
||||
QAbstractTableModel *myModel = new MyModel(this);
|
||||
MyModel *myModel = new MyModel(this);
|
||||
tableView->setModel(myModel);
|
||||
|
||||
//transfer changes to the model to the window title
|
||||
connect(myModel, SIGNAL(editCompleted(const QString &)), this, SLOT(setWindowTitle(const QString &)));
|
||||
connect(myModel, &MyModel::editCompleted,
|
||||
this, &MainWindow::showWindowTitle);
|
||||
}
|
||||
|
||||
void MainWindow::showWindowTitle(const QString & title)
|
||||
void MainWindow::showWindowTitle(const QString &title)
|
||||
{
|
||||
setWindowTitle(title);
|
||||
setWindowTitle(title);
|
||||
}
|
||||
|
@ -51,9 +51,9 @@
|
||||
#ifndef MAINWINDOW_H
|
||||
#define MAINWINDOW_H
|
||||
|
||||
#include <QtWidgets/QMainWindow>
|
||||
#include <QMainWindow>
|
||||
|
||||
QT_BEGIN_NAMESPACE // QT_BEGIN_NAMESPACE / QT_END_NAMESPACE are not needed in Qt user code
|
||||
QT_BEGIN_NAMESPACE
|
||||
class QTableView; //forward declaration
|
||||
QT_END_NAMESPACE
|
||||
|
||||
@ -64,9 +64,9 @@ class MainWindow : public QMainWindow
|
||||
private:
|
||||
QTableView *tableView;
|
||||
public:
|
||||
MainWindow(QWidget *parent = 0);
|
||||
MainWindow(QWidget *parent = nullptr);
|
||||
public slots:
|
||||
void showWindowTitle(const QString & title);
|
||||
void showWindowTitle(const QString &title);
|
||||
};
|
||||
|
||||
#endif // MAINWINDOW_H
|
||||
|
@ -48,12 +48,10 @@
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
|
||||
#include "mymodel.h"
|
||||
|
||||
|
||||
MyModel::MyModel(QObject *parent)
|
||||
:QAbstractTableModel(parent)
|
||||
: QAbstractTableModel(parent)
|
||||
{
|
||||
}
|
||||
|
||||
@ -72,33 +70,31 @@ int MyModel::columnCount(const QModelIndex & /*parent*/) const
|
||||
//-----------------------------------------------------------------
|
||||
QVariant MyModel::data(const QModelIndex &index, int role) const
|
||||
{
|
||||
if (role == Qt::DisplayRole)
|
||||
{
|
||||
return m_gridData[index.row()][index.column()];
|
||||
}
|
||||
if (role == Qt::DisplayRole && checkIndex(index))
|
||||
return m_gridData[index.row()][index.column()];
|
||||
|
||||
return QVariant();
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------
|
||||
//! [quoting mymodel_e]
|
||||
bool MyModel::setData(const QModelIndex & index, const QVariant & value, int role)
|
||||
bool MyModel::setData(const QModelIndex &index, const QVariant &value, int role)
|
||||
{
|
||||
if (role == Qt::EditRole)
|
||||
{
|
||||
if (role == Qt::EditRole) {
|
||||
if (!checkIndex(index))
|
||||
return false;
|
||||
//save value from editor to member m_gridData
|
||||
m_gridData[index.row()][index.column()] = value.toString();
|
||||
//for presentation purposes only: build and emit a joined string
|
||||
QString result;
|
||||
for (int row= 0; row < ROWS; row++)
|
||||
{
|
||||
for(int col= 0; col < COLS; col++)
|
||||
{
|
||||
for (int row = 0; row < ROWS; row++) {
|
||||
for (int col= 0; col < COLS; col++)
|
||||
result += m_gridData[row][col] + ' ';
|
||||
}
|
||||
}
|
||||
emit editCompleted( result );
|
||||
emit editCompleted(result);
|
||||
return true;
|
||||
}
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
//! [quoting mymodel_e]
|
||||
|
||||
|
@ -64,12 +64,12 @@ class MyModel : public QAbstractTableModel
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
MyModel(QObject *parent);
|
||||
int rowCount(const QModelIndex &parent = QModelIndex()) const override ;
|
||||
MyModel(QObject *parent = nullptr);
|
||||
int rowCount(const QModelIndex &parent = QModelIndex()) const override;
|
||||
int columnCount(const QModelIndex &parent = QModelIndex()) const override;
|
||||
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
|
||||
bool setData(const QModelIndex & index, const QVariant & value, int role = Qt::EditRole) override;
|
||||
Qt::ItemFlags flags(const QModelIndex & index) const override ;
|
||||
bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override;
|
||||
Qt::ItemFlags flags(const QModelIndex &index) const override;
|
||||
private:
|
||||
QString m_gridData[ROWS][COLS]; //holds text entered into QTableView
|
||||
signals:
|
||||
|
@ -48,7 +48,7 @@
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#include <QtWidgets/QApplication>
|
||||
#include <QApplication>
|
||||
#include "mainwindow.h"
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
|
@ -50,24 +50,25 @@
|
||||
|
||||
//! [Quoting ModelView Tutorial]
|
||||
// modelview.cpp
|
||||
#include "mainwindow.h"
|
||||
|
||||
#include <QTreeView>
|
||||
#include <QStandardItemModel>
|
||||
#include <QStandardItem>
|
||||
#include "mainwindow.h"
|
||||
|
||||
MainWindow::MainWindow(QWidget *parent)
|
||||
: QMainWindow(parent)
|
||||
, treeView(new QTreeView(this))
|
||||
, standardModel(new QStandardItemModel(this))
|
||||
{
|
||||
treeView = new QTreeView(this);
|
||||
setCentralWidget(treeView);
|
||||
standardModel = new QStandardItemModel ;
|
||||
|
||||
QList<QStandardItem *> preparedRow =prepareRow("first", "second", "third");
|
||||
QList<QStandardItem *> preparedRow = prepareRow("first", "second", "third");
|
||||
QStandardItem *item = standardModel->invisibleRootItem();
|
||||
// adding a row to the invisible root item produces a root element
|
||||
item->appendRow(preparedRow);
|
||||
|
||||
QList<QStandardItem *> secondRow =prepareRow("111", "222", "333");
|
||||
QList<QStandardItem *> secondRow = prepareRow("111", "222", "333");
|
||||
// adding a row to an item starts a subtree
|
||||
preparedRow.first()->appendRow(secondRow);
|
||||
|
||||
@ -76,13 +77,11 @@ MainWindow::MainWindow(QWidget *parent)
|
||||
}
|
||||
|
||||
QList<QStandardItem *> MainWindow::prepareRow(const QString &first,
|
||||
const QString &second,
|
||||
const QString &third)
|
||||
const QString &second,
|
||||
const QString &third) const
|
||||
{
|
||||
QList<QStandardItem *> rowItems;
|
||||
rowItems << new QStandardItem(first);
|
||||
rowItems << new QStandardItem(second);
|
||||
rowItems << new QStandardItem(third);
|
||||
return rowItems;
|
||||
return {new QStandardItem(first),
|
||||
new QStandardItem(second),
|
||||
new QStandardItem(third)};
|
||||
}
|
||||
//! [Quoting ModelView Tutorial]
|
||||
|
@ -51,9 +51,9 @@
|
||||
#ifndef MAINWINDOW_H
|
||||
#define MAINWINDOW_H
|
||||
|
||||
#include <QtWidgets/QMainWindow>
|
||||
#include <QMainWindow>
|
||||
|
||||
QT_BEGIN_NAMESPACE // QT_BEGIN_NAMESPACE / QT_END_NAMESPACE are not needed in Qt user code
|
||||
QT_BEGIN_NAMESPACE
|
||||
class QTreeView; //forward declarations
|
||||
class QStandardItemModel;
|
||||
class QStandardItem;
|
||||
@ -66,11 +66,11 @@ class MainWindow : public QMainWindow
|
||||
private:
|
||||
QTreeView *treeView;
|
||||
QStandardItemModel *standardModel;
|
||||
QList<QStandardItem *> prepareRow( const QString &first,
|
||||
const QString &second,
|
||||
const QString &third );
|
||||
QList<QStandardItem *> prepareRow(const QString &first,
|
||||
const QString &second,
|
||||
const QString &third) const;
|
||||
public:
|
||||
MainWindow(QWidget *parent = 0);
|
||||
MainWindow(QWidget *parent = nullptr);
|
||||
};
|
||||
|
||||
#endif // MAINWINDOW_H
|
||||
|
@ -48,7 +48,7 @@
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#include <QtWidgets/QApplication>
|
||||
#include <QApplication>
|
||||
#include "mainwindow.h"
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
|
@ -49,17 +49,18 @@
|
||||
****************************************************************************/
|
||||
|
||||
//! [quoting modelview_a]
|
||||
#include "mainwindow.h"
|
||||
|
||||
#include <QTreeView>
|
||||
#include <QStandardItemModel>
|
||||
#include <QItemSelectionModel>
|
||||
#include "mainwindow.h"
|
||||
|
||||
MainWindow::MainWindow(QWidget *parent)
|
||||
: QMainWindow(parent)
|
||||
, treeView(new QTreeView(this))
|
||||
, standardModel(new QStandardItemModel(this))
|
||||
{
|
||||
treeView = new QTreeView(this);
|
||||
setCentralWidget(treeView);
|
||||
standardModel = new QStandardItemModel ;
|
||||
QStandardItem *rootNode = standardModel->invisibleRootItem();
|
||||
|
||||
|
||||
@ -88,9 +89,9 @@ MainWindow::MainWindow(QWidget *parent)
|
||||
treeView->expandAll();
|
||||
|
||||
//selection changes shall trigger a slot
|
||||
QItemSelectionModel *selectionModel= treeView->selectionModel();
|
||||
connect(selectionModel, SIGNAL(selectionChanged (const QItemSelection &, const QItemSelection &)),
|
||||
this, SLOT(selectionChangedSlot(const QItemSelection &, const QItemSelection &)));
|
||||
QItemSelectionModel *selectionModel = treeView->selectionModel();
|
||||
connect(selectionModel, &QItemSelectionModel::selectionChanged,
|
||||
this, &MainWindow::selectionChangedSlot);
|
||||
}
|
||||
//! [quoting modelview_a]
|
||||
|
||||
@ -103,10 +104,9 @@ void MainWindow::selectionChangedSlot(const QItemSelection & /*newSelection*/, c
|
||||
const QModelIndex index = treeView->selectionModel()->currentIndex();
|
||||
QString selectedText = index.data(Qt::DisplayRole).toString();
|
||||
//find out the hierarchy level of the selected item
|
||||
int hierarchyLevel=1;
|
||||
int hierarchyLevel = 1;
|
||||
QModelIndex seekRoot = index;
|
||||
while(seekRoot.parent() != QModelIndex())
|
||||
{
|
||||
while (seekRoot.parent() != QModelIndex()) {
|
||||
seekRoot = seekRoot.parent();
|
||||
hierarchyLevel++;
|
||||
}
|
||||
|
@ -51,9 +51,9 @@
|
||||
#ifndef MAINWINDOW_H
|
||||
#define MAINWINDOW_H
|
||||
|
||||
#include <QtWidgets/QMainWindow>
|
||||
#include <QMainWindow>
|
||||
|
||||
QT_BEGIN_NAMESPACE // QT_BEGIN_NAMESPACE / QT_END_NAMESPACE are not needed in Qt user code
|
||||
QT_BEGIN_NAMESPACE
|
||||
class QTreeView; //forward declarations
|
||||
class QStandardItemModel;
|
||||
class QItemSelection;
|
||||
@ -67,7 +67,7 @@ private:
|
||||
QTreeView *treeView;
|
||||
QStandardItemModel *standardModel;
|
||||
private slots:
|
||||
void selectionChangedSlot(const QItemSelection & newSelection, const QItemSelection & oldSelection);
|
||||
void selectionChangedSlot(const QItemSelection &newSelection, const QItemSelection &oldSelection);
|
||||
public:
|
||||
MainWindow(QWidget *parent = 0);
|
||||
};
|
||||
|
Loading…
x
Reference in New Issue
Block a user