SQL examples: code cleanup
Misc code cleanup for the sql examples: - don't include global Qt headers but only needed ones - use proper tr() where possible - pass parameters by const ref - style fixes Change-Id: I4fd4293948918b9d7b373b6d1e8eeecf6f25a622 Reviewed-by: Samuel Gaist <samuel.gaist@idiap.ch>
This commit is contained in:
parent
27dd178900
commit
8bfebaa22f
@ -3,14 +3,13 @@
|
||||
|
||||
#include "bookdelegate.h"
|
||||
|
||||
#include <QtWidgets>
|
||||
#include <QMouseEvent>
|
||||
#include <QPainter>
|
||||
#include <QSpinBox>
|
||||
|
||||
BookDelegate::BookDelegate(QObject *parent)
|
||||
: QSqlRelationalDelegate(parent)
|
||||
{}
|
||||
|
||||
void BookDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
|
||||
const QModelIndex &index) const
|
||||
void BookDelegate::paint(QPainter *painter,
|
||||
const QStyleOptionViewItem &option,
|
||||
const QModelIndex &index) const
|
||||
{
|
||||
if (index.column() != 5) {
|
||||
QSqlRelationalDelegate::paint(painter, option, index);
|
||||
@ -38,18 +37,17 @@ void BookDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
|
||||
QIcon starFilledIcon(QStringLiteral(":images/star-filled.svg"));
|
||||
|
||||
for (int i = 0; i < 5; ++i) {
|
||||
if (i < rating) {
|
||||
if (i < rating)
|
||||
starFilledIcon.paint(painter, QRect(x, y, width, height));
|
||||
} else {
|
||||
else
|
||||
starIcon.paint(painter, QRect(x, y, width, height));
|
||||
}
|
||||
x += width;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
QSize BookDelegate::sizeHint(const QStyleOptionViewItem &option,
|
||||
const QModelIndex &index) const
|
||||
const QModelIndex &index) const
|
||||
{
|
||||
if (index.column() == 5)
|
||||
return QSize(5 * iconDimension, iconDimension) + QSize(cellPadding, cellPadding);
|
||||
@ -66,7 +64,7 @@ bool BookDelegate::editorEvent(QEvent *event, QAbstractItemModel *model,
|
||||
|
||||
if (event->type() == QEvent::MouseButtonPress) {
|
||||
QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event);
|
||||
int stars = qBound(0, int(0.7 + qreal(mouseEvent->position().toPoint().x()
|
||||
int stars = qBound(0, int(0.7 + qreal(mouseEvent->position().x()
|
||||
- option.rect.x()) / iconDimension), 5);
|
||||
model->setData(index, QVariant(stars));
|
||||
// So that the selection can change:
|
||||
|
@ -5,7 +5,6 @@
|
||||
#define BOOKDELEGATE_H
|
||||
|
||||
#include <QModelIndex>
|
||||
#include <QPixmap>
|
||||
#include <QSize>
|
||||
#include <QSqlRelationalDelegate>
|
||||
|
||||
@ -14,7 +13,7 @@ QT_FORWARD_DECLARE_CLASS(QPainter)
|
||||
class BookDelegate : public QSqlRelationalDelegate
|
||||
{
|
||||
public:
|
||||
BookDelegate(QObject *parent);
|
||||
using QSqlRelationalDelegate::QSqlRelationalDelegate;
|
||||
|
||||
void paint(QPainter *painter, const QStyleOptionViewItem &option,
|
||||
const QModelIndex &index) const override;
|
||||
|
@ -5,16 +5,27 @@
|
||||
#include "bookdelegate.h"
|
||||
#include "initdb.h"
|
||||
|
||||
#include <QtSql>
|
||||
#include <QApplication>
|
||||
#include <QComboBox>
|
||||
#include <QDataWidgetMapper>
|
||||
#include <QGridLayout>
|
||||
#include <QHeaderView>
|
||||
#include <QLabel>
|
||||
#include <QLineEdit>
|
||||
#include <QMenu>
|
||||
#include <QMenuBar>
|
||||
#include <QMessageBox>
|
||||
#include <QPainter>
|
||||
#include <QScrollBar>
|
||||
#include <QSpinBox>
|
||||
#include <QSqlDatabase>
|
||||
#include <QTableView>
|
||||
|
||||
BookWindow::BookWindow()
|
||||
{
|
||||
if (!QSqlDatabase::drivers().contains("QSQLITE"))
|
||||
QMessageBox::critical(
|
||||
this,
|
||||
"Unable to load database",
|
||||
"This demo needs the SQLITE driver"
|
||||
);
|
||||
QMessageBox::critical(this, tr("Unable to load database"),
|
||||
tr("This demo needs the SQLITE driver"));
|
||||
|
||||
// Initialize the database:
|
||||
QSqlError err = initDb();
|
||||
@ -50,8 +61,8 @@ BookWindow::BookWindow()
|
||||
|
||||
void BookWindow::showError(const QSqlError &err)
|
||||
{
|
||||
QMessageBox::critical(this, "Unable to initialize Database",
|
||||
"Error initializing database: " + err.text());
|
||||
QMessageBox::critical(this, tr("Unable to initialize Database"),
|
||||
tr("Error initializing database: %1").arg(err.text()));
|
||||
}
|
||||
|
||||
void BookWindow::createLayout()
|
||||
|
@ -4,8 +4,15 @@
|
||||
#ifndef BOOKWINDOW_H
|
||||
#define BOOKWINDOW_H
|
||||
|
||||
#include <QtWidgets>
|
||||
#include <QtSql>
|
||||
#include <QMainWindow>
|
||||
QT_FORWARD_DECLARE_CLASS(QComboBox)
|
||||
QT_FORWARD_DECLARE_CLASS(QGridLayout)
|
||||
QT_FORWARD_DECLARE_CLASS(QLabel)
|
||||
QT_FORWARD_DECLARE_CLASS(QLineEdit)
|
||||
QT_FORWARD_DECLARE_CLASS(QSpinBox)
|
||||
QT_FORWARD_DECLARE_CLASS(QSqlError)
|
||||
QT_FORWARD_DECLARE_CLASS(QSqlRelationalTableModel)
|
||||
QT_FORWARD_DECLARE_CLASS(QTableView)
|
||||
|
||||
class BookWindow: public QMainWindow
|
||||
{
|
||||
|
@ -4,7 +4,9 @@
|
||||
#ifndef INITDB_H
|
||||
#define INITDB_H
|
||||
|
||||
#include <QtSql>
|
||||
#include <QDate>
|
||||
#include <QSqlError>
|
||||
#include <QSqlQuery>
|
||||
|
||||
void addBook(QSqlQuery &q, const QString &title, int year, const QVariant &authorId,
|
||||
const QVariant &genreId, int rating)
|
||||
|
@ -3,7 +3,7 @@
|
||||
|
||||
#include "bookwindow.h"
|
||||
|
||||
#include <QtWidgets>
|
||||
#include <QApplication>
|
||||
|
||||
int main(int argc, char * argv[])
|
||||
{
|
||||
|
@ -1,11 +1,16 @@
|
||||
// Copyright (C) 2016 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
||||
|
||||
#include <QtWidgets>
|
||||
#include <QtSql>
|
||||
|
||||
#include "tableeditor.h"
|
||||
|
||||
#include <QDialogButtonBox>
|
||||
#include <QHBoxLayout>
|
||||
#include <QMessageBox>
|
||||
#include <QPushButton>
|
||||
#include <QSqlError>
|
||||
#include <QSqlTableModel>
|
||||
#include <QTableView>
|
||||
|
||||
//! [0]
|
||||
TableEditor::TableEditor(const QString &tableName, QWidget *parent)
|
||||
: QWidget(parent)
|
||||
|
@ -6,11 +6,9 @@
|
||||
|
||||
#include <QDialog>
|
||||
|
||||
QT_BEGIN_NAMESPACE
|
||||
class QDialogButtonBox;
|
||||
class QPushButton;
|
||||
class QSqlTableModel;
|
||||
QT_END_NAMESPACE
|
||||
QT_FORWARD_DECLARE_CLASS(QDialogButtonBox)
|
||||
QT_FORWARD_DECLARE_CLASS(QPushButton)
|
||||
QT_FORWARD_DECLARE_CLASS(QSqlTableModel)
|
||||
|
||||
//! [0]
|
||||
class TableEditor : public QWidget
|
||||
|
@ -3,10 +3,23 @@
|
||||
|
||||
#include "dialog.h"
|
||||
|
||||
int uniqueAlbumId;
|
||||
int uniqueArtistId;
|
||||
#include <QDialogButtonBox>
|
||||
#include <QDate>
|
||||
#include <QDomDocument>
|
||||
#include <QGroupBox>
|
||||
#include <QLabel>
|
||||
#include <QLineEdit>
|
||||
#include <QMessageBox>
|
||||
#include <QPushButton>
|
||||
#include <QSpinBox>
|
||||
#include <QSqlField>
|
||||
#include <QSqlRelationalTableModel>
|
||||
#include <QSqlRecord>
|
||||
#include <QVBoxLayout>
|
||||
|
||||
Dialog::Dialog(QSqlRelationalTableModel *albums, QDomDocument details,
|
||||
int Dialog::s_artistId = 0;
|
||||
int Dialog::s_albumId = 0;
|
||||
Dialog::Dialog(QSqlRelationalTableModel *albums, const QDomDocument &details,
|
||||
QFile *output, QWidget *parent)
|
||||
: QDialog(parent)
|
||||
{
|
||||
@ -25,6 +38,12 @@ Dialog::Dialog(QSqlRelationalTableModel *albums, QDomDocument details,
|
||||
setWindowTitle(tr("Add Album"));
|
||||
}
|
||||
|
||||
void Dialog::setInitialAlbumAndArtistId(int albumId, int artistId)
|
||||
{
|
||||
s_albumId = albumId;
|
||||
s_artistId = artistId;
|
||||
}
|
||||
|
||||
void Dialog::submit()
|
||||
{
|
||||
QString artist = artistEditor->text();
|
||||
@ -145,7 +164,7 @@ void Dialog::addTracks(int albumId, const QStringList &tracks)
|
||||
*/
|
||||
}
|
||||
|
||||
void Dialog::increaseAlbumCount(QModelIndex artistIndex)
|
||||
void Dialog::increaseAlbumCount(const QModelIndex &artistIndex)
|
||||
{
|
||||
QSqlTableModel *artistModel = model->relationModel(2);
|
||||
|
||||
@ -219,7 +238,7 @@ QDialogButtonBox *Dialog::createButtons()
|
||||
return buttonBox;
|
||||
}
|
||||
|
||||
QModelIndex Dialog::indexOfArtist(const QString &artist)
|
||||
QModelIndex Dialog::indexOfArtist(const QString &artist) const
|
||||
{
|
||||
QSqlTableModel *artistModel = model->relationModel(2);
|
||||
|
||||
@ -234,12 +253,12 @@ QModelIndex Dialog::indexOfArtist(const QString &artist)
|
||||
|
||||
int Dialog::generateArtistId()
|
||||
{
|
||||
uniqueArtistId += 1;
|
||||
return uniqueArtistId;
|
||||
s_artistId += 1;
|
||||
return s_artistId;
|
||||
}
|
||||
|
||||
int Dialog::generateAlbumId()
|
||||
{
|
||||
uniqueAlbumId += 1;
|
||||
return uniqueAlbumId;
|
||||
s_albumId += 1;
|
||||
return s_albumId;
|
||||
}
|
||||
|
@ -4,17 +4,25 @@
|
||||
#ifndef DIALOG_H
|
||||
#define DIALOG_H
|
||||
|
||||
#include <QtWidgets>
|
||||
#include <QtSql>
|
||||
#include <QtXml>
|
||||
#include <QDialog>
|
||||
#include <QDomDocument>
|
||||
|
||||
QT_FORWARD_DECLARE_CLASS(QDialogButtonBox)
|
||||
QT_FORWARD_DECLARE_CLASS(QFile)
|
||||
QT_FORWARD_DECLARE_CLASS(QGroupBox)
|
||||
QT_FORWARD_DECLARE_CLASS(QLineEdit)
|
||||
QT_FORWARD_DECLARE_CLASS(QModelIndex)
|
||||
QT_FORWARD_DECLARE_CLASS(QSpinBox)
|
||||
QT_FORWARD_DECLARE_CLASS(QSqlRelationalTableModel)
|
||||
|
||||
class Dialog : public QDialog
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
Dialog(QSqlRelationalTableModel *albums, QDomDocument details,
|
||||
Dialog(QSqlRelationalTableModel *albums, const QDomDocument &details,
|
||||
QFile *output, QWidget *parent = nullptr);
|
||||
static void setInitialAlbumAndArtistId(int albumId, int artistId);
|
||||
|
||||
private slots:
|
||||
void revert();
|
||||
@ -27,10 +35,8 @@ private:
|
||||
QDialogButtonBox *createButtons();
|
||||
QGroupBox *createInputWidgets();
|
||||
int findArtistId(const QString &artist);
|
||||
static int generateAlbumId();
|
||||
static int generateArtistId();
|
||||
void increaseAlbumCount(QModelIndex artistIndex);
|
||||
QModelIndex indexOfArtist(const QString &artist);
|
||||
void increaseAlbumCount(const QModelIndex &artistIndex);
|
||||
QModelIndex indexOfArtist(const QString &artist) const;
|
||||
|
||||
QSqlRelationalTableModel *model;
|
||||
QDomDocument albumDetails;
|
||||
@ -40,6 +46,11 @@ private:
|
||||
QLineEdit *titleEditor;
|
||||
QSpinBox *yearEditor;
|
||||
QLineEdit *tracksEditor;
|
||||
|
||||
static int generateAlbumId();
|
||||
static int generateArtistId();
|
||||
static int s_artistId;
|
||||
static int s_albumId;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
@ -7,8 +7,6 @@
|
||||
#include <QApplication>
|
||||
#include <QFile>
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
QApplication app(argc, argv);
|
||||
|
@ -4,12 +4,20 @@
|
||||
#include "mainwindow.h"
|
||||
#include "dialog.h"
|
||||
|
||||
#include <QtWidgets>
|
||||
#include <QtSql>
|
||||
#include <QtXml>
|
||||
|
||||
extern int uniqueAlbumId;
|
||||
extern int uniqueArtistId;
|
||||
#include <QApplication>
|
||||
#include <QComboBox>
|
||||
#include <QHeaderView>
|
||||
#include <QFile>
|
||||
#include <QGridLayout>
|
||||
#include <QGroupBox>
|
||||
#include <QLabel>
|
||||
#include <QListWidget>
|
||||
#include <QMenu>
|
||||
#include <QMenuBar>
|
||||
#include <QMessageBox>
|
||||
#include <QSqlRecord>
|
||||
#include <QSqlRelationalTableModel>
|
||||
#include <QTableView>
|
||||
|
||||
MainWindow::MainWindow(const QString &artistTable, const QString &albumTable,
|
||||
QFile *albumDetails, QWidget *parent)
|
||||
@ -28,13 +36,12 @@ MainWindow::MainWindow(const QString &artistTable, const QString &albumTable,
|
||||
QGroupBox *details = createDetailsGroupBox();
|
||||
|
||||
artistView->setCurrentIndex(0);
|
||||
uniqueAlbumId = model->rowCount();
|
||||
uniqueArtistId = artistView->count();
|
||||
Dialog::setInitialAlbumAndArtistId(model->rowCount(), artistView->count());
|
||||
|
||||
connect(model, &QSqlRelationalTableModel::rowsInserted,
|
||||
this, &MainWindow::updateHeader);
|
||||
this, &MainWindow::adjustHeader);
|
||||
connect(model, &QSqlRelationalTableModel::rowsRemoved,
|
||||
this, &MainWindow::updateHeader);
|
||||
this, &MainWindow::adjustHeader);
|
||||
|
||||
QGridLayout *layout = new QGridLayout;
|
||||
layout->addWidget(artists, 0, 0);
|
||||
@ -67,7 +74,7 @@ void MainWindow::changeArtist(int row)
|
||||
}
|
||||
}
|
||||
|
||||
void MainWindow::showArtistProfile(QModelIndex index)
|
||||
void MainWindow::showArtistProfile(const QModelIndex &index)
|
||||
{
|
||||
QSqlRecord record = model->relationModel(2)->record(index.row());
|
||||
|
||||
@ -84,7 +91,7 @@ void MainWindow::showArtistProfile(QModelIndex index)
|
||||
imageLabel->hide();
|
||||
}
|
||||
|
||||
void MainWindow::showAlbumDetails(QModelIndex index)
|
||||
void MainWindow::showAlbumDetails(const QModelIndex &index)
|
||||
{
|
||||
QSqlRecord record = model->record(index.row());
|
||||
|
||||
@ -113,14 +120,11 @@ void MainWindow::getTrackList(QDomNode album)
|
||||
{
|
||||
trackList->clear();
|
||||
|
||||
QDomNodeList tracks = album.childNodes();
|
||||
QDomNode track;
|
||||
QString trackNumber;
|
||||
|
||||
const QDomNodeList tracks = album.childNodes();
|
||||
for (int i = 0; i < tracks.count(); ++i) {
|
||||
|
||||
track = tracks.item(i);
|
||||
trackNumber = track.toElement().attribute("number");
|
||||
const QDomNode track = tracks.item(i);
|
||||
const QString trackNumber = track.toElement().attribute("number");
|
||||
|
||||
QListWidgetItem *item = new QListWidgetItem(trackList);
|
||||
item->setText(trackNumber + ": " + track.toElement().text());
|
||||
@ -132,7 +136,7 @@ void MainWindow::addAlbum()
|
||||
Dialog *dialog = new Dialog(model, albumData, file, this);
|
||||
int accepted = dialog->exec();
|
||||
|
||||
if (accepted == 1) {
|
||||
if (accepted == QDialog::Accepted) {
|
||||
int lastRow = model->rowCount() - 1;
|
||||
albumView->selectRow(lastRow);
|
||||
albumView->scrollToBottom();
|
||||
@ -142,10 +146,10 @@ void MainWindow::addAlbum()
|
||||
|
||||
void MainWindow::deleteAlbum()
|
||||
{
|
||||
QModelIndexList selection = albumView->selectionModel()->selectedRows(0);
|
||||
const QModelIndexList selection = albumView->selectionModel()->selectedRows(0);
|
||||
|
||||
if (!selection.empty()) {
|
||||
QModelIndex idIndex = selection.at(0);
|
||||
const QModelIndex &idIndex = selection.at(0);
|
||||
int id = idIndex.data().toInt();
|
||||
QString title = idIndex.sibling(idIndex.row(), 1).data().toString();
|
||||
QString artist = idIndex.sibling(idIndex.row(), 2).data().toString();
|
||||
@ -172,9 +176,7 @@ void MainWindow::deleteAlbum()
|
||||
|
||||
void MainWindow::removeAlbumFromFile(int id)
|
||||
{
|
||||
|
||||
QDomNodeList albums = albumData.elementsByTagName("album");
|
||||
|
||||
for (int i = 0; i < albums.count(); ++i) {
|
||||
QDomNode node = albums.item(i);
|
||||
if (node.toElement().attribute("id").toInt() == id) {
|
||||
@ -197,12 +199,12 @@ void MainWindow::removeAlbumFromFile(int id)
|
||||
*/
|
||||
}
|
||||
|
||||
void MainWindow::removeAlbumFromDatabase(QModelIndex index)
|
||||
void MainWindow::removeAlbumFromDatabase(const QModelIndex &index)
|
||||
{
|
||||
model->removeRow(index.row());
|
||||
}
|
||||
|
||||
void MainWindow::decreaseAlbumCount(QModelIndex artistIndex)
|
||||
void MainWindow::decreaseAlbumCount(const QModelIndex &artistIndex)
|
||||
{
|
||||
int row = artistIndex.row();
|
||||
QModelIndex albumCountIndex = artistIndex.sibling(row, 2);
|
||||
@ -358,7 +360,7 @@ void MainWindow::showImageLabel()
|
||||
imageLabel->show();
|
||||
}
|
||||
|
||||
QModelIndex MainWindow::indexOfArtist(const QString &artist)
|
||||
QModelIndex MainWindow::indexOfArtist(const QString &artist) const
|
||||
{
|
||||
QSqlTableModel *artistModel = model->relationModel(2);
|
||||
|
||||
@ -370,11 +372,6 @@ QModelIndex MainWindow::indexOfArtist(const QString &artist)
|
||||
return QModelIndex();
|
||||
}
|
||||
|
||||
void MainWindow::updateHeader(QModelIndex, int, int)
|
||||
{
|
||||
adjustHeader();
|
||||
}
|
||||
|
||||
void MainWindow::adjustHeader()
|
||||
{
|
||||
albumView->hideColumn(0);
|
||||
|
@ -8,15 +8,13 @@
|
||||
#include <QMainWindow>
|
||||
#include <QModelIndex>
|
||||
|
||||
QT_BEGIN_NAMESPACE
|
||||
class QComboBox;
|
||||
class QFile;
|
||||
class QGroupBox;
|
||||
class QLabel;
|
||||
class QListWidget;
|
||||
class QSqlRelationalTableModel;
|
||||
class QTableView;
|
||||
QT_END_NAMESPACE
|
||||
QT_FORWARD_DECLARE_CLASS(QComboBox)
|
||||
QT_FORWARD_DECLARE_CLASS(QFile)
|
||||
QT_FORWARD_DECLARE_CLASS(QGroupBox)
|
||||
QT_FORWARD_DECLARE_CLASS(QLabel)
|
||||
QT_FORWARD_DECLARE_CLASS(QListWidget)
|
||||
QT_FORWARD_DECLARE_CLASS(QSqlRelationalTableModel)
|
||||
QT_FORWARD_DECLARE_CLASS(QTableView)
|
||||
|
||||
class MainWindow : public QMainWindow
|
||||
{
|
||||
@ -31,21 +29,20 @@ private slots:
|
||||
void addAlbum();
|
||||
void changeArtist(int row);
|
||||
void deleteAlbum();
|
||||
void showAlbumDetails(QModelIndex index);
|
||||
void showArtistProfile(QModelIndex index);
|
||||
void updateHeader(QModelIndex, int, int);
|
||||
void showAlbumDetails(const QModelIndex &index);
|
||||
void showArtistProfile(const QModelIndex &index);
|
||||
void adjustHeader();
|
||||
|
||||
private:
|
||||
void adjustHeader();
|
||||
QGroupBox *createAlbumGroupBox();
|
||||
QGroupBox *createArtistGroupBox();
|
||||
QGroupBox *createDetailsGroupBox();
|
||||
void createMenuBar();
|
||||
void decreaseAlbumCount(QModelIndex artistIndex);
|
||||
void decreaseAlbumCount(const QModelIndex &artistIndex);
|
||||
void getTrackList(QDomNode album);
|
||||
QModelIndex indexOfArtist(const QString &artist);
|
||||
QModelIndex indexOfArtist(const QString &artist) const;
|
||||
void readAlbumData();
|
||||
void removeAlbumFromDatabase(QModelIndex album);
|
||||
void removeAlbumFromDatabase(const QModelIndex &album);
|
||||
void removeAlbumFromFile(int id);
|
||||
void showImageLabel();
|
||||
|
||||
|
@ -1,10 +1,10 @@
|
||||
// Copyright (C) 2016 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
||||
|
||||
#include <QtWidgets>
|
||||
|
||||
#include "customsqlmodel.h"
|
||||
|
||||
#include <QColor>
|
||||
|
||||
CustomSqlModel::CustomSqlModel(QObject *parent)
|
||||
: QSqlQueryModel(parent)
|
||||
{
|
||||
|
@ -1,18 +1,17 @@
|
||||
// Copyright (C) 2016 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
||||
|
||||
#include <QtSql>
|
||||
|
||||
#include "editablesqlmodel.h"
|
||||
|
||||
#include <QSqlQuery>
|
||||
|
||||
EditableSqlModel::EditableSqlModel(QObject *parent)
|
||||
: QSqlQueryModel(parent)
|
||||
{
|
||||
}
|
||||
|
||||
//! [0]
|
||||
Qt::ItemFlags EditableSqlModel::flags(
|
||||
const QModelIndex &index) const
|
||||
Qt::ItemFlags EditableSqlModel::flags(const QModelIndex &index) const
|
||||
{
|
||||
Qt::ItemFlags flags = QSqlQueryModel::flags(index);
|
||||
if (index.column() == 1 || index.column() == 2)
|
||||
@ -33,11 +32,10 @@ bool EditableSqlModel::setData(const QModelIndex &index, const QVariant &value,
|
||||
clear();
|
||||
|
||||
bool ok;
|
||||
if (index.column() == 1) {
|
||||
if (index.column() == 1)
|
||||
ok = setFirstName(id, value.toString());
|
||||
} else {
|
||||
else
|
||||
ok = setLastName(id, value.toString());
|
||||
}
|
||||
refresh();
|
||||
return ok;
|
||||
}
|
||||
|
@ -8,8 +8,6 @@
|
||||
#include <QApplication>
|
||||
#include <QTableView>
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
void initializeModel(QSqlQueryModel *model)
|
||||
{
|
||||
model->setQuery("select * from person");
|
||||
|
@ -1,11 +1,20 @@
|
||||
// Copyright (C) 2016 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
||||
|
||||
#include <QtWidgets>
|
||||
#include <QtSql>
|
||||
|
||||
#include "window.h"
|
||||
|
||||
#include <QComboBox>
|
||||
#include <QDataWidgetMapper>
|
||||
#include <QGridLayout>
|
||||
#include <QLabel>
|
||||
#include <QLineEdit>
|
||||
#include <QMessageBox>
|
||||
#include <QPushButton>
|
||||
#include <QSqlQuery>
|
||||
#include <QSqlRelationalDelegate>
|
||||
#include <QSqlTableModel>
|
||||
#include <QTextEdit>
|
||||
|
||||
//! [Set up widgets]
|
||||
Window::Window(QWidget *parent)
|
||||
: QWidget(parent)
|
||||
|
@ -6,18 +6,16 @@
|
||||
|
||||
#include <QWidget>
|
||||
|
||||
QT_BEGIN_NAMESPACE
|
||||
class QComboBox;
|
||||
class QDataWidgetMapper;
|
||||
class QItemSelectionModel;
|
||||
class QLabel;
|
||||
class QLineEdit;
|
||||
class QPushButton;
|
||||
class QSqlRelationalTableModel;
|
||||
class QStandardItemModel;
|
||||
class QStringListModel;
|
||||
class QTextEdit;
|
||||
QT_END_NAMESPACE
|
||||
QT_FORWARD_DECLARE_CLASS(QComboBox)
|
||||
QT_FORWARD_DECLARE_CLASS(QDataWidgetMapper)
|
||||
QT_FORWARD_DECLARE_CLASS(QItemSelectionModel)
|
||||
QT_FORWARD_DECLARE_CLASS(QLabel)
|
||||
QT_FORWARD_DECLARE_CLASS(QLineEdit)
|
||||
QT_FORWARD_DECLARE_CLASS(QPushButton)
|
||||
QT_FORWARD_DECLARE_CLASS(QSqlRelationalTableModel)
|
||||
QT_FORWARD_DECLARE_CLASS(QStandardItemModel)
|
||||
QT_FORWARD_DECLARE_CLASS(QStringListModel)
|
||||
QT_FORWARD_DECLARE_CLASS(QTextEdit)
|
||||
|
||||
//! [Window definition]
|
||||
class Window : public QWidget
|
||||
|
@ -6,8 +6,6 @@
|
||||
#include <QSqlTableModel>
|
||||
#include <QTableView>
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
void initializeModel(QSqlTableModel *model)
|
||||
{
|
||||
model->setTable("person");
|
||||
|
Loading…
x
Reference in New Issue
Block a user