diff options
Diffstat (limited to 'examples/widgets/itemviews/spreadsheet')
8 files changed, 72 insertions, 90 deletions
diff --git a/examples/widgets/itemviews/spreadsheet/main.cpp b/examples/widgets/itemviews/spreadsheet/main.cpp index 769edbdf03..548dc8d604 100644 --- a/examples/widgets/itemviews/spreadsheet/main.cpp +++ b/examples/widgets/itemviews/spreadsheet/main.cpp @@ -53,7 +53,8 @@ #include <QApplication> #include <QLayout> -int main(int argc, char** argv) { +int main(int argc, char **argv) +{ Q_INIT_RESOURCE(spreadsheet); QApplication app(argc, argv); SpreadSheet sheet(10, 6); diff --git a/examples/widgets/itemviews/spreadsheet/printview.cpp b/examples/widgets/itemviews/spreadsheet/printview.cpp index 31424e9070..7db1a6bad9 100644 --- a/examples/widgets/itemviews/spreadsheet/printview.cpp +++ b/examples/widgets/itemviews/spreadsheet/printview.cpp @@ -48,12 +48,11 @@ ** ****************************************************************************/ +#include "printview.h" + #ifndef QT_NO_PRINTER #include <QPrinter> #endif -#include <QStyleOptionViewItem> - -#include "printview.h" PrintView::PrintView() { diff --git a/examples/widgets/itemviews/spreadsheet/spreadsheet.cpp b/examples/widgets/itemviews/spreadsheet/spreadsheet.cpp index 621f655b02..fc7fbb872c 100644 --- a/examples/widgets/itemviews/spreadsheet/spreadsheet.cpp +++ b/examples/widgets/itemviews/spreadsheet/spreadsheet.cpp @@ -48,36 +48,30 @@ ** ****************************************************************************/ -#include <QtWidgets> -#if defined(QT_PRINTSUPPORT_LIB) -#include <QtPrintSupport/qtprintsupportglobal.h> -#if QT_CONFIG(printdialog) -#include <QPrinter> -#include <QPrintDialog> -#endif -#if QT_CONFIG(printpreviewdialog) -#include <QPrintPreviewDialog> -#endif -#endif - #include "spreadsheet.h" #include "spreadsheetdelegate.h" #include "spreadsheetitem.h" #include "printview.h" +#include <QtWidgets> +#if defined(QT_PRINTSUPPORT_LIB) +#include <QtPrintSupport> +#endif + SpreadSheet::SpreadSheet(int rows, int cols, QWidget *parent) - : QMainWindow(parent) + : QMainWindow(parent), + toolBar(new QToolBar(this)), + cellLabel(new QLabel(toolBar)), + table(new QTableWidget(rows, cols, this)), + formulaInput(new QLineEdit(this)) { - addToolBar(toolBar = new QToolBar()); - formulaInput = new QLineEdit(); + addToolBar(toolBar); - cellLabel = new QLabel(toolBar); cellLabel->setMinimumSize(80, 0); toolBar->addWidget(cellLabel); toolBar->addWidget(formulaInput); - table = new QTableWidget(rows, cols, this); table->setSizeAdjustPolicy(QTableWidget::AdjustToContents); for (int c = 0; c < cols; ++c) { QString character(QChar('A' + c)); @@ -88,7 +82,7 @@ SpreadSheet::SpreadSheet(int rows, int cols, QWidget *parent) table->setItemDelegate(new SpreadSheetDelegate()); createActions(); - updateColor(0); + updateColor(nullptr); setupMenuBar(); setupContents(); setupContextMenu(); @@ -103,7 +97,8 @@ SpreadSheet::SpreadSheet(int rows, int cols, QWidget *parent) this, &SpreadSheet::updateLineEdit); connect(table, &QTableWidget::itemChanged, this, &SpreadSheet::updateStatus); - connect(formulaInput, &QLineEdit::returnPressed, this, &SpreadSheet::returnPressed); + connect(formulaInput, &QLineEdit::returnPressed, + this, &SpreadSheet::returnPressed); connect(table, &QTableWidget::itemChanged, this, &SpreadSheet::updateLineEdit); @@ -193,19 +188,19 @@ void SpreadSheet::updateColor(QTableWidgetItem *item) QPixmap pix(16, 16); QColor col; if (item) - col = item->backgroundColor(); + col = item->background().color(); if (!col.isValid()) col = palette().base().color(); QPainter pt(&pix); pt.fillRect(0, 0, 16, 16, col); - QColor lighter = col.light(); + QColor lighter = col.lighter(); pt.setPen(lighter); QPoint lightFrame[] = { QPoint(0, 15), QPoint(0, 0), QPoint(15, 0) }; pt.drawPolyline(lightFrame, 3); - pt.setPen(col.dark()); + pt.setPen(col.darker()); QPoint darkFrame[] = { QPoint(1, 15), QPoint(15, 15), QPoint(15, 1) }; pt.drawPolyline(darkFrame, 3); @@ -240,18 +235,18 @@ void SpreadSheet::returnPressed() void SpreadSheet::selectColor() { QTableWidgetItem *item = table->currentItem(); - QColor col = item ? item->backgroundColor() : table->palette().base().color(); + QColor col = item ? item->background().color() : table->palette().base().color(); col = QColorDialog::getColor(col, this); if (!col.isValid()) return; - QList<QTableWidgetItem*> selected = table->selectedItems(); - if (selected.count() == 0) + const QList<QTableWidgetItem *> selected = table->selectedItems(); + if (selected.isEmpty()) return; - foreach (QTableWidgetItem *i, selected) { + for (QTableWidgetItem *i : selected) { if (i) - i->setBackgroundColor(col); + i->setBackground(col); } updateColor(table->currentItem()); @@ -259,8 +254,8 @@ void SpreadSheet::selectColor() void SpreadSheet::selectFont() { - QList<QTableWidgetItem*> selected = table->selectedItems(); - if (selected.count() == 0) + const QList<QTableWidgetItem *> selected = table->selectedItems(); + if (selected.isEmpty()) return; bool ok = false; @@ -268,7 +263,7 @@ void SpreadSheet::selectFont() if (!ok) return; - foreach (QTableWidgetItem *i, selected) { + for (QTableWidgetItem *i : selected) { if (i) i->setFont(fnt); } @@ -397,7 +392,7 @@ void SpreadSheet::actionSum() int col_last = 0; int col_cur = 0; - QList<QTableWidgetItem*> selected = table->selectedItems(); + const QList<QTableWidgetItem *> selected = table->selectedItems(); if (!selected.isEmpty()) { QTableWidgetItem *first = selected.first(); @@ -408,7 +403,7 @@ void SpreadSheet::actionSum() col_last = table->column(last); } - QTableWidgetItem *current = table->currentItem(); + const QTableWidgetItem *current = table->currentItem(); if (current) { row_cur = table->row(current); @@ -435,7 +430,7 @@ void SpreadSheet::actionMath_helper(const QString &title, const QString &op) QString cell2 = "C2"; QString out = "C3"; - QTableWidgetItem *current = table->currentItem(); + const QTableWidgetItem *current = table->currentItem(); if (current) out = encode_pos(table->currentRow(), table->currentColumn()); @@ -468,8 +463,9 @@ void SpreadSheet::actionDivide() void SpreadSheet::clear() { - foreach (QTableWidgetItem *i, table->selectedItems()) - i->setText(""); + const QList<QTableWidgetItem *> selectedItems = table->selectedItems(); + for (QTableWidgetItem *i : selectedItems) + i->setText(QString()); } void SpreadSheet::setupContextMenu() @@ -489,13 +485,13 @@ void SpreadSheet::setupContextMenu() void SpreadSheet::setupContents() { - QColor titleBackground(Qt::lightGray); + QBrush titleBackground(Qt::lightGray); QFont titleFont = table->font(); titleFont.setBold(true); // column 0 table->setItem(0, 0, new SpreadSheetItem("Item")); - table->item(0, 0)->setBackgroundColor(titleBackground); + table->item(0, 0)->setBackground(titleBackground); table->item(0, 0)->setToolTip("This column shows the purchased item/service"); table->item(0, 0)->setFont(titleFont); @@ -510,11 +506,11 @@ void SpreadSheet::setupContents() table->setItem(9, 0, new SpreadSheetItem("Total:")); table->item(9, 0)->setFont(titleFont); - table->item(9, 0)->setBackgroundColor(Qt::lightGray); + table->item(9, 0)->setBackground(titleBackground); // column 1 table->setItem(0, 1, new SpreadSheetItem("Date")); - table->item(0, 1)->setBackgroundColor(titleBackground); + table->item(0, 1)->setBackground(titleBackground); table->item(0, 1)->setToolTip("This column shows the purchase date, double click to change"); table->item(0, 1)->setFont(titleFont); @@ -528,11 +524,11 @@ void SpreadSheet::setupContents() table->setItem(8, 1, new SpreadSheetItem("18/6/2006")); table->setItem(9, 1, new SpreadSheetItem()); - table->item(9, 1)->setBackgroundColor(Qt::lightGray); + table->item(9, 1)->setBackground(titleBackground); // column 2 table->setItem(0, 2, new SpreadSheetItem("Price")); - table->item(0, 2)->setBackgroundColor(titleBackground); + table->item(0, 2)->setBackground(titleBackground); table->item(0, 2)->setToolTip("This column shows the price of the purchase"); table->item(0, 2)->setFont(titleFont); @@ -546,11 +542,11 @@ void SpreadSheet::setupContents() table->setItem(8, 2, new SpreadSheetItem("1240")); table->setItem(9, 2, new SpreadSheetItem()); - table->item(9, 2)->setBackgroundColor(Qt::lightGray); + table->item(9, 2)->setBackground(Qt::lightGray); // column 3 table->setItem(0, 3, new SpreadSheetItem("Currency")); - table->item(0, 3)->setBackgroundColor(titleBackground); + table->item(0, 3)->setBackground(titleBackground); table->item(0, 3)->setToolTip("This column shows the currency"); table->item(0, 3)->setFont(titleFont); @@ -564,11 +560,11 @@ void SpreadSheet::setupContents() table->setItem(8, 3, new SpreadSheetItem("USD")); table->setItem(9, 3, new SpreadSheetItem()); - table->item(9,3)->setBackgroundColor(Qt::lightGray); + table->item(9, 3)->setBackground(Qt::lightGray); // column 4 table->setItem(0, 4, new SpreadSheetItem("Ex. Rate")); - table->item(0, 4)->setBackgroundColor(titleBackground); + table->item(0, 4)->setBackground(titleBackground); table->item(0, 4)->setToolTip("This column shows the exchange rate to NOK"); table->item(0, 4)->setFont(titleFont); @@ -582,11 +578,11 @@ void SpreadSheet::setupContents() table->setItem(8, 4, new SpreadSheetItem("7")); table->setItem(9, 4, new SpreadSheetItem()); - table->item(9,4)->setBackgroundColor(Qt::lightGray); + table->item(9, 4)->setBackground(titleBackground); // column 5 table->setItem(0, 5, new SpreadSheetItem("NOK")); - table->item(0, 5)->setBackgroundColor(titleBackground); + table->item(0, 5)->setBackground(titleBackground); table->item(0, 5)->setToolTip("This column shows the expenses in NOK"); table->item(0, 5)->setFont(titleFont); @@ -600,7 +596,7 @@ void SpreadSheet::setupContents() table->setItem(8, 5, new SpreadSheetItem("* C9 E9")); table->setItem(9, 5, new SpreadSheetItem("sum F2 F9")); - table->item(9,5)->setBackgroundColor(Qt::lightGray); + table->item(9, 5)->setBackground(titleBackground); } const char *htmlText = diff --git a/examples/widgets/itemviews/spreadsheet/spreadsheet.h b/examples/widgets/itemviews/spreadsheet/spreadsheet.h index a90d1d8c20..b36a377156 100644 --- a/examples/widgets/itemviews/spreadsheet/spreadsheet.h +++ b/examples/widgets/itemviews/spreadsheet/spreadsheet.h @@ -65,10 +65,8 @@ QT_END_NAMESPACE class SpreadSheet : public QMainWindow { Q_OBJECT - public: - - SpreadSheet(int rows, int cols, QWidget *parent = 0); + SpreadSheet(int rows, int cols, QWidget *parent = nullptr); public slots: void updateStatus(QTableWidgetItem *item); diff --git a/examples/widgets/itemviews/spreadsheet/spreadsheetdelegate.cpp b/examples/widgets/itemviews/spreadsheet/spreadsheetdelegate.cpp index aaf8902af5..eadd5fadb8 100644 --- a/examples/widgets/itemviews/spreadsheet/spreadsheetdelegate.cpp +++ b/examples/widgets/itemviews/spreadsheet/spreadsheetdelegate.cpp @@ -53,11 +53,12 @@ #include <QtWidgets> SpreadSheetDelegate::SpreadSheetDelegate(QObject *parent) - : QItemDelegate(parent) {} + : QItemDelegate(parent) +{} QWidget *SpreadSheetDelegate::createEditor(QWidget *parent, - const QStyleOptionViewItem &, - const QModelIndex &index) const + const QStyleOptionViewItem &, + const QModelIndex &index) const { if (index.column() == 1) { QDateTimeEdit *editor = new QDateTimeEdit(parent); @@ -92,7 +93,7 @@ void SpreadSheetDelegate::commitAndCloseEditor() } void SpreadSheetDelegate::setEditorData(QWidget *editor, - const QModelIndex &index) const + const QModelIndex &index) const { QLineEdit *edit = qobject_cast<QLineEdit*>(editor); if (edit) { @@ -109,7 +110,8 @@ void SpreadSheetDelegate::setEditorData(QWidget *editor, } void SpreadSheetDelegate::setModelData(QWidget *editor, - QAbstractItemModel *model, const QModelIndex &index) const + QAbstractItemModel *model, + const QModelIndex &index) const { QLineEdit *edit = qobject_cast<QLineEdit *>(editor); if (edit) { diff --git a/examples/widgets/itemviews/spreadsheet/spreadsheetdelegate.h b/examples/widgets/itemviews/spreadsheet/spreadsheetdelegate.h index 333467720f..c89459cadf 100644 --- a/examples/widgets/itemviews/spreadsheet/spreadsheetdelegate.h +++ b/examples/widgets/itemviews/spreadsheet/spreadsheetdelegate.h @@ -51,8 +51,6 @@ #ifndef SPREADSHEETDELEGATE_H #define SPREADSHEETDELEGATE_H -#include "spreadsheet.h" - #include <QItemDelegate> class SpreadSheetDelegate : public QItemDelegate @@ -60,7 +58,7 @@ class SpreadSheetDelegate : public QItemDelegate Q_OBJECT public: - SpreadSheetDelegate(QObject *parent = 0); + SpreadSheetDelegate(QObject *parent = nullptr); QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &, const QModelIndex &index) const override; void setEditorData(QWidget *editor, const QModelIndex &index) const override; diff --git a/examples/widgets/itemviews/spreadsheet/spreadsheetitem.cpp b/examples/widgets/itemviews/spreadsheet/spreadsheetitem.cpp index 4c18fa8251..62d3a6fdcd 100644 --- a/examples/widgets/itemviews/spreadsheet/spreadsheetitem.cpp +++ b/examples/widgets/itemviews/spreadsheet/spreadsheetitem.cpp @@ -50,19 +50,9 @@ #include "spreadsheetitem.h" -SpreadSheetItem::SpreadSheetItem() - : QTableWidgetItem(), isResolving(false) -{ -} - -SpreadSheetItem::SpreadSheetItem(const QString &text) - : QTableWidgetItem(text), isResolving(false) -{ -} - QTableWidgetItem *SpreadSheetItem::clone() const { - SpreadSheetItem *item = new SpreadSheetItem(); + SpreadSheetItem *item = new SpreadSheetItem; *item = *this; return item; } @@ -75,21 +65,20 @@ QVariant SpreadSheetItem::data(int role) const if (role == Qt::DisplayRole) return display(); - QString t = display().toString(); - bool isNumber = false; - int number = t.toInt(&isNumber); + const QString t = display().toString(); - if (role == Qt::TextColorRole) { - if (!isNumber) - return QVariant::fromValue(QColor(Qt::black)); - else if (number < 0) - return QVariant::fromValue(QColor(Qt::red)); - return QVariant::fromValue(QColor(Qt::blue)); + if (role == Qt::ForegroundRole) { + bool isNumber = false; + const int number = t.toInt(&isNumber); + QColor color = Qt::black; + if (isNumber) + color = (number < 0) ? Qt::red : Qt::blue; + return QVariant::fromValue(color); } - if (role == Qt::TextAlignmentRole) - if (!t.isEmpty() && (t.at(0).isNumber() || t.at(0) == '-')) - return (int)(Qt::AlignRight | Qt::AlignVCenter); + if (role == Qt::TextAlignmentRole) + if (!t.isEmpty() && (t.at(0).isNumber() || t.at(0) == '-')) + return int(Qt::AlignRight | Qt::AlignVCenter); return QTableWidgetItem::data(role); } diff --git a/examples/widgets/itemviews/spreadsheet/spreadsheetitem.h b/examples/widgets/itemviews/spreadsheet/spreadsheetitem.h index 46460df527..159f4a7eea 100644 --- a/examples/widgets/itemviews/spreadsheet/spreadsheetitem.h +++ b/examples/widgets/itemviews/spreadsheet/spreadsheetitem.h @@ -58,8 +58,7 @@ class SpreadSheetItem : public QTableWidgetItem { public: - SpreadSheetItem(); - SpreadSheetItem(const QString &text); + using QTableWidgetItem::QTableWidgetItem; QTableWidgetItem *clone() const override; @@ -74,10 +73,10 @@ public: static QVariant computeFormula(const QString &formula, const QTableWidget *widget, - const QTableWidgetItem *self = 0); + const QTableWidgetItem *self = nullptr); private: - mutable bool isResolving; + mutable bool isResolving = false; }; #endif // SPREADSHEETITEM_H |