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