QtBase: examples/widgets/itemviews code style

Change-Id: I78a7745f7dc3add3fd7780220118d1b35b50a941
Reviewed-by: hjk <qthjk@ovi.com>
This commit is contained in:
David Schulz 2012-11-22 13:40:51 +01:00 committed by The Qt Project
parent 49a4d058cc
commit ab0a007cb3
66 changed files with 355 additions and 385 deletions

View File

@ -38,10 +38,12 @@
**
****************************************************************************/
#include <QtWidgets>
#include "window.h"
#include <QApplication>
#include <QStandardItemModel>
#include <QTime>
void addMail(QAbstractItemModel *model, const QString &subject,
const QString &sender, const QDateTime &date)
{

View File

@ -85,4 +85,4 @@ private:
QComboBox *filterColumnComboBox;
};
#endif
#endif // WINDOW_H

View File

@ -103,42 +103,41 @@ void MainWindow::openFile(const QString &path)
{
QString fileName;
if (path.isNull())
fileName = QFileDialog::getOpenFileName(this, tr("Choose a data file"),
"", "*.cht");
fileName = QFileDialog::getOpenFileName(this, tr("Choose a data file"), "", "*.cht");
else
fileName = path;
if (!fileName.isEmpty()) {
QFile file(fileName);
if (fileName.isEmpty())
return;
if (file.open(QFile::ReadOnly | QFile::Text)) {
QTextStream stream(&file);
QString line;
QFile file(fileName);
if (!file.open(QFile::ReadOnly | QFile::Text))
return;
model->removeRows(0, model->rowCount(QModelIndex()), QModelIndex());
QTextStream stream(&file);
QString line;
int row = 0;
do {
line = stream.readLine();
if (!line.isEmpty()) {
model->removeRows(0, model->rowCount(QModelIndex()), QModelIndex());
model->insertRows(row, 1, QModelIndex());
int row = 0;
do {
line = stream.readLine();
if (!line.isEmpty()) {
model->insertRows(row, 1, QModelIndex());
QStringList pieces = line.split(",", QString::SkipEmptyParts);
model->setData(model->index(row, 0, QModelIndex()),
pieces.value(0));
model->setData(model->index(row, 1, QModelIndex()),
pieces.value(1));
model->setData(model->index(row, 0, QModelIndex()),
QColor(pieces.value(2)), Qt::DecorationRole);
row++;
}
} while (!line.isEmpty());
file.close();
statusBar()->showMessage(tr("Loaded %1").arg(fileName), 2000);
QStringList pieces = line.split(",", QString::SkipEmptyParts);
model->setData(model->index(row, 0, QModelIndex()),
pieces.value(0));
model->setData(model->index(row, 1, QModelIndex()),
pieces.value(1));
model->setData(model->index(row, 0, QModelIndex()),
QColor(pieces.value(2)), Qt::DecorationRole);
row++;
}
}
} while (!line.isEmpty());
file.close();
statusBar()->showMessage(tr("Loaded %1").arg(fileName), 2000);
}
void MainWindow::saveFile()
@ -146,27 +145,28 @@ void MainWindow::saveFile()
QString fileName = QFileDialog::getSaveFileName(this,
tr("Save file as"), "", "*.cht");
if (!fileName.isEmpty()) {
QFile file(fileName);
QTextStream stream(&file);
if (fileName.isEmpty())
return;
if (file.open(QFile::WriteOnly | QFile::Text)) {
for (int row = 0; row < model->rowCount(QModelIndex()); ++row) {
QFile file(fileName);
if (!file.open(QFile::WriteOnly | QFile::Text))
return;
QStringList pieces;
QTextStream stream(&file);
for (int row = 0; row < model->rowCount(QModelIndex()); ++row) {
pieces.append(model->data(model->index(row, 0, QModelIndex()),
Qt::DisplayRole).toString());
pieces.append(model->data(model->index(row, 1, QModelIndex()),
Qt::DisplayRole).toString());
pieces.append(model->data(model->index(row, 0, QModelIndex()),
Qt::DecorationRole).toString());
QStringList pieces;
stream << pieces.join(',') << "\n";
}
}
pieces.append(model->data(model->index(row, 0, QModelIndex()),
Qt::DisplayRole).toString());
pieces.append(model->data(model->index(row, 1, QModelIndex()),
Qt::DisplayRole).toString());
pieces.append(model->data(model->index(row, 0, QModelIndex()),
Qt::DecorationRole).toString());
file.close();
statusBar()->showMessage(tr("Saved %1").arg(fileName), 2000);
stream << pieces.join(',') << "\n";
}
file.close();
statusBar()->showMessage(tr("Saved %1").arg(fileName), 2000);
}

View File

@ -69,4 +69,4 @@ private:
QItemSelectionModel *selectionModel;
};
#endif
#endif // MAINWINDOW_H

View File

@ -55,7 +55,7 @@ PieView::PieView(QWidget *parent)
margin = 8;
totalSize = 300;
pieSize = totalSize - 2*margin;
pieSize = totalSize - 2 * margin;
validItems = 0;
totalValue = 0.0;
rubberBand = 0;
@ -105,17 +105,17 @@ QModelIndex PieView::indexAt(const QPoint &point) const
int wy = point.y() + verticalScrollBar()->value();
if (wx < totalSize) {
double cx = wx - totalSize/2;
double cy = totalSize/2 - wy; // positive cy for items above the center
double cx = wx - totalSize / 2;
double cy = totalSize / 2 - wy; // positive cy for items above the center
// Determine the distance from the center point of the pie chart.
double d = pow(pow(cx, 2) + pow(cy, 2), 0.5);
if (d == 0 || d > pieSize/2)
if (d == 0 || d > pieSize / 2)
return QModelIndex();
// Determine the angle of the point.
double angle = (180 / M_PI) * acos(cx/d);
double angle = (180 / M_PI) * acos(cx / d);
if (cy < 0)
angle = 360 - angle;
@ -128,7 +128,7 @@ QModelIndex PieView::indexAt(const QPoint &point) const
double value = model()->data(index).toDouble();
if (value > 0.0) {
double sliceAngle = 360*value/totalValue;
double sliceAngle = 360 * value / totalValue;
if (angle >= startAngle && angle < (startAngle + sliceAngle))
return model()->index(row, 1, rootIndex());
@ -150,7 +150,7 @@ QModelIndex PieView::indexAt(const QPoint &point) const
return model()->index(row, 0, rootIndex());
// Update the list index that corresponds to the next valid row.
validRow++;
++validRow;
}
}
}
@ -182,27 +182,26 @@ QRect PieView::itemRect(const QModelIndex &index) const
else
valueIndex = index;
if (model()->data(valueIndex).toDouble() > 0.0) {
if (model()->data(valueIndex).toDouble() <= 0.0)
return QRect();
int listItem = 0;
for (int row = index.row()-1; row >= 0; --row) {
if (model()->data(model()->index(row, 1, rootIndex())).toDouble() > 0.0)
listItem++;
}
int listItem = 0;
for (int row = index.row()-1; row >= 0; --row) {
if (model()->data(model()->index(row, 1, rootIndex())).toDouble() > 0.0)
listItem++;
}
double itemHeight;
double itemHeight;
switch (index.column()) {
case 0:
itemHeight = QFontMetrics(viewOptions().font).height();
return QRect(totalSize,
int(margin + listItem*itemHeight),
totalSize - margin, int(itemHeight));
case 1:
return viewport()->rect();
}
switch (index.column()) {
case 0:
itemHeight = QFontMetrics(viewOptions().font).height();
return QRect(totalSize,
int(margin + listItem*itemHeight),
totalSize - margin, int(itemHeight));
case 1:
return viewport()->rect();
}
return QRect();
}
@ -225,11 +224,11 @@ QRegion PieView::itemRegion(const QModelIndex &index) const
double value = model()->data(sliceIndex).toDouble();
if (value > 0.0) {
double angle = 360*value/totalValue;
double angle = 360 * value / totalValue;
if (sliceIndex == index) {
QPainterPath slicePath;
slicePath.moveTo(totalSize/2, totalSize/2);
slicePath.moveTo(totalSize / 2, totalSize / 2);
slicePath.arcTo(margin, margin, margin+pieSize, margin+pieSize,
startAngle, angle);
slicePath.closeSubpath();
@ -322,62 +321,58 @@ void PieView::paintEvent(QPaintEvent *event)
// Viewport rectangles
QRect pieRect = QRect(margin, margin, pieSize, pieSize);
if (validItems > 0) {
if (validItems <= 0)
return;
painter.save();
painter.translate(pieRect.x() - horizontalScrollBar()->value(),
pieRect.y() - verticalScrollBar()->value());
painter.drawEllipse(0, 0, pieSize, pieSize);
double startAngle = 0.0;
int row;
painter.save();
painter.translate(pieRect.x() - horizontalScrollBar()->value(),
pieRect.y() - verticalScrollBar()->value());
painter.drawEllipse(0, 0, pieSize, pieSize);
double startAngle = 0.0;
int row;
for (row = 0; row < model()->rowCount(rootIndex()); ++row) {
for (row = 0; row < model()->rowCount(rootIndex()); ++row) {
QModelIndex index = model()->index(row, 1, rootIndex());
double value = model()->data(index).toDouble();
QModelIndex index = model()->index(row, 1, rootIndex());
double value = model()->data(index).toDouble();
if (value > 0.0) {
double angle = 360*value/totalValue;
if (value > 0.0) {
double angle = 360*value/totalValue;
QModelIndex colorIndex = model()->index(row, 0, rootIndex());
QColor color = QColor(model()->data(colorIndex, Qt::DecorationRole).toString());
QModelIndex colorIndex = model()->index(row, 0, rootIndex());
QColor color = QColor(model()->data(colorIndex,
Qt::DecorationRole).toString());
if (currentIndex() == index)
painter.setBrush(QBrush(color, Qt::Dense4Pattern));
else if (selections->isSelected(index))
painter.setBrush(QBrush(color, Qt::Dense3Pattern));
else
painter.setBrush(QBrush(color));
if (currentIndex() == index)
painter.setBrush(QBrush(color, Qt::Dense4Pattern));
else if (selections->isSelected(index))
painter.setBrush(QBrush(color, Qt::Dense3Pattern));
else
painter.setBrush(QBrush(color));
painter.drawPie(0, 0, pieSize, pieSize, int(startAngle*16), int(angle*16));
painter.drawPie(0, 0, pieSize, pieSize, int(startAngle*16),
int(angle*16));
startAngle += angle;
}
startAngle += angle;
}
painter.restore();
}
painter.restore();
int keyNumber = 0;
int keyNumber = 0;
for (row = 0; row < model()->rowCount(rootIndex()); ++row) {
for (row = 0; row < model()->rowCount(rootIndex()); ++row) {
QModelIndex index = model()->index(row, 1, rootIndex());
double value = model()->data(index).toDouble();
QModelIndex index = model()->index(row, 1, rootIndex());
double value = model()->data(index).toDouble();
if (value > 0.0) {
QModelIndex labelIndex = model()->index(row, 0, rootIndex());
if (value > 0.0) {
QModelIndex labelIndex = model()->index(row, 0, rootIndex());
QStyleOptionViewItem option = viewOptions();
option.rect = visualRect(labelIndex);
if (selections->isSelected(labelIndex))
option.state |= QStyle::State_Selected;
if (currentIndex() == labelIndex)
option.state |= QStyle::State_HasFocus;
itemDelegate()->paint(&painter, option, labelIndex);
QStyleOptionViewItem option = viewOptions();
option.rect = visualRect(labelIndex);
if (selections->isSelected(labelIndex))
option.state |= QStyle::State_Selected;
if (currentIndex() == labelIndex)
option.state |= QStyle::State_HasFocus;
itemDelegate()->paint(&painter, option, labelIndex);
keyNumber++;
}
++keyNumber;
}
}
}
@ -395,13 +390,12 @@ int PieView::rows(const QModelIndex &index) const
void PieView::rowsInserted(const QModelIndex &parent, int start, int end)
{
for (int row = start; row <= end; ++row) {
QModelIndex index = model()->index(row, 1, rootIndex());
double value = model()->data(index).toDouble();
if (value > 0.0) {
totalValue += value;
validItems++;
++validItems;
}
}
@ -411,12 +405,11 @@ void PieView::rowsInserted(const QModelIndex &parent, int start, int end)
void PieView::rowsAboutToBeRemoved(const QModelIndex &parent, int start, int end)
{
for (int row = start; row <= end; ++row) {
QModelIndex index = model()->index(row, 1, rootIndex());
double value = model()->data(index).toDouble();
if (value > 0.0) {
totalValue -= value;
validItems--;
--validItems;
}
}
@ -433,21 +426,23 @@ void PieView::scrollTo(const QModelIndex &index, ScrollHint)
QRect area = viewport()->rect();
QRect rect = visualRect(index);
if (rect.left() < area.left())
if (rect.left() < area.left()) {
horizontalScrollBar()->setValue(
horizontalScrollBar()->value() + rect.left() - area.left());
else if (rect.right() > area.right())
} else if (rect.right() > area.right()) {
horizontalScrollBar()->setValue(
horizontalScrollBar()->value() + qMin(
rect.right() - area.right(), rect.left() - area.left()));
}
if (rect.top() < area.top())
if (rect.top() < area.top()) {
verticalScrollBar()->setValue(
verticalScrollBar()->value() + rect.top() - area.top());
else if (rect.bottom() > area.bottom())
} else if (rect.bottom() > area.bottom()) {
verticalScrollBar()->setValue(
verticalScrollBar()->value() + qMin(
rect.bottom() - area.bottom(), rect.top() - area.top()));
}
update();
}
@ -524,12 +519,12 @@ int PieView::verticalOffset() const
QRect PieView::visualRect(const QModelIndex &index) const
{
QRect rect = itemRect(index);
if (rect.isValid())
return QRect(rect.left() - horizontalScrollBar()->value(),
rect.top() - verticalScrollBar()->value(),
rect.width(), rect.height());
else
if (!rect.isValid())
return rect;
return QRect(rect.left() - horizontalScrollBar()->value(),
rect.top() - verticalScrollBar()->value(),
rect.width(), rect.height());
}
/*

View File

@ -42,18 +42,6 @@
#define PIEVIEW_H
#include <QAbstractItemView>
#include <QFont>
#include <QItemSelection>
#include <QItemSelectionModel>
#include <QModelIndex>
#include <QRect>
#include <QSize>
#include <QPoint>
#include <QWidget>
QT_BEGIN_NAMESPACE
class QRubberBand;
QT_END_NAMESPACE
//! [0]
class PieView : public QAbstractItemView
@ -68,7 +56,8 @@ public:
QModelIndex indexAt(const QPoint &point) const;
protected slots:
void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector<int> & = QVector<int>());
void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight,
const QVector<int> &roles = QVector<int>());
void rowsInserted(const QModelIndex &parent, int start, int end);
void rowsAboutToBeRemoved(const QModelIndex &parent, int start, int end);
@ -111,4 +100,4 @@ private:
};
//! [0]
#endif
#endif // PIEVIEW_H

View File

@ -83,4 +83,4 @@ private:
};
//! [Window definition]
#endif
#endif // WINDOW_H

View File

@ -75,7 +75,7 @@ bool MySortFilterProxyModel::filterAcceptsRow(int sourceRow,
return (sourceModel()->data(index0).toString().contains(filterRegExp())
|| sourceModel()->data(index1).toString().contains(filterRegExp()))
&& dateInRange(sourceModel()->data(index2).toDate());
&& dateInRange(sourceModel()->data(index2).toDate());
}
//! [3]
@ -110,6 +110,6 @@ bool MySortFilterProxyModel::lessThan(const QModelIndex &left,
bool MySortFilterProxyModel::dateInRange(const QDate &date) const
{
return (!minDate.isValid() || date > minDate)
&& (!maxDate.isValid() || date < maxDate);
&& (!maxDate.isValid() || date < maxDate);
}
//! [7]

View File

@ -70,4 +70,4 @@ private:
};
//! [0]
#endif
#endif // MYSORTFILTERPROXYMODEL_H

View File

@ -150,7 +150,7 @@ void Window::textFilterChanged()
filterSyntaxComboBox->currentIndex()).toInt());
Qt::CaseSensitivity caseSensitivity =
filterCaseSensitivityCheckBox->isChecked() ? Qt::CaseSensitive
: Qt::CaseInsensitive;
: Qt::CaseInsensitive;
QRegExp regExp(filterPatternLineEdit->text(), caseSensitivity, syntax);
proxyModel->setFilterRegExp(regExp);

View File

@ -87,4 +87,4 @@ private:
};
//! [0]
#endif
#endif // WINDOW_H

View File

@ -38,7 +38,9 @@
**
****************************************************************************/
#include <QtWidgets>
#include <QApplication>
#include <QFileSystemModel>
#include <QTreeView>
int main(int argc, char *argv[])
{

View File

@ -38,7 +38,7 @@
**
****************************************************************************/
#include <QtWidgets>
#include <QApplication>
#include "mainwindow.h"

View File

@ -38,17 +38,16 @@
**
****************************************************************************/
#include <QtWidgets>
#include "mainwindow.h"
#include "treemodel.h"
#include <QFile>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
setupUi(this);
QStringList headers;
headers << tr("Title") << tr("Description");
@ -95,8 +94,7 @@ void MainWindow::insertChild()
QModelIndex child = model->index(0, column, index);
model->setData(child, QVariant("[No data]"), Qt::EditRole);
if (!model->headerData(column, Qt::Horizontal).isValid())
model->setHeaderData(column, Qt::Horizontal, QVariant("[No header]"),
Qt::EditRole);
model->setHeaderData(column, Qt::Horizontal, QVariant("[No header]"), Qt::EditRole);
}
view->selectionModel()->setCurrentIndex(model->index(0, 0, index),
@ -112,8 +110,7 @@ bool MainWindow::insertColumn(const QModelIndex &parent)
// Insert a column in the parent item.
bool changed = model->insertColumn(column + 1, parent);
if (changed)
model->setHeaderData(column + 1, Qt::Horizontal, QVariant("[No header]"),
Qt::EditRole);
model->setHeaderData(column + 1, Qt::Horizontal, QVariant("[No header]"), Qt::EditRole);
updateActions();

View File

@ -41,14 +41,10 @@
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QModelIndex>
#include "ui_mainwindow.h"
class QAction;
class QTreeView;
class QWidget;
#include <QMainWindow>
#include <QModelIndex>
class MainWindow : public QMainWindow, private Ui::MainWindow
{
@ -68,4 +64,4 @@ private slots:
void removeRow();
};
#endif
#endif // MAINWINDOW_H

View File

@ -44,10 +44,10 @@
A container for items of data supplied by the simple tree model.
*/
#include <QStringList>
#include "treeitem.h"
#include <QStringList>
//! [0]
TreeItem::TreeItem(const QVector<QVariant> &data, TreeItem *parent)
{

View File

@ -71,4 +71,4 @@ private:
};
//! [0]
#endif
#endif // TREEITEM_H

View File

@ -44,8 +44,7 @@
#include "treemodel.h"
//! [0]
TreeModel::TreeModel(const QStringList &headers, const QString &data,
QObject *parent)
TreeModel::TreeModel(const QStringList &headers, const QString &data, QObject *parent)
: QAbstractItemModel(parent)
{
QVector<QVariant> rootData;
@ -99,7 +98,8 @@ TreeItem *TreeModel::getItem(const QModelIndex &index) const
{
if (index.isValid()) {
TreeItem *item = static_cast<TreeItem*>(index.internalPointer());
if (item) return item;
if (item)
return item;
}
return rootItem;
}
@ -206,8 +206,7 @@ int TreeModel::rowCount(const QModelIndex &parent) const
}
//! [8]
bool TreeModel::setData(const QModelIndex &index, const QVariant &value,
int role)
bool TreeModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
if (role != Qt::EditRole)
return false;
@ -249,7 +248,7 @@ void TreeModel::setupModelData(const QStringList &lines, TreeItem *parent)
while (position < lines[number].length()) {
if (lines[number].mid(position, 1) != " ")
break;
position++;
++position;
}
QString lineData = lines[number].mid(position).trimmed();
@ -283,6 +282,6 @@ void TreeModel::setupModelData(const QStringList &lines, TreeItem *parent)
parent->child(parent->childCount() - 1)->setData(column, columnData[column]);
}
number++;
++number;
}
}

View File

@ -94,4 +94,4 @@ private:
};
//! [2]
#endif
#endif // TREEMODEL_H

View File

@ -39,10 +39,11 @@
****************************************************************************/
#include "filelistmodel.h"
#include <QApplication>
#include <QPalette>
#include <QBrush>
#include <QDir>
#include <QPalette>
FileListModel::FileListModel(QObject *parent)
: QAbstractListModel(parent)
@ -63,9 +64,9 @@ QVariant FileListModel::data(const QModelIndex &index, int role) const
if (index.row() >= fileList.size() || index.row() < 0)
return QVariant();
if (role == Qt::DisplayRole)
if (role == Qt::DisplayRole) {
return fileList.at(index.row());
else if (role == Qt::BackgroundRole) {
} else if (role == Qt::BackgroundRole) {
int batch = (index.row() / 100) % 2;
if (batch == 0)
return qApp->palette().base();

View File

@ -72,4 +72,4 @@ private:
};
//![0]
#endif
#endif // FILELISTMODEL_H

View File

@ -38,9 +38,10 @@
**
****************************************************************************/
#include <QApplication>
#include "window.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);

View File

@ -38,10 +38,11 @@
**
****************************************************************************/
#include <QtWidgets>
#include "filelistmodel.h"
#include "window.h"
#include <QtWidgets>
Window::Window(QWidget *parent)
: QWidget(parent)
{

View File

@ -61,4 +61,4 @@ private:
QTextBrowser *logViewer;
};
#endif
#endif // WINDOW_H

View File

@ -38,10 +38,11 @@
**
****************************************************************************/
#include <QtWidgets>
#include "freezetablewidget.h"
#include <QScrollBar>
#include <QHeaderView>
//! [constructor]
FreezeTableWidget::FreezeTableWidget(QAbstractItemModel * model)
{
@ -86,7 +87,7 @@ void FreezeTableWidget::init()
"background-color: #8EDE21;"
"selection-background-color: #999}"); //for demo purposes
frozenTableView->setSelectionModel(selectionModel());
for(int col=1; col<model()->columnCount(); col++)
for (int col = 1; col < model()->columnCount(); ++col)
frozenTableView->setColumnHidden(col, true);
frozenTableView->setColumnWidth(0, columnWidth(0) );
@ -105,15 +106,15 @@ void FreezeTableWidget::init()
//! [sections]
void FreezeTableWidget::updateSectionWidth(int logicalIndex, int, int newSize)
void FreezeTableWidget::updateSectionWidth(int logicalIndex, int /* oldSize */, int newSize)
{
if(logicalIndex==0){
frozenTableView->setColumnWidth(0,newSize);
if (logicalIndex == 0){
frozenTableView->setColumnWidth(0, newSize);
updateFrozenTableGeometry();
}
}
void FreezeTableWidget::updateSectionHeight(int logicalIndex, int, int newSize)
void FreezeTableWidget::updateSectionHeight(int logicalIndex, int /* oldSize */, int newSize)
{
frozenTableView->setRowHeight(logicalIndex, newSize);
}
@ -135,9 +136,8 @@ QModelIndex FreezeTableWidget::moveCursor(CursorAction cursorAction,
{
QModelIndex current = QTableView::moveCursor(cursorAction, modifiers);
if(cursorAction == MoveLeft && current.column()>0
&& visualRect(current).topLeft().x() < frozenTableView->columnWidth(0) ){
if (cursorAction == MoveLeft && current.column() > 0
&& visualRect(current).topLeft().x() < frozenTableView->columnWidth(0) ){
const int newValue = horizontalScrollBar()->value() + visualRect(current).topLeft().x()
- frozenTableView->columnWidth(0);
horizontalScrollBar()->setValue(newValue);
@ -147,18 +147,16 @@ QModelIndex FreezeTableWidget::moveCursor(CursorAction cursorAction,
//! [navigate]
void FreezeTableWidget::scrollTo (const QModelIndex & index, ScrollHint hint){
if(index.column()>0)
if (index.column() > 0)
QTableView::scrollTo(index, hint);
}
//! [geometry]
void FreezeTableWidget::updateFrozenTableGeometry()
{
frozenTableView->setGeometry( verticalHeader()->width()+frameWidth(),
frameWidth(), columnWidth(0),
viewport()->height()+horizontalHeader()->height());
frozenTableView->setGeometry(verticalHeader()->width() + frameWidth(),
frameWidth(), columnWidth(0),
viewport()->height()+horizontalHeader()->height());
}
//! [geometry]

View File

@ -64,9 +64,9 @@ private:
private slots:
void updateSectionWidth(int logicalIndex,int, int newSize);
void updateSectionHeight(int logicalIndex, int, int newSize);
void updateSectionWidth(int logicalIndex, int oldSize, int newSize);
void updateSectionHeight(int logicalIndex, int oldSize, int newSize);
};
//! [Widget definition]
#endif
#endif // FREEZETABLEWIDGET_H

View File

@ -44,45 +44,40 @@
#include "freezetablewidget.h"
int main( int argc, char** argv )
int main(int argc, char* argv[])
{
Q_INIT_RESOURCE(grades);
Q_INIT_RESOURCE(grades);
QApplication app( argc, argv );
QStandardItemModel *model=new QStandardItemModel();
QFile file(":/grades.txt");
if (file.open(QFile::ReadOnly)) {
QString line = file.readLine(200);
QStringList list = line.simplified().split(",");
model->setHorizontalHeaderLabels(list);
QApplication app( argc, argv );
QStandardItemModel *model=new QStandardItemModel();
QFile file(":/grades.txt");
QString line;
QStringList list;
if (file.open(QFile::ReadOnly)) {
int row = 0;
QStandardItem *newItem = 0;
while (file.canReadLine()) {
line = file.readLine(200);
list= line.simplified().split(",");
model->setHorizontalHeaderLabels(list);
int row=0;
QStandardItem *newItem=0;
while(file.canReadLine()){
line = file.readLine(200);
if(!line.startsWith("#") && line.contains(",")){
list= line.simplified().split(",");
for(int col=0; col<list.length(); col++){
newItem = new QStandardItem(list.at(col));
model->setItem(row ,col, newItem);
}
row++;
}
if (!line.startsWith("#") && line.contains(",")) {
list= line.simplified().split(",");
for (int col = 0; col < list.length(); ++col){
newItem = new QStandardItem(list.at(col));
model->setItem(row, col, newItem);
}
++row;
}
}
file.close();
}
}
file.close();
FreezeTableWidget *tableView = new FreezeTableWidget(model);
FreezeTableWidget *tableView = new FreezeTableWidget(model);
tableView->setWindowTitle(QObject::tr("Frozen Column Example"));
tableView->resize(560,680);
tableView->show();
return app.exec();
tableView->setWindowTitle(QObject::tr("Frozen Column Example"));
tableView->resize(560, 680);
tableView->show();
return app.exec();
}

View File

@ -42,11 +42,11 @@
#include "model.h"
#include <QApplication>
#include <QTableView>
#include <QTreeView>
#include <QHeaderView>
#include <QListView>
#include <QSplitter>
#include <QHeaderView>
#include <QTableView>
#include <QTreeView>
int main(int argc, char *argv[])
{

View File

@ -40,6 +40,7 @@
****************************************************************************/
#include "model.h"
#include <QIcon>
#include <QPixmap>
@ -60,10 +61,10 @@ Model::~Model()
QModelIndex Model::index(int row, int column, const QModelIndex &parent) const
{
if (row < rc && row >= 0 && column < cc && column >= 0) {
Node *p = static_cast<Node*>(parent.internalPointer());
Node *n = node(row, p);
if (n)
return createIndex(row, column, n);
Node *parentNode = static_cast<Node*>(parent.internalPointer());
Node *childNode = node(row, parentNode);
if (childNode)
return createIndex(row, column, childNode);
}
return QModelIndex();
}
@ -71,10 +72,10 @@ QModelIndex Model::index(int row, int column, const QModelIndex &parent) const
QModelIndex Model::parent(const QModelIndex &child) const
{
if (child.isValid()) {
Node *n = static_cast<Node*>(child.internalPointer());
Node *p = parent(n);
if (p)
return createIndex(row(p), 0, p);
Node *childNode = static_cast<Node*>(child.internalPointer());
Node *parentNode = parent(childNode);
if (parentNode)
return createIndex(row(parentNode), 0, parentNode);
}
return QModelIndex();
}
@ -130,7 +131,7 @@ Qt::ItemFlags Model::flags(const QModelIndex &index) const
Model::Node *Model::node(int row, Node *parent) const
{
if (parent && !parent->children)
parent->children = new QVector<Node>(rc, Node(parent));
parent->children = new QVector<Node>(rc, Node(parent));
QVector<Node> *v = parent ? parent->children : tree;
return const_cast<Node*>(&(v->at(row)));
}
@ -142,6 +143,6 @@ Model::Node *Model::parent(Node *child) const
int Model::row(Node *node) const
{
const Node *first = node->parent ? &(node->parent->children->at(0)) : &(tree->at(0));
return (node - first);
const Node *first = node->parent ? &(node->parent->children->at(0)) : &(tree->at(0));
return node - first;
}

View File

@ -71,10 +71,10 @@ private:
struct Node
{
Node(Node *parent = 0) : parent(parent), children(0) {}
~Node() { delete children; }
Node *parent;
QVector<Node> *children;
Node(Node *parent = 0) : parent(parent), children(0) {}
~Node() { delete children; }
Node *parent;
QVector<Node> *children;
};
Node *node(int row, Node *parent) const;
@ -82,9 +82,10 @@ private:
int row(Node *node) const;
QIcon services;
int rc, cc;
int rc;
int cc;
QVector<Node> *tree;
QFileIconProvider iconProvider;
};
#endif
#endif // MODEL_H

View File

@ -38,8 +38,6 @@
**
****************************************************************************/
#include <QtWidgets>
#include "imagemodel.h"
//! [0]

View File

@ -65,4 +65,4 @@ private:
};
//! [0]
#endif
#endif // IMAGEMODEL_H

View File

@ -38,16 +38,16 @@
**
****************************************************************************/
#include "imagemodel.h"
#include "mainwindow.h"
#include "pixeldelegate.h"
#include <QtWidgets>
#ifndef QT_NO_PRINTER
#include <QPrinter>
#include <QPrintDialog>
#endif
#include "imagemodel.h"
#include "mainwindow.h"
#include "pixeldelegate.h"
//! [0]
MainWindow::MainWindow()
{
@ -155,8 +155,7 @@ void MainWindow::openImage(const QString &fileName)
void MainWindow::printImage()
{
#ifndef QT_NO_PRINTER
if (model->rowCount(QModelIndex())*model->columnCount(QModelIndex())
> 90000) {
if (model->rowCount(QModelIndex())*model->columnCount(QModelIndex()) > 90000) {
QMessageBox::StandardButton answer;
answer = QMessageBox::question(this, tr("Large Image Size"),
tr("The printed image may be very large. Are you sure that "
@ -179,26 +178,26 @@ void MainWindow::printImage()
int rows = model->rowCount(QModelIndex());
int columns = model->columnCount(QModelIndex());
int sourceWidth = (columns+1) * ItemSize;
int sourceHeight = (rows+1) * ItemSize;
int sourceWidth = (columns + 1) * ItemSize;
int sourceHeight = (rows + 1) * ItemSize;
painter.save();
double xscale = printer.pageRect().width()/double(sourceWidth);
double yscale = printer.pageRect().height()/double(sourceHeight);
double xscale = printer.pageRect().width() / double(sourceWidth);
double yscale = printer.pageRect().height() / double(sourceHeight);
double scale = qMin(xscale, yscale);
painter.translate(printer.paperRect().x() + printer.pageRect().width()/2,
printer.paperRect().y() + printer.pageRect().height()/2);
painter.translate(printer.paperRect().x() + printer.pageRect().width() / 2,
printer.paperRect().y() + printer.pageRect().height() / 2);
painter.scale(scale, scale);
painter.translate(-sourceWidth/2, -sourceHeight/2);
painter.translate(-sourceWidth / 2, -sourceHeight / 2);
QStyleOptionViewItem option;
QModelIndex parent = QModelIndex();
QProgressDialog progress(tr("Printing..."), tr("Cancel"), 0, rows, this);
progress.setWindowModality(Qt::ApplicationModal);
float y = ItemSize/2;
float y = ItemSize / 2;
for (int row = 0; row < rows; ++row) {
progress.setValue(row);
@ -206,7 +205,7 @@ void MainWindow::printImage()
if (progress.wasCanceled())
break;
float x = ItemSize/2;
float x = ItemSize / 2;
for (int column = 0; column < columns; ++column) {
option.rect = QRect(int(x), int(y), ItemSize, ItemSize);

View File

@ -71,4 +71,4 @@ private:
QTableView *view;
};
#endif
#endif // MAINWINDOW_H

View File

@ -38,10 +38,10 @@
**
****************************************************************************/
#include <QtWidgets>
#include "pixeldelegate.h"
#include <QPainter>
//! [0]
PixelDelegate::PixelDelegate(QObject *parent)
: QAbstractItemDelegate(parent)
@ -63,7 +63,7 @@ void PixelDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
int size = qMin(option.rect.width(), option.rect.height());
//! [3] //! [4]
int brightness = index.model()->data(index, Qt::DisplayRole).toInt();
double radius = (size/2.0) - (brightness/255.0 * size/2.0);
double radius = (size / 2.0) - (brightness / 255.0 * size / 2.0);
if (radius == 0.0)
return;
//! [4]
@ -84,9 +84,9 @@ void PixelDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
//! [9]
//! [10]
painter->drawEllipse(QRectF(option.rect.x() + option.rect.width()/2 - radius,
option.rect.y() + option.rect.height()/2 - radius,
2*radius, 2*radius));
painter->drawEllipse(QRectF(option.rect.x() + option.rect.width() / 2 - radius,
option.rect.y() + option.rect.height() / 2 - radius,
2 * radius, 2 * radius));
painter->restore();
}
//! [10]

View File

@ -42,7 +42,6 @@
#define PIXELDELEGATE_H
#include <QAbstractItemDelegate>
#include <QFontMetrics>
#include <QModelIndex>
#include <QSize>
@ -76,4 +75,4 @@ private:
};
//! [0]
#endif
#endif // PIXELDELEGATE_H

View File

@ -38,10 +38,10 @@
**
****************************************************************************/
#include <QApplication>
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
Q_INIT_RESOURCE(puzzle);

View File

@ -38,13 +38,13 @@
**
****************************************************************************/
#include <QtWidgets>
#include <stdlib.h>
#include "mainwindow.h"
#include "piecesmodel.h"
#include "puzzlewidget.h"
#include <QtWidgets>
#include <stdlib.h>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
@ -61,9 +61,10 @@ void MainWindow::openImage(const QString &path)
{
QString fileName = path;
if (fileName.isNull())
if (fileName.isNull()) {
fileName = QFileDialog::getOpenFileName(this,
tr("Open Image"), "", tr("Image Files (*.png *.jpg *.bmp)"));
}
if (!fileName.isEmpty()) {
QPixmap newImage;
@ -81,9 +82,9 @@ void MainWindow::openImage(const QString &path)
void MainWindow::setCompleted()
{
QMessageBox::information(this, tr("Puzzle Completed"),
tr("Congratulations! You have completed the puzzle!\n"
"Click OK to start again."),
QMessageBox::Ok);
tr("Congratulations! You have completed the puzzle!\n"
"Click OK to start again."),
QMessageBox::Ok);
setupPuzzle();
}
@ -91,8 +92,8 @@ void MainWindow::setCompleted()
void MainWindow::setupPuzzle()
{
int size = qMin(puzzleImage.width(), puzzleImage.height());
puzzleImage = puzzleImage.copy((puzzleImage.width() - size)/2,
(puzzleImage.height() - size)/2, size, size).scaled(puzzleWidget->imageSize(),
puzzleImage = puzzleImage.copy((puzzleImage.width() - size) / 2,
(puzzleImage.height() - size) / 2, size, size).scaled(puzzleWidget->imageSize(),
puzzleWidget->imageSize(), Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
qsrand(QCursor::pos().x() ^ QCursor::pos().y());

View File

@ -41,8 +41,8 @@
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QPixmap>
#include <QMainWindow>
#include <QPixmap>
class PuzzleWidget;
class PiecesModel;
@ -74,4 +74,4 @@ private:
PiecesModel *model;
};
#endif
#endif // MAINWINDOW_H

View File

@ -38,10 +38,11 @@
**
****************************************************************************/
#include <QtWidgets>
#include "piecesmodel.h"
#include <QIcon>
#include <QMimeData>
PiecesModel::PiecesModel(int pieceSize, QObject *parent)
: QAbstractListModel(parent), m_PieceSize(pieceSize)
{
@ -66,7 +67,7 @@ QVariant PiecesModel::data(const QModelIndex &index, int role) const
void PiecesModel::addPiece(const QPixmap &pixmap, const QPoint &location)
{
int row;
if (int(2.0*qrand()/(RAND_MAX+1.0)) == 1)
if (int(2.0 * qrand() / (RAND_MAX + 1.0)) == 1)
row = 0;
else
row = pixmaps.size();
@ -153,8 +154,9 @@ bool PiecesModel::dropMimeData(const QMimeData *data, Qt::DropAction action,
endRow = pixmaps.size();
else
endRow = qMin(row, pixmaps.size());
} else
} else {
endRow = parent.row();
}
QByteArray encodedData = data->data("image/x-puzzle-piece");
QDataStream stream(&encodedData, QIODevice::ReadOnly);

View File

@ -79,4 +79,4 @@ private:
int m_PieceSize;
};
#endif
#endif // PIECESLIST_H

View File

@ -38,10 +38,10 @@
**
****************************************************************************/
#include <QtWidgets>
#include "puzzlewidget.h"
#include <QtWidgets>
PuzzleWidget::PuzzleWidget(int imageSize, QWidget *parent)
: QWidget(parent), m_ImageSize(imageSize)
{
@ -130,9 +130,8 @@ void PuzzleWidget::dropEvent(QDropEvent *event)
int PuzzleWidget::findPiece(const QRect &pieceRect) const
{
for (int i = 0; i < pieceRects.size(); ++i) {
if (pieceRect == pieceRects[i]) {
if (pieceRect == pieceRects[i])
return i;
}
}
return -1;
}

View File

@ -42,8 +42,8 @@
#define PUZZLEWIDGET_H
#include <QList>
#include <QPoint>
#include <QPixmap>
#include <QPoint>
#include <QWidget>
QT_BEGIN_NAMESPACE
@ -86,4 +86,4 @@ private:
int m_ImageSize;
};
#endif
#endif // PUZZLEWIDGET_H

View File

@ -38,10 +38,10 @@
**
****************************************************************************/
#include <QtXml>
#include "domitem.h"
#include <QtXml>
//! [0]
DomItem::DomItem(QDomNode &node, int row, DomItem *parent)
{

View File

@ -63,4 +63,4 @@ private:
};
//! [0]
#endif
#endif // DOMITEM_H

View File

@ -38,12 +38,11 @@
**
****************************************************************************/
#include <QtWidgets>
#include <QtXml>
#include "domitem.h"
#include "dommodel.h"
#include <QtXml>
//! [0]
DomModel::DomModel(QDomDocument document, QObject *parent)
: QAbstractItemModel(parent), domDocument(document)

View File

@ -44,7 +44,6 @@
#include <QAbstractItemModel>
#include <QDomDocument>
#include <QModelIndex>
#include <QVariant>
class DomItem;
@ -73,4 +72,4 @@ private:
};
//! [0]
#endif
#endif // DOMMODEL_H

View File

@ -38,10 +38,10 @@
**
****************************************************************************/
#include <QApplication>
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);

View File

@ -38,20 +38,19 @@
**
****************************************************************************/
#include <QDomDocument>
#include <QFile>
#include <QtWidgets>
#include "dommodel.h"
#include "mainwindow.h"
#include <QDomDocument>
#include <QTreeView>
#include <QMenuBar>
#include <QFileDialog>
MainWindow::MainWindow() : QMainWindow(), model(0)
{
fileMenu = menuBar()->addMenu(tr("&File"));
fileMenu->addAction(tr("&Open..."), this, SLOT(openFile()),
QKeySequence::Open);
fileMenu->addAction(tr("E&xit"), this, SLOT(close()),
QKeySequence::Quit);
fileMenu->addAction(tr("&Open..."), this, SLOT(openFile()), QKeySequence::Open);
fileMenu->addAction(tr("E&xit"), this, SLOT(close()), QKeySequence::Quit);
model = new DomModel(QDomDocument(), this);
view = new QTreeView(this);

View File

@ -67,4 +67,4 @@ private:
QTreeView *view;
};
#endif
#endif // MAINWINDOW_H

View File

@ -38,10 +38,12 @@
**
****************************************************************************/
#include <QtWidgets>
#include "treemodel.h"
#include <QApplication>
#include <QFile>
#include <QTreeView>
int main(int argc, char *argv[])
{
Q_INIT_RESOURCE(simpletreemodel);

View File

@ -67,4 +67,4 @@ private:
};
//! [0]
#endif
#endif // TREEITEM_H

View File

@ -45,11 +45,11 @@
models.
*/
#include <QtWidgets>
#include "treeitem.h"
#include "treemodel.h"
#include <QStringList>
//! [0]
TreeModel::TreeModel(const QString &data, QObject *parent)
: QAbstractItemModel(parent)
@ -213,6 +213,6 @@ void TreeModel::setupModelData(const QStringList &lines, TreeItem *parent)
parents.last()->appendChild(new TreeItem(columnData, parents.last()));
}
number++;
++number;
}
}

View File

@ -73,4 +73,4 @@ private:
};
//! [0]
#endif
#endif // TREEMODEL_H

View File

@ -38,10 +38,10 @@
**
****************************************************************************/
#include <QApplication>
#include "window.h"
#include <QApplication>
int main(int argc, char **argv)
{
QApplication app(argc, argv);

View File

@ -38,10 +38,10 @@
**
****************************************************************************/
#include <QtWidgets>
#include "window.h"
#include <QtWidgets>
//! [Set up widgets]
Window::Window(QWidget *parent)
: QWidget(parent)
@ -69,12 +69,9 @@ Window::Window(QWidget *parent)
mapper->addMapping(addressEdit, 1);
mapper->addMapping(ageSpinBox, 2);
connect(previousButton, SIGNAL(clicked()),
mapper, SLOT(toPrevious()));
connect(nextButton, SIGNAL(clicked()),
mapper, SLOT(toNext()));
connect(mapper, SIGNAL(currentIndexChanged(int)),
this, SLOT(updateButtons(int)));
connect(previousButton, SIGNAL(clicked()), mapper, SLOT(toPrevious()));
connect(nextButton, SIGNAL(clicked()), mapper, SLOT(toNext()));
connect(mapper, SIGNAL(currentIndexChanged(int)), this, SLOT(updateButtons(int)));
//! [Set up the mapper]
//! [Set up the layout]

View File

@ -81,4 +81,4 @@ private:
};
//! [Window definition]
#endif
#endif // WINDOW_H

View File

@ -45,10 +45,9 @@
using a spin box widget.
*/
#include <QtWidgets>
#include "delegate.h"
#include <QSpinBox>
//! [0]
SpinBoxDelegate::SpinBoxDelegate(QObject *parent)

View File

@ -42,10 +42,6 @@
#define DELEGATE_H
#include <QStyledItemDelegate>
#include <QModelIndex>
#include <QObject>
#include <QSize>
#include <QSpinBox>
//! [0]
class SpinBoxDelegate : public QStyledItemDelegate

View File

@ -45,14 +45,13 @@
data obtained from a model.
*/
#include "delegate.h"
#include <QApplication>
#include <QHeaderView>
#include <QItemSelectionModel>
#include <QStandardItemModel>
#include <QTableView>
#include "delegate.h"
//! [0]
int main(int argc, char *argv[])
{
@ -72,7 +71,7 @@ int main(int argc, char *argv[])
for (int row = 0; row < 4; ++row) {
for (int column = 0; column < 2; ++column) {
QModelIndex index = model.index(row, column, QModelIndex());
model.setData(index, QVariant((row+1) * (column+1)));
model.setData(index, QVariant((row + 1) * (column + 1)));
}
//! [1] //! [2]
}

View File

@ -39,9 +39,10 @@
**
****************************************************************************/
#include <QtWidgets/QApplication>
#include "spreadsheet.h"
#include <QApplication>
int main(int argc, char** argv) {
Q_INIT_RESOURCE(spreadsheet);
QApplication app(argc, argv);

View File

@ -39,13 +39,13 @@
**
****************************************************************************/
#include "printview.h"
#ifndef QT_NO_PRINTER
#include <QPrinter>
#endif
#include <QStyleOptionViewItem>
#include "printview.h"
PrintView::PrintView()
{
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);

View File

@ -69,7 +69,7 @@ SpreadSheet::SpreadSheet(int rows, int cols, QWidget *parent)
table->setHorizontalHeaderItem(c, new QTableWidgetItem(character));
}
table->setItemPrototype(table->item(rows -1, cols - 1));
table->setItemPrototype(table->item(rows - 1, cols - 1));
table->setItemDelegate(new SpreadSheetDelegate());
createActions();
@ -168,10 +168,8 @@ void SpreadSheet::setupMenuBar()
void SpreadSheet::updateStatus(QTableWidgetItem *item)
{
if (item && item == table->currentItem()) {
statusBar()->showMessage(item->data(Qt::StatusTipRole).toString(),
1000);
cellLabel->setText(tr("Cell: (%1)").arg(encode_pos(table->row(item),
table->column(item))));
statusBar()->showMessage(item->data(Qt::StatusTipRole).toString(), 1000);
cellLabel->setText(tr("Cell: (%1)").arg(encode_pos(table->row(item), table->column(item))));
}
}
@ -236,9 +234,10 @@ void SpreadSheet::selectColor()
if (selected.count() == 0)
return;
foreach(QTableWidgetItem *i, selected)
foreach (QTableWidgetItem *i, selected) {
if (i)
i->setBackgroundColor(col);
}
updateColor(table->currentItem());
}
@ -254,9 +253,10 @@ void SpreadSheet::selectFont()
if (!ok)
return;
foreach(QTableWidgetItem *i, selected)
foreach (QTableWidgetItem *i, selected) {
if (i)
i->setFont(fnt);
}
}
bool SpreadSheet::runInputDialog(const QString &title,
@ -407,7 +407,8 @@ void SpreadSheet::actionSum()
if (runInputDialog(tr("Sum cells"), tr("First cell:"), tr("Last cell:"),
QString("%1").arg(QChar(0x03a3)), tr("Output to:"),
&cell1, &cell2, &out)) {
int row, col;
int row;
int col;
decode_pos(out, &row, &col);
table->item(row, col)->setText(tr("sum %1 %2").arg(cell1, cell2));
}
@ -631,8 +632,7 @@ void SpreadSheet::print()
QPrintPreviewDialog dlg(&printer);
PrintView view;
view.setModel(table->model());
connect(&dlg, SIGNAL(paintRequested(QPrinter*)),
&view, SLOT(print(QPrinter*)));
connect(&dlg, SIGNAL(paintRequested(QPrinter*)), &view, SLOT(print(QPrinter*)));
dlg.exec();
#endif
}

View File

@ -40,6 +40,7 @@
****************************************************************************/
#include "spreadsheetdelegate.h"
#include <QtWidgets>
SpreadSheetDelegate::SpreadSheetDelegate(QObject *parent)
@ -70,8 +71,7 @@ QWidget *SpreadSheetDelegate::createEditor(QWidget *parent,
QCompleter *autoComplete = new QCompleter(allStrings);
editor->setCompleter(autoComplete);
connect(editor, SIGNAL(editingFinished()),
this, SLOT(commitAndCloseEditor()));
connect(editor, SIGNAL(editingFinished()), this, SLOT(commitAndCloseEditor()));
return editor;
}
@ -88,13 +88,14 @@ void SpreadSheetDelegate::setEditorData(QWidget *editor,
QLineEdit *edit = qobject_cast<QLineEdit*>(editor);
if (edit) {
edit->setText(index.model()->data(index, Qt::EditRole).toString());
} else {
QDateTimeEdit *dateEditor = qobject_cast<QDateTimeEdit *>(editor);
if (dateEditor) {
dateEditor->setDate(QDate::fromString(
index.model()->data(index, Qt::EditRole).toString(),
"d/M/yyyy"));
}
return;
}
QDateTimeEdit *dateEditor = qobject_cast<QDateTimeEdit *>(editor);
if (dateEditor) {
dateEditor->setDate(QDate::fromString(
index.model()->data(index, Qt::EditRole).toString(),
"d/M/yyyy"));
}
}
@ -104,11 +105,10 @@ void SpreadSheetDelegate::setModelData(QWidget *editor,
QLineEdit *edit = qobject_cast<QLineEdit *>(editor);
if (edit) {
model->setData(index, edit->text());
} else {
QDateTimeEdit *dateEditor = qobject_cast<QDateTimeEdit *>(editor);
if (dateEditor) {
model->setData(index, dateEditor->date().toString("dd/M/yyyy"));
}
return;
}
}
QDateTimeEdit *dateEditor = qobject_cast<QDateTimeEdit *>(editor);
if (dateEditor)
model->setData(index, dateEditor->date().toString("dd/M/yyyy"));
}

View File

@ -42,9 +42,10 @@
#ifndef SPREADSHEETDELEGATE_H
#define SPREADSHEETDELEGATE_H
#include <QItemDelegate>
#include "spreadsheet.h"
#include <QItemDelegate>
class SpreadSheetDelegate : public QItemDelegate
{
Q_OBJECT

View File

@ -42,10 +42,10 @@
#ifndef SPREADSHEETITEM_H
#define SPREADSHEETITEM_H
#include <QTableWidgetItem>
#include <QtWidgets>
#include "spreadsheet.h"
#include <QTableWidgetItem>
class SpreadSheetItem : public QTableWidgetItem
{
public:
@ -59,7 +59,9 @@ public:
QVariant display() const;
inline QString formula() const
{ return QTableWidgetItem::data(Qt::DisplayRole).toString(); }
{
return QTableWidgetItem::data(Qt::DisplayRole).toString();
}
static QVariant computeFormula(const QString &formula,
const QTableWidget *widget,