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