Revamp SQL examples to C++11
Changed signals and slots to new syntax, used nullptr and replaced foreach with new C++11 range based for loops. Also fixed a few minor flaws. Task-number: QTBUG-60633 Change-Id: Ice4030133971912f96752d9d84c638c70fd73e35 Reviewed-by: Jesus Fernandez <Jesus.Fernandez@qt.io> Reviewed-by: André Hartmann <aha_1980@gmx.de> Reviewed-by: Friedemann Kleint <Friedemann.Kleint@qt.io>
This commit is contained in:
parent
590e71a69c
commit
3d67793a9e
@ -85,9 +85,9 @@ TableEditor::TableEditor(const QString &tableName, QWidget *parent)
|
|||||||
//! [2]
|
//! [2]
|
||||||
|
|
||||||
//! [3]
|
//! [3]
|
||||||
connect(submitButton, SIGNAL(clicked()), this, SLOT(submit()));
|
connect(submitButton, &QPushButton::clicked, this, &TableEditor::submit);
|
||||||
connect(revertButton, SIGNAL(clicked()), model, SLOT(revertAll()));
|
connect(revertButton, &QPushButton::clicked, model, &QSqlTableModel::revertAll);
|
||||||
connect(quitButton, SIGNAL(clicked()), this, SLOT(close()));
|
connect(quitButton, &QPushButton::clicked, this, &TableEditor::close);
|
||||||
//! [3]
|
//! [3]
|
||||||
|
|
||||||
//! [4]
|
//! [4]
|
||||||
|
@ -65,7 +65,7 @@ class TableEditor : public QWidget
|
|||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
|
|
||||||
public:
|
public:
|
||||||
explicit TableEditor(const QString &tableName, QWidget *parent = 0);
|
explicit TableEditor(const QString &tableName, QWidget *parent = nullptr);
|
||||||
|
|
||||||
private slots:
|
private slots:
|
||||||
void submit();
|
void submit();
|
||||||
|
@ -70,12 +70,12 @@ static bool createConnection()
|
|||||||
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
|
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
|
||||||
db.setDatabaseName(":memory:");
|
db.setDatabaseName(":memory:");
|
||||||
if (!db.open()) {
|
if (!db.open()) {
|
||||||
QMessageBox::critical(0, qApp->tr("Cannot open database"),
|
QMessageBox::critical(nullptr, QObject::tr("Cannot open database"),
|
||||||
qApp->tr("Unable to establish a database connection.\n"
|
QObject::tr("Unable to establish a database connection.\n"
|
||||||
"This example needs SQLite support. Please read "
|
"This example needs SQLite support. Please read "
|
||||||
"the Qt SQL driver documentation for information how "
|
"the Qt SQL driver documentation for information how "
|
||||||
"to build it.\n\n"
|
"to build it.\n\n"
|
||||||
"Click Cancel to exit."), QMessageBox::Cancel);
|
"Click Cancel to exit."), QMessageBox::Cancel);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -172,11 +172,20 @@
|
|||||||
\snippet drilldown/informationwindow.cpp 4
|
\snippet drilldown/informationwindow.cpp 4
|
||||||
|
|
||||||
Finally, we connect the "something's changed" signals in the
|
Finally, we connect the "something's changed" signals in the
|
||||||
editors to our custom \c enableButtons() slot, enabling the users
|
editors to our custom \c enableButtons slot, enabling the users
|
||||||
to either submit or revert their changes. We add all the widgets
|
to either submit or revert their changes.
|
||||||
into a layout, store the item ID and the name of the displayed
|
We need to use lambdas for connecting the \c enableButtons slot
|
||||||
image file for future reference, and set the window title and
|
because its signature does not match \c QTextEdit::textChanged
|
||||||
initial size.
|
and \c QComboBox::currentIndexChanged.
|
||||||
|
Since the latter has another overload with the signature
|
||||||
|
\c {const QString &} and the selected signal would be ambiguous,
|
||||||
|
we need to use \c QOverload<int>::of to select a specific overload
|
||||||
|
for \c currentIndexChanged.
|
||||||
|
|
||||||
|
We add all the widgets into a layout, store the item ID and the
|
||||||
|
name of the displayed image file for future reference, and set
|
||||||
|
the window title and initial size.
|
||||||
|
|
||||||
|
|
||||||
Note that we also set the Qt::Window window flag to indicate that
|
Note that we also set the Qt::Window window flag to indicate that
|
||||||
our widget is in fact a window, with a window system frame and a
|
our widget is in fact a window, with a window system frame and a
|
||||||
@ -389,19 +398,21 @@
|
|||||||
\snippet drilldown/view.cpp 6
|
\snippet drilldown/view.cpp 6
|
||||||
|
|
||||||
The \c showInformation() function is given an \c ImageItem object
|
The \c showInformation() function is given an \c ImageItem object
|
||||||
as argument, and starts off by extracting the item's item
|
as argument, and starts off by extracting the item's item ID.
|
||||||
ID. Then it determines if there already is created an information
|
|
||||||
window for this location. If it is, and the window is visible, it
|
|
||||||
ensures that the window is raised to the top of the widget stack
|
|
||||||
and activated. If the window exists but is hidden, calling its \l
|
|
||||||
{QWidget::}{show()} slot gives the same result.
|
|
||||||
|
|
||||||
|
Then it determines if there already is created an information
|
||||||
|
window for this location.
|
||||||
If no window for the given location exists, we create one by
|
If no window for the given location exists, we create one by
|
||||||
passing the item ID, a pointer to the model, and our view as a
|
passing the item ID, a pointer to the model, and our view as a
|
||||||
parent, to the \c InformationWindow constructor. Note that we
|
parent, to the \c InformationWindow constructor. Note that we
|
||||||
connect the information window's \c imageChanged() signal to \e
|
connect the information window's \c imageChanged() signal to \e
|
||||||
this widget's \c updateImage() slot, before we give it a suitable
|
this widget's \c updateImage() slot, before we give it a suitable
|
||||||
position and add it to the list of existing windows.
|
position and add it to the list of existing windows.
|
||||||
|
If there is a window for the given location, and that window is
|
||||||
|
visible, it ensures that the window is raised to the top of the
|
||||||
|
widget stack and activated. If it is hidden, calling its \l
|
||||||
|
{QWidget::}{show()} slot gives the same result.
|
||||||
|
|
||||||
|
|
||||||
\snippet drilldown/view.cpp 7
|
\snippet drilldown/view.cpp 7
|
||||||
|
|
||||||
|
@ -60,8 +60,8 @@ ImageItem::ImageItem(int id, const QPixmap &pixmap, QGraphicsItem *parent)
|
|||||||
timeLine.setDuration(150);
|
timeLine.setDuration(150);
|
||||||
timeLine.setFrameRange(0, 150);
|
timeLine.setFrameRange(0, 150);
|
||||||
|
|
||||||
connect(&timeLine, SIGNAL(frameChanged(int)), this, SLOT(setFrame(int)));
|
connect(&timeLine, &QTimeLine::frameChanged, this, &ImageItem::setFrame);
|
||||||
connect(&timeLine, SIGNAL(finished()), this, SLOT(updateItemPosition()));
|
connect(&timeLine, &QTimeLine::finished, this, &ImageItem::updateItemPosition);
|
||||||
|
|
||||||
adjust();
|
adjust();
|
||||||
}
|
}
|
||||||
@ -116,7 +116,7 @@ void ImageItem::adjust()
|
|||||||
//! [4]
|
//! [4]
|
||||||
|
|
||||||
//! [5]
|
//! [5]
|
||||||
int ImageItem::id()
|
int ImageItem::id() const
|
||||||
{
|
{
|
||||||
return recordId;
|
return recordId;
|
||||||
}
|
}
|
||||||
|
@ -60,10 +60,10 @@ class ImageItem : public QObject, public QGraphicsPixmapItem
|
|||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
|
|
||||||
public:
|
public:
|
||||||
ImageItem(int id, const QPixmap &pixmap, QGraphicsItem *parent = 0);
|
ImageItem(int id, const QPixmap &pixmap, QGraphicsItem *parent = nullptr);
|
||||||
|
|
||||||
void adjust();
|
void adjust();
|
||||||
int id();
|
int id() const;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
void hoverEnterEvent(QGraphicsSceneHoverEvent *event) override;
|
void hoverEnterEvent(QGraphicsSceneHoverEvent *event) override;
|
||||||
|
@ -84,10 +84,12 @@ InformationWindow::InformationWindow(int id, QSqlRelationalTableModel *items,
|
|||||||
//! [3]
|
//! [3]
|
||||||
|
|
||||||
//! [4]
|
//! [4]
|
||||||
connect(descriptionEditor, SIGNAL(textChanged()),
|
connect(descriptionEditor, &QTextEdit::textChanged, [=]() {
|
||||||
this, SLOT(enableButtons()));
|
enableButtons();
|
||||||
connect(imageFileEditor, SIGNAL(currentIndexChanged(int)),
|
});
|
||||||
this, SLOT(enableButtons()));
|
connect(imageFileEditor, QOverload<int>::of(&QComboBox::currentIndexChanged), [=]() {
|
||||||
|
enableButtons();
|
||||||
|
});
|
||||||
|
|
||||||
QFormLayout *formLayout = new QFormLayout;
|
QFormLayout *formLayout = new QFormLayout;
|
||||||
formLayout->addRow(itemLabel, itemText);
|
formLayout->addRow(itemLabel, itemText);
|
||||||
@ -109,7 +111,7 @@ InformationWindow::InformationWindow(int id, QSqlRelationalTableModel *items,
|
|||||||
//! [4]
|
//! [4]
|
||||||
|
|
||||||
//! [5]
|
//! [5]
|
||||||
int InformationWindow::id()
|
int InformationWindow::id() const
|
||||||
{
|
{
|
||||||
return itemId;
|
return itemId;
|
||||||
}
|
}
|
||||||
@ -149,9 +151,9 @@ void InformationWindow::createButtons()
|
|||||||
|
|
||||||
closeButton->setDefault(true);
|
closeButton->setDefault(true);
|
||||||
|
|
||||||
connect(closeButton, SIGNAL(clicked()), this, SLOT(close()));
|
connect(closeButton, &QPushButton::clicked, this, &InformationWindow::close);
|
||||||
connect(revertButton, SIGNAL(clicked()), this, SLOT(revert()));
|
connect(revertButton, &QPushButton::clicked, this, &InformationWindow::revert);
|
||||||
connect(submitButton, SIGNAL(clicked()), this, SLOT(submit()));
|
connect(submitButton, &QPushButton::clicked, this, &InformationWindow::submit);
|
||||||
//! [8]
|
//! [8]
|
||||||
|
|
||||||
//! [9]
|
//! [9]
|
||||||
|
@ -61,9 +61,9 @@ class InformationWindow : public QDialog
|
|||||||
|
|
||||||
public:
|
public:
|
||||||
InformationWindow(int id, QSqlRelationalTableModel *items,
|
InformationWindow(int id, QSqlRelationalTableModel *items,
|
||||||
QWidget *parent = 0);
|
QWidget *parent = nullptr);
|
||||||
|
|
||||||
int id();
|
int id() const;
|
||||||
|
|
||||||
signals:
|
signals:
|
||||||
void imageChanged(int id, const QString &fileName);
|
void imageChanged(int id, const QString &fileName);
|
||||||
@ -83,16 +83,16 @@ private:
|
|||||||
int itemId;
|
int itemId;
|
||||||
QString displayedImage;
|
QString displayedImage;
|
||||||
|
|
||||||
QComboBox *imageFileEditor;
|
QComboBox *imageFileEditor = nullptr;
|
||||||
QLabel *itemText;
|
QLabel *itemText = nullptr;
|
||||||
QTextEdit *descriptionEditor;
|
QTextEdit *descriptionEditor = nullptr;
|
||||||
|
|
||||||
QPushButton *closeButton;
|
QPushButton *closeButton = nullptr;
|
||||||
QPushButton *submitButton;
|
QPushButton *submitButton = nullptr;
|
||||||
QPushButton *revertButton;
|
QPushButton *revertButton = nullptr;
|
||||||
QDialogButtonBox *buttonBox;
|
QDialogButtonBox *buttonBox = nullptr;
|
||||||
|
|
||||||
QDataWidgetMapper *mapper;
|
QDataWidgetMapper *mapper = nullptr;
|
||||||
};
|
};
|
||||||
//! [2]
|
//! [2]
|
||||||
|
|
||||||
|
@ -48,10 +48,13 @@
|
|||||||
**
|
**
|
||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
|
|
||||||
#include <QtWidgets>
|
|
||||||
|
|
||||||
#include "../connection.h"
|
|
||||||
#include "view.h"
|
#include "view.h"
|
||||||
|
#include "../connection.h"
|
||||||
|
|
||||||
|
#include <QApplication>
|
||||||
|
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
|
||||||
int main(int argc, char *argv[])
|
int main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
@ -60,7 +63,7 @@ int main(int argc, char *argv[])
|
|||||||
QApplication app(argc, argv);
|
QApplication app(argc, argv);
|
||||||
|
|
||||||
if (!createConnection())
|
if (!createConnection())
|
||||||
return 1;
|
return EXIT_FAILURE;
|
||||||
|
|
||||||
View view("items", "images");
|
View view("items", "images");
|
||||||
view.show();
|
view.show();
|
||||||
|
@ -89,8 +89,6 @@ void View::addItems()
|
|||||||
int topMargin = 40;
|
int topMargin = 40;
|
||||||
|
|
||||||
for (int i = 0; i < itemCount; i++) {
|
for (int i = 0; i < itemCount; i++) {
|
||||||
ImageItem *image;
|
|
||||||
QGraphicsTextItem *label;
|
|
||||||
QSqlRecord record = itemTable->record(i);
|
QSqlRecord record = itemTable->record(i);
|
||||||
|
|
||||||
int id = record.value("id").toInt();
|
int id = record.value("id").toInt();
|
||||||
@ -101,12 +99,12 @@ void View::addItems()
|
|||||||
int x = ((i % 2) * imageOffset) + leftMargin + columnOffset;
|
int x = ((i % 2) * imageOffset) + leftMargin + columnOffset;
|
||||||
int y = ((i / 2) * imageOffset) + topMargin;
|
int y = ((i / 2) * imageOffset) + topMargin;
|
||||||
|
|
||||||
image = new ImageItem(id, QPixmap(":/" + file));
|
ImageItem *image = new ImageItem(id, QPixmap(":/" + file));
|
||||||
image->setData(0, i);
|
image->setData(0, i);
|
||||||
image->setPos(x, y);
|
image->setPos(x, y);
|
||||||
scene->addItem(image);
|
scene->addItem(image);
|
||||||
|
|
||||||
label = scene->addText(item);
|
QGraphicsTextItem *label = scene->addText(item);
|
||||||
label->setDefaultTextColor(QColor("#d7d6d5"));
|
label->setDefaultTextColor(QColor("#d7d6d5"));
|
||||||
QPointF labelOffset((120 - label->boundingRect().width()) / 2, 120.0);
|
QPointF labelOffset((120 - label->boundingRect().width()) / 2, 120.0);
|
||||||
label->setPos(QPointF(x, y) + labelOffset);
|
label->setPos(QPointF(x, y) + labelOffset);
|
||||||
@ -133,22 +131,22 @@ void View::showInformation(ImageItem *image)
|
|||||||
return;
|
return;
|
||||||
|
|
||||||
InformationWindow *window = findWindow(id);
|
InformationWindow *window = findWindow(id);
|
||||||
if (window && window->isVisible()) {
|
if (!window) {
|
||||||
window->raise();
|
|
||||||
window->activateWindow();
|
|
||||||
} else if (window && !window->isVisible()) {
|
|
||||||
window->show();
|
|
||||||
} else {
|
|
||||||
InformationWindow *window;
|
|
||||||
window = new InformationWindow(id, itemTable, this);
|
window = new InformationWindow(id, itemTable, this);
|
||||||
|
|
||||||
connect(window, SIGNAL(imageChanged(int,QString)),
|
connect(window, QOverload<int,const QString &>::of(&InformationWindow::imageChanged),
|
||||||
this, SLOT(updateImage(int,QString)));
|
this, QOverload<int,const QString &>::of(&View::updateImage));
|
||||||
|
|
||||||
window->move(pos() + QPoint(20, 40));
|
window->move(pos() + QPoint(20, 40));
|
||||||
window->show();
|
window->show();
|
||||||
informationWindows.append(window);
|
informationWindows.append(window);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (window->isVisible()) {
|
||||||
|
window->raise();
|
||||||
|
window->activateWindow();
|
||||||
|
} else
|
||||||
|
window->show();
|
||||||
}
|
}
|
||||||
//! [6]
|
//! [6]
|
||||||
|
|
||||||
@ -172,19 +170,13 @@ void View::updateImage(int id, const QString &fileName)
|
|||||||
//! [7]
|
//! [7]
|
||||||
|
|
||||||
//! [8]
|
//! [8]
|
||||||
InformationWindow* View::findWindow(int id)
|
InformationWindow *View::findWindow(int id) const
|
||||||
{
|
{
|
||||||
QList<InformationWindow*>::iterator i, beginning, end;
|
for (auto window : informationWindows) {
|
||||||
|
|
||||||
beginning = informationWindows.begin();
|
|
||||||
end = informationWindows.end();
|
|
||||||
|
|
||||||
for (i = beginning; i != end; ++i) {
|
|
||||||
InformationWindow *window = (*i);
|
|
||||||
if (window && (window->id() == id))
|
if (window && (window->id() == id))
|
||||||
return window;
|
return window;
|
||||||
}
|
}
|
||||||
return 0;
|
return nullptr;
|
||||||
}
|
}
|
||||||
//! [8]
|
//! [8]
|
||||||
|
|
||||||
|
@ -63,7 +63,7 @@ class View : public QGraphicsView
|
|||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
|
|
||||||
public:
|
public:
|
||||||
View(const QString &items, const QString &images, QWidget *parent = 0);
|
View(const QString &items, const QString &images, QWidget *parent = nullptr);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
void mouseReleaseEvent(QMouseEvent *event) override;
|
void mouseReleaseEvent(QMouseEvent *event) override;
|
||||||
@ -77,7 +77,7 @@ private slots:
|
|||||||
//! [2]
|
//! [2]
|
||||||
private:
|
private:
|
||||||
void addItems();
|
void addItems();
|
||||||
InformationWindow* findWindow(int id);
|
InformationWindow *findWindow(int id) const;
|
||||||
void showInformation(ImageItem *image);
|
void showInformation(ImageItem *image);
|
||||||
|
|
||||||
QGraphicsScene *scene;
|
QGraphicsScene *scene;
|
||||||
|
@ -61,12 +61,12 @@ static bool createConnection()
|
|||||||
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
|
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
|
||||||
db.setDatabaseName(":memory:");
|
db.setDatabaseName(":memory:");
|
||||||
if (!db.open()) {
|
if (!db.open()) {
|
||||||
QMessageBox::critical(0, qApp->tr("Cannot open database"),
|
QMessageBox::critical(nullptr, QObject::tr("Cannot open database"),
|
||||||
qApp->tr("Unable to establish a database connection.\n"
|
QObject::tr("Unable to establish a database connection.\n"
|
||||||
"This example needs SQLite support. Please read "
|
"This example needs SQLite support. Please read "
|
||||||
"the Qt SQL driver documentation for information how "
|
"the Qt SQL driver documentation for information how "
|
||||||
"to build it.\n\n"
|
"to build it.\n\n"
|
||||||
"Click Cancel to exit."), QMessageBox::Cancel);
|
"Click Cancel to exit."), QMessageBox::Cancel);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -155,12 +155,12 @@ int Dialog::addNewAlbum(const QString &title, int artistId)
|
|||||||
return id;
|
return id;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Dialog::addTracks(int albumId, QStringList tracks)
|
void Dialog::addTracks(int albumId, const QStringList &tracks)
|
||||||
{
|
{
|
||||||
QDomElement albumNode = albumDetails.createElement("album");
|
QDomElement albumNode = albumDetails.createElement("album");
|
||||||
albumNode.setAttribute("id", albumId);
|
albumNode.setAttribute("id", albumId);
|
||||||
|
|
||||||
for (int i = 0; i < tracks.count(); i++) {
|
for (int i = 0; i < tracks.count(); ++i) {
|
||||||
QString trackNumber = QString::number(i);
|
QString trackNumber = QString::number(i);
|
||||||
if (i < 10)
|
if (i < 10)
|
||||||
trackNumber.prepend('0');
|
trackNumber.prepend('0');
|
||||||
@ -254,9 +254,9 @@ QDialogButtonBox *Dialog::createButtons()
|
|||||||
|
|
||||||
closeButton->setDefault(true);
|
closeButton->setDefault(true);
|
||||||
|
|
||||||
connect(closeButton, SIGNAL(clicked()), this, SLOT(close()));
|
connect(closeButton, &QPushButton::clicked, this, &Dialog::close);
|
||||||
connect(revertButton, SIGNAL(clicked()), this, SLOT(revert()));
|
connect(revertButton, &QPushButton::clicked, this, &Dialog::revert);
|
||||||
connect(submitButton, SIGNAL(clicked()), this, SLOT(submit()));
|
connect(submitButton, &QPushButton::clicked, this, &Dialog::submit);
|
||||||
|
|
||||||
QDialogButtonBox *buttonBox = new QDialogButtonBox;
|
QDialogButtonBox *buttonBox = new QDialogButtonBox;
|
||||||
buttonBox->addButton(submitButton, QDialogButtonBox::ResetRole);
|
buttonBox->addButton(submitButton, QDialogButtonBox::ResetRole);
|
||||||
@ -270,7 +270,7 @@ QModelIndex Dialog::indexOfArtist(const QString &artist)
|
|||||||
{
|
{
|
||||||
QSqlTableModel *artistModel = model->relationModel(2);
|
QSqlTableModel *artistModel = model->relationModel(2);
|
||||||
|
|
||||||
for (int i = 0; i < artistModel->rowCount(); i++) {
|
for (int i = 0; i < artistModel->rowCount(); ++i) {
|
||||||
QSqlRecord record = artistModel->record(i);
|
QSqlRecord record = artistModel->record(i);
|
||||||
if (record.value("artist") == artist)
|
if (record.value("artist") == artist)
|
||||||
return artistModel->index(i, 1);
|
return artistModel->index(i, 1);
|
||||||
|
@ -61,7 +61,7 @@ class Dialog : public QDialog
|
|||||||
|
|
||||||
public:
|
public:
|
||||||
Dialog(QSqlRelationalTableModel *albums, QDomDocument details,
|
Dialog(QSqlRelationalTableModel *albums, QDomDocument details,
|
||||||
QFile *output, QWidget *parent = 0);
|
QFile *output, QWidget *parent = nullptr);
|
||||||
|
|
||||||
private slots:
|
private slots:
|
||||||
void revert();
|
void revert();
|
||||||
@ -70,12 +70,12 @@ private slots:
|
|||||||
private:
|
private:
|
||||||
int addNewAlbum(const QString &title, int artistId);
|
int addNewAlbum(const QString &title, int artistId);
|
||||||
int addNewArtist(const QString &name);
|
int addNewArtist(const QString &name);
|
||||||
void addTracks(int albumId, QStringList tracks);
|
void addTracks(int albumId, const QStringList &tracks);
|
||||||
QDialogButtonBox *createButtons();
|
QDialogButtonBox *createButtons();
|
||||||
QGroupBox *createInputWidgets();
|
QGroupBox *createInputWidgets();
|
||||||
int findArtistId(const QString &artist);
|
int findArtistId(const QString &artist);
|
||||||
int generateAlbumId();
|
static int generateAlbumId();
|
||||||
int generateArtistId();
|
static int generateArtistId();
|
||||||
void increaseAlbumCount(QModelIndex artistIndex);
|
void increaseAlbumCount(QModelIndex artistIndex);
|
||||||
QModelIndex indexOfArtist(const QString &artist);
|
QModelIndex indexOfArtist(const QString &artist);
|
||||||
|
|
||||||
|
@ -48,11 +48,14 @@
|
|||||||
**
|
**
|
||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
|
|
||||||
#include <QtWidgets>
|
|
||||||
|
|
||||||
#include "database.h"
|
#include "database.h"
|
||||||
#include "mainwindow.h"
|
#include "mainwindow.h"
|
||||||
|
|
||||||
|
#include <QApplication>
|
||||||
|
#include <QFile>
|
||||||
|
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
int main(int argc, char *argv[])
|
int main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
Q_INIT_RESOURCE(masterdetail);
|
Q_INIT_RESOURCE(masterdetail);
|
||||||
@ -60,10 +63,10 @@ int main(int argc, char *argv[])
|
|||||||
QApplication app(argc, argv);
|
QApplication app(argc, argv);
|
||||||
|
|
||||||
if (!createConnection())
|
if (!createConnection())
|
||||||
return 1;
|
return EXIT_FAILURE;
|
||||||
|
|
||||||
QFile *albumDetails = new QFile("albumdetails.xml");
|
QFile albumDetails("albumdetails.xml");
|
||||||
MainWindow window("artists", "albums", albumDetails);
|
MainWindow window("artists", "albums", &albumDetails);
|
||||||
window.show();
|
window.show();
|
||||||
return app.exec();
|
return app.exec();
|
||||||
}
|
}
|
||||||
|
@ -78,10 +78,10 @@ MainWindow::MainWindow(const QString &artistTable, const QString &albumTable,
|
|||||||
uniqueAlbumId = model->rowCount();
|
uniqueAlbumId = model->rowCount();
|
||||||
uniqueArtistId = artistView->count();
|
uniqueArtistId = artistView->count();
|
||||||
|
|
||||||
connect(model, SIGNAL(rowsInserted(QModelIndex,int,int)),
|
connect(model, &QSqlRelationalTableModel::rowsInserted,
|
||||||
this, SLOT(updateHeader(QModelIndex,int,int)));
|
this, &MainWindow::updateHeader);
|
||||||
connect(model, SIGNAL(rowsRemoved(QModelIndex,int,int)),
|
connect(model, &QSqlRelationalTableModel::rowsRemoved,
|
||||||
this, SLOT(updateHeader(QModelIndex,int,int)));
|
this, &MainWindow::updateHeader);
|
||||||
|
|
||||||
QGridLayout *layout = new QGridLayout;
|
QGridLayout *layout = new QGridLayout;
|
||||||
layout->addWidget(artists, 0, 0);
|
layout->addWidget(artists, 0, 0);
|
||||||
@ -145,7 +145,7 @@ void MainWindow::showAlbumDetails(QModelIndex index)
|
|||||||
titleLabel->show();
|
titleLabel->show();
|
||||||
|
|
||||||
QDomNodeList albums = albumData.elementsByTagName("album");
|
QDomNodeList albums = albumData.elementsByTagName("album");
|
||||||
for (int i = 0; i < albums.count(); i++) {
|
for (int i = 0; i < albums.count(); ++i) {
|
||||||
QDomNode album = albums.item(i);
|
QDomNode album = albums.item(i);
|
||||||
if (album.toElement().attribute("id") == albumId) {
|
if (album.toElement().attribute("id") == albumId) {
|
||||||
getTrackList(album.toElement());
|
getTrackList(album.toElement());
|
||||||
@ -164,9 +164,9 @@ void MainWindow::getTrackList(QDomNode album)
|
|||||||
QDomNode track;
|
QDomNode track;
|
||||||
QString trackNumber;
|
QString trackNumber;
|
||||||
|
|
||||||
for (int j = 0; j < tracks.count(); j++) {
|
for (int i = 0; i < tracks.count(); ++i) {
|
||||||
|
|
||||||
track = tracks.item(j);
|
track = tracks.item(i);
|
||||||
trackNumber = track.toElement().attribute("number");
|
trackNumber = track.toElement().attribute("number");
|
||||||
|
|
||||||
QListWidgetItem *item = new QListWidgetItem(trackList);
|
QListWidgetItem *item = new QListWidgetItem(trackList);
|
||||||
@ -222,7 +222,7 @@ void MainWindow::removeAlbumFromFile(int id)
|
|||||||
|
|
||||||
QDomNodeList albums = albumData.elementsByTagName("album");
|
QDomNodeList albums = albumData.elementsByTagName("album");
|
||||||
|
|
||||||
for (int i = 0; i < albums.count(); i++) {
|
for (int i = 0; i < albums.count(); ++i) {
|
||||||
QDomNode node = albums.item(i);
|
QDomNode node = albums.item(i);
|
||||||
if (node.toElement().attribute("id").toInt() == id) {
|
if (node.toElement().attribute("id").toInt() == id) {
|
||||||
albumData.elementsByTagName("archive").item(0).removeChild(node);
|
albumData.elementsByTagName("archive").item(0).removeChild(node);
|
||||||
@ -283,8 +283,8 @@ QGroupBox* MainWindow::createArtistGroupBox()
|
|||||||
artistView->setModel(model->relationModel(2));
|
artistView->setModel(model->relationModel(2));
|
||||||
artistView->setModelColumn(1);
|
artistView->setModelColumn(1);
|
||||||
|
|
||||||
connect(artistView, SIGNAL(currentIndexChanged(int)),
|
connect(artistView, QOverload<int>::of(&QComboBox::currentIndexChanged),
|
||||||
this, SLOT(changeArtist(int)));
|
this, &MainWindow::changeArtist);
|
||||||
|
|
||||||
QGroupBox *box = new QGroupBox(tr("Artist"));
|
QGroupBox *box = new QGroupBox(tr("Artist"));
|
||||||
|
|
||||||
@ -314,10 +314,10 @@ QGroupBox* MainWindow::createAlbumGroupBox()
|
|||||||
locale.setNumberOptions(QLocale::OmitGroupSeparator);
|
locale.setNumberOptions(QLocale::OmitGroupSeparator);
|
||||||
albumView->setLocale(locale);
|
albumView->setLocale(locale);
|
||||||
|
|
||||||
connect(albumView, SIGNAL(clicked(QModelIndex)),
|
connect(albumView, &QTableView::clicked,
|
||||||
this, SLOT(showAlbumDetails(QModelIndex)));
|
this, &MainWindow::showAlbumDetails);
|
||||||
connect(albumView, SIGNAL(activated(QModelIndex)),
|
connect(albumView, &QTableView::activated,
|
||||||
this, SLOT(showAlbumDetails(QModelIndex)));
|
this, &MainWindow::showAlbumDetails);
|
||||||
|
|
||||||
QVBoxLayout *layout = new QVBoxLayout;
|
QVBoxLayout *layout = new QVBoxLayout;
|
||||||
layout->addWidget(albumView, 0, 0);
|
layout->addWidget(albumView, 0, 0);
|
||||||
@ -383,11 +383,16 @@ void MainWindow::createMenuBar()
|
|||||||
helpMenu->addAction(aboutAction);
|
helpMenu->addAction(aboutAction);
|
||||||
helpMenu->addAction(aboutQtAction);
|
helpMenu->addAction(aboutQtAction);
|
||||||
|
|
||||||
connect(addAction, SIGNAL(triggered(bool)), this, SLOT(addAlbum()));
|
connect(addAction, &QAction::triggered,
|
||||||
connect(deleteAction, SIGNAL(triggered(bool)), this, SLOT(deleteAlbum()));
|
this, &MainWindow::addAlbum);
|
||||||
connect(quitAction, SIGNAL(triggered(bool)), this, SLOT(close()));
|
connect(deleteAction, &QAction::triggered,
|
||||||
connect(aboutAction, SIGNAL(triggered(bool)), this, SLOT(about()));
|
this, &MainWindow::deleteAlbum);
|
||||||
connect(aboutQtAction, SIGNAL(triggered(bool)), qApp, SLOT(aboutQt()));
|
connect(quitAction, &QAction::triggered,
|
||||||
|
this, &MainWindow::close);
|
||||||
|
connect(aboutAction, &QAction::triggered,
|
||||||
|
this, &MainWindow::about);
|
||||||
|
connect(aboutQtAction, &QAction::triggered,
|
||||||
|
qApp, &QApplication::aboutQt);
|
||||||
}
|
}
|
||||||
|
|
||||||
void MainWindow::showImageLabel()
|
void MainWindow::showImageLabel()
|
||||||
|
@ -71,7 +71,7 @@ class MainWindow : public QMainWindow
|
|||||||
|
|
||||||
public:
|
public:
|
||||||
MainWindow(const QString &artistTable, const QString &albumTable,
|
MainWindow(const QString &artistTable, const QString &albumTable,
|
||||||
QFile *albumDetails, QWidget *parent = 0);
|
QFile *albumDetails, QWidget *parent = nullptr);
|
||||||
|
|
||||||
private slots:
|
private slots:
|
||||||
void about();
|
void about();
|
||||||
|
@ -59,7 +59,7 @@ class CustomSqlModel : public QSqlQueryModel
|
|||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
|
|
||||||
public:
|
public:
|
||||||
CustomSqlModel(QObject *parent = 0);
|
CustomSqlModel(QObject *parent = nullptr);
|
||||||
|
|
||||||
QVariant data(const QModelIndex &item, int role) const override;
|
QVariant data(const QModelIndex &item, int role) const override;
|
||||||
};
|
};
|
||||||
|
@ -58,7 +58,7 @@ class EditableSqlModel : public QSqlQueryModel
|
|||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
|
|
||||||
public:
|
public:
|
||||||
EditableSqlModel(QObject *parent = 0);
|
EditableSqlModel(QObject *parent = nullptr);
|
||||||
|
|
||||||
Qt::ItemFlags flags(const QModelIndex &index) const override;
|
Qt::ItemFlags flags(const QModelIndex &index) const override;
|
||||||
bool setData(const QModelIndex &index, const QVariant &value, int role) override;
|
bool setData(const QModelIndex &index, const QVariant &value, int role) override;
|
||||||
|
@ -48,12 +48,15 @@
|
|||||||
**
|
**
|
||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
|
|
||||||
#include <QtWidgets>
|
|
||||||
|
|
||||||
#include "../connection.h"
|
#include "../connection.h"
|
||||||
#include "customsqlmodel.h"
|
#include "customsqlmodel.h"
|
||||||
#include "editablesqlmodel.h"
|
#include "editablesqlmodel.h"
|
||||||
|
|
||||||
|
#include <QApplication>
|
||||||
|
#include <QTableView>
|
||||||
|
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
void initializeModel(QSqlQueryModel *model)
|
void initializeModel(QSqlQueryModel *model)
|
||||||
{
|
{
|
||||||
model->setQuery("select * from person");
|
model->setQuery("select * from person");
|
||||||
@ -80,7 +83,7 @@ int main(int argc, char *argv[])
|
|||||||
{
|
{
|
||||||
QApplication app(argc, argv);
|
QApplication app(argc, argv);
|
||||||
if (!createConnection())
|
if (!createConnection())
|
||||||
return 1;
|
return EXIT_FAILURE;
|
||||||
|
|
||||||
QSqlQueryModel plainModel;
|
QSqlQueryModel plainModel;
|
||||||
EditableSqlModel editableModel;
|
EditableSqlModel editableModel;
|
||||||
|
@ -110,7 +110,8 @@ int main(int argc, char *argv[])
|
|||||||
{
|
{
|
||||||
QApplication app(argc, argv);
|
QApplication app(argc, argv);
|
||||||
if (!createConnection())
|
if (!createConnection())
|
||||||
return 1;
|
return EXIT_FAILURE;
|
||||||
|
|
||||||
createRelationalTables();
|
createRelationalTables();
|
||||||
|
|
||||||
QSqlRelationalTableModel model;
|
QSqlRelationalTableModel model;
|
||||||
|
@ -165,11 +165,12 @@ void Browser::showTable(const QString &t)
|
|||||||
model->select();
|
model->select();
|
||||||
if (model->lastError().type() != QSqlError::NoError)
|
if (model->lastError().type() != QSqlError::NoError)
|
||||||
emit statusMessage(model->lastError().text());
|
emit statusMessage(model->lastError().text());
|
||||||
|
|
||||||
table->setModel(model);
|
table->setModel(model);
|
||||||
table->setEditTriggers(QAbstractItemView::DoubleClicked|QAbstractItemView::EditKeyPressed);
|
table->setEditTriggers(QAbstractItemView::DoubleClicked|QAbstractItemView::EditKeyPressed);
|
||||||
|
connect(table->selectionModel(), &QItemSelectionModel::currentRowChanged,
|
||||||
|
this, &Browser::currentChanged);
|
||||||
|
|
||||||
connect(table->selectionModel(), SIGNAL(currentRowChanged(QModelIndex,QModelIndex)),
|
|
||||||
this, SLOT(currentChanged()));
|
|
||||||
updateActions();
|
updateActions();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -189,7 +190,6 @@ void Browser::showMetaData(const QString &t)
|
|||||||
model->setHeaderData(5, Qt::Horizontal, "AutoValue");
|
model->setHeaderData(5, Qt::Horizontal, "AutoValue");
|
||||||
model->setHeaderData(6, Qt::Horizontal, "DefaultValue");
|
model->setHeaderData(6, Qt::Horizontal, "DefaultValue");
|
||||||
|
|
||||||
|
|
||||||
for (int i = 0; i < rec.count(); ++i) {
|
for (int i = 0; i < rec.count(); ++i) {
|
||||||
QSqlField fld = rec.field(i);
|
QSqlField fld = rec.field(i);
|
||||||
model->setData(model->index(i, 0), fld.name());
|
model->setData(model->index(i, 0), fld.name());
|
||||||
|
@ -65,7 +65,7 @@ class Browser: public QWidget, private Ui::Browser
|
|||||||
{
|
{
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
public:
|
public:
|
||||||
Browser(QWidget *parent = 0);
|
Browser(QWidget *parent = nullptr);
|
||||||
virtual ~Browser();
|
virtual ~Browser();
|
||||||
|
|
||||||
QSqlError addConnection(const QString &driver, const QString &dbName, const QString &host,
|
QSqlError addConnection(const QString &driver, const QString &dbName, const QString &host,
|
||||||
@ -116,7 +116,9 @@ class CustomModel: public QSqlTableModel
|
|||||||
{
|
{
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
public:
|
public:
|
||||||
explicit CustomModel(QObject *parent = 0, QSqlDatabase db = QSqlDatabase()):QSqlTableModel(parent, db) {}
|
explicit CustomModel(QObject *parent = nullptr, QSqlDatabase db = QSqlDatabase())
|
||||||
|
: QSqlTableModel(parent, db) {}
|
||||||
|
|
||||||
QVariant data(const QModelIndex &idx, int role) const override
|
QVariant data(const QModelIndex &idx, int role) const override
|
||||||
{
|
{
|
||||||
if (role == Qt::BackgroundRole && isDirty(idx))
|
if (role == Qt::BackgroundRole && isDirty(idx))
|
||||||
|
@ -63,8 +63,8 @@ ConnectionWidget::ConnectionWidget(QWidget *parent)
|
|||||||
tree->header()->setSectionResizeMode(QHeaderView::Stretch);
|
tree->header()->setSectionResizeMode(QHeaderView::Stretch);
|
||||||
QAction *refreshAction = new QAction(tr("Refresh"), tree);
|
QAction *refreshAction = new QAction(tr("Refresh"), tree);
|
||||||
metaDataAction = new QAction(tr("Show Schema"), tree);
|
metaDataAction = new QAction(tr("Show Schema"), tree);
|
||||||
connect(refreshAction, SIGNAL(triggered()), SLOT(refresh()));
|
connect(refreshAction, &QAction::triggered, this, &ConnectionWidget::refresh);
|
||||||
connect(metaDataAction, SIGNAL(triggered()), SLOT(showMetaData()));
|
connect(metaDataAction, &QAction::triggered, this, &ConnectionWidget::showMetaData);
|
||||||
tree->addAction(refreshAction);
|
tree->addAction(refreshAction);
|
||||||
tree->addAction(metaDataAction);
|
tree->addAction(metaDataAction);
|
||||||
tree->setContextMenuPolicy(Qt::ActionsContextMenu);
|
tree->setContextMenuPolicy(Qt::ActionsContextMenu);
|
||||||
@ -146,7 +146,6 @@ void ConnectionWidget::setActive(QTreeWidgetItem *item)
|
|||||||
|
|
||||||
void ConnectionWidget::on_tree_itemActivated(QTreeWidgetItem *item, int /* column */)
|
void ConnectionWidget::on_tree_itemActivated(QTreeWidgetItem *item, int /* column */)
|
||||||
{
|
{
|
||||||
|
|
||||||
if (!item)
|
if (!item)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
@ -62,7 +62,7 @@ class ConnectionWidget: public QWidget
|
|||||||
{
|
{
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
public:
|
public:
|
||||||
ConnectionWidget(QWidget *parent = 0);
|
ConnectionWidget(QWidget *parent = nullptr);
|
||||||
virtual ~ConnectionWidget();
|
virtual ~ConnectionWidget();
|
||||||
|
|
||||||
QSqlDatabase currentDatabase() const;
|
QSqlDatabase currentDatabase() const;
|
||||||
|
@ -80,16 +80,18 @@ int main(int argc, char *argv[])
|
|||||||
mainWin.setCentralWidget(&browser);
|
mainWin.setCentralWidget(&browser);
|
||||||
|
|
||||||
QMenu *fileMenu = mainWin.menuBar()->addMenu(QObject::tr("&File"));
|
QMenu *fileMenu = mainWin.menuBar()->addMenu(QObject::tr("&File"));
|
||||||
fileMenu->addAction(QObject::tr("Add &Connection..."), &browser, SLOT(addConnection()));
|
fileMenu->addAction(QObject::tr("Add &Connection..."),
|
||||||
|
[&]() { browser.addConnection(); });
|
||||||
fileMenu->addSeparator();
|
fileMenu->addSeparator();
|
||||||
fileMenu->addAction(QObject::tr("&Quit"), &app, SLOT(quit()));
|
fileMenu->addAction(QObject::tr("&Quit"), []() { qApp->quit(); });
|
||||||
|
|
||||||
QMenu *helpMenu = mainWin.menuBar()->addMenu(QObject::tr("&Help"));
|
QMenu *helpMenu = mainWin.menuBar()->addMenu(QObject::tr("&Help"));
|
||||||
helpMenu->addAction(QObject::tr("About"), &browser, SLOT(about()));
|
helpMenu->addAction(QObject::tr("About"), [&]() { browser.about(); });
|
||||||
helpMenu->addAction(QObject::tr("About Qt"), qApp, SLOT(aboutQt()));
|
helpMenu->addAction(QObject::tr("About Qt"), []() { qApp->aboutQt(); });
|
||||||
|
|
||||||
QObject::connect(&browser, SIGNAL(statusMessage(QString)),
|
QObject::connect(&browser, &Browser::statusMessage, [&mainWin](const QString &text) {
|
||||||
mainWin.statusBar(), SLOT(showMessage(QString)));
|
mainWin.statusBar()->showMessage(text);
|
||||||
|
});
|
||||||
|
|
||||||
addConnectionsFromCommandline(app.arguments(), &browser);
|
addConnectionsFromCommandline(app.arguments(), &browser);
|
||||||
mainWin.show();
|
mainWin.show();
|
||||||
|
@ -60,7 +60,7 @@ class QSqlConnectionDialog: public QDialog
|
|||||||
{
|
{
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
public:
|
public:
|
||||||
QSqlConnectionDialog(QWidget *parent = 0);
|
QSqlConnectionDialog(QWidget *parent = nullptr);
|
||||||
~QSqlConnectionDialog();
|
~QSqlConnectionDialog();
|
||||||
|
|
||||||
QString driverName() const;
|
QString driverName() const;
|
||||||
|
@ -87,12 +87,12 @@ Window::Window(QWidget *parent)
|
|||||||
//! [Set up the mapper]
|
//! [Set up the mapper]
|
||||||
|
|
||||||
//! [Set up connections and layouts]
|
//! [Set up connections and layouts]
|
||||||
connect(previousButton, SIGNAL(clicked()),
|
connect(previousButton, &QPushButton::clicked,
|
||||||
mapper, SLOT(toPrevious()));
|
mapper, &QDataWidgetMapper::toPrevious);
|
||||||
connect(nextButton, SIGNAL(clicked()),
|
connect(nextButton, &QPushButton::clicked,
|
||||||
mapper, SLOT(toNext()));
|
mapper, &QDataWidgetMapper::toNext);
|
||||||
connect(mapper, SIGNAL(currentIndexChanged(int)),
|
connect(mapper, &QDataWidgetMapper::currentIndexChanged,
|
||||||
this, SLOT(updateButtons(int)));
|
this, &Window::updateButtons);
|
||||||
|
|
||||||
QGridLayout *layout = new QGridLayout();
|
QGridLayout *layout = new QGridLayout();
|
||||||
layout->addWidget(nameLabel, 0, 0, 1, 1);
|
layout->addWidget(nameLabel, 0, 0, 1, 1);
|
||||||
|
@ -72,7 +72,7 @@ class Window : public QWidget
|
|||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Window(QWidget *parent = 0);
|
Window(QWidget *parent = nullptr);
|
||||||
|
|
||||||
private slots:
|
private slots:
|
||||||
void updateButtons(int row);
|
void updateButtons(int row);
|
||||||
|
@ -47,12 +47,14 @@
|
|||||||
** $QT_END_LICENSE$
|
** $QT_END_LICENSE$
|
||||||
**
|
**
|
||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
|
|
||||||
#include <QtWidgets>
|
|
||||||
#include <QtSql>
|
|
||||||
|
|
||||||
#include "../connection.h"
|
#include "../connection.h"
|
||||||
|
|
||||||
|
#include <QApplication>
|
||||||
|
#include <QSqlTableModel>
|
||||||
|
#include <QTableView>
|
||||||
|
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
void initializeModel(QSqlTableModel *model)
|
void initializeModel(QSqlTableModel *model)
|
||||||
{
|
{
|
||||||
model->setTable("person");
|
model->setTable("person");
|
||||||
@ -76,7 +78,7 @@ int main(int argc, char *argv[])
|
|||||||
{
|
{
|
||||||
QApplication app(argc, argv);
|
QApplication app(argc, argv);
|
||||||
if (!createConnection())
|
if (!createConnection())
|
||||||
return 1;
|
return EXIT_FAILURE;
|
||||||
|
|
||||||
QSqlTableModel model;
|
QSqlTableModel model;
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user