qt 6.5.1 original

This commit is contained in:
kleuter
2023-10-29 23:33:08 +01:00
parent 71d22ab6b0
commit 85d238dfda
21202 changed files with 5499099 additions and 0 deletions

View File

@ -0,0 +1,52 @@
# Copyright (C) 2022 The Qt Company Ltd.
# SPDX-License-Identifier: BSD-3-Clause
cmake_minimum_required(VERSION 3.16)
project(books LANGUAGES CXX)
if(NOT DEFINED INSTALL_EXAMPLESDIR)
set(INSTALL_EXAMPLESDIR "examples")
endif()
set(INSTALL_EXAMPLEDIR "${INSTALL_EXAMPLESDIR}/sql/books")
find_package(Qt6 REQUIRED COMPONENTS Core Gui Sql Widgets)
qt_standard_project_setup()
qt_add_executable(books
bookdelegate.cpp bookdelegate.h
bookwindow.cpp bookwindow.h bookwindow.ui
initdb.h
main.cpp
)
set_target_properties(books PROPERTIES
WIN32_EXECUTABLE TRUE
MACOSX_BUNDLE TRUE
)
target_link_libraries(books PRIVATE
Qt6::Core
Qt6::Gui
Qt6::Sql
Qt6::Widgets
)
# Resources:
set(books_resource_files
"images/star.png"
)
qt_add_resources(books "books"
PREFIX
"/"
FILES
${books_resource_files}
)
install(TARGETS books
RUNTIME DESTINATION "${INSTALL_EXAMPLEDIR}"
BUNDLE DESTINATION "${INSTALL_EXAMPLEDIR}"
LIBRARY DESTINATION "${INSTALL_EXAMPLEDIR}"
)

View File

@ -0,0 +1,91 @@
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
#include "bookdelegate.h"
#include <QtWidgets>
BookDelegate::BookDelegate(QObject *parent)
: QSqlRelationalDelegate(parent), star(QPixmap(":images/star.png"))
{
}
void BookDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
const QModelIndex &index) const
{
if (index.column() != 5) {
QSqlRelationalDelegate::paint(painter, option, index);
} else {
const QAbstractItemModel *model = index.model();
QPalette::ColorGroup cg = (option.state & QStyle::State_Enabled) ?
(option.state & QStyle::State_Active) ?
QPalette::Normal :
QPalette::Inactive :
QPalette::Disabled;
if (option.state & QStyle::State_Selected)
painter->fillRect(
option.rect,
option.palette.color(cg, QPalette::Highlight));
int rating = model->data(index, Qt::DisplayRole).toInt();
int width = star.width();
int height = star.height();
int x = option.rect.x();
int y = option.rect.y() + (option.rect.height() / 2) - (height / 2);
for (int i = 0; i < rating; ++i) {
painter->drawPixmap(x, y, star);
x += width;
}
}
QPen pen = painter->pen();
painter->setPen(option.palette.color(QPalette::Mid));
painter->drawLine(option.rect.bottomLeft(), option.rect.bottomRight());
painter->drawLine(option.rect.topRight(), option.rect.bottomRight());
painter->setPen(pen);
}
QSize BookDelegate::sizeHint(const QStyleOptionViewItem &option,
const QModelIndex &index) const
{
if (index.column() == 5)
return QSize(5 * star.width(), star.height()) + QSize(1, 1);
// Since we draw the grid ourselves:
return QSqlRelationalDelegate::sizeHint(option, index) + QSize(1, 1);
}
bool BookDelegate::editorEvent(QEvent *event, QAbstractItemModel *model,
const QStyleOptionViewItem &option,
const QModelIndex &index)
{
if (index.column() != 5)
return QSqlRelationalDelegate::editorEvent(event, model, option, index);
if (event->type() == QEvent::MouseButtonPress) {
QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event);
int stars = qBound(0, int(0.7 + qreal(mouseEvent->position().toPoint().x()
- option.rect.x()) / star.width()), 5);
model->setData(index, QVariant(stars));
// So that the selection can change:
return false;
}
return true;
}
QWidget *BookDelegate::createEditor(QWidget *parent,
const QStyleOptionViewItem &option,
const QModelIndex &index) const
{
if (index.column() != 4)
return QSqlRelationalDelegate::createEditor(parent, option, index);
// For editing the year, return a spinbox with a range from -1000 to 2100.
QSpinBox *sb = new QSpinBox(parent);
sb->setFrame(false);
sb->setMaximum(2100);
sb->setMinimum(-1000);
return sb;
}

View File

@ -0,0 +1,36 @@
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
#ifndef BOOKDELEGATE_H
#define BOOKDELEGATE_H
#include <QModelIndex>
#include <QPixmap>
#include <QSize>
#include <QSqlRelationalDelegate>
QT_FORWARD_DECLARE_CLASS(QPainter)
class BookDelegate : public QSqlRelationalDelegate
{
public:
BookDelegate(QObject *parent);
void paint(QPainter *painter, const QStyleOptionViewItem &option,
const QModelIndex &index) const override;
QSize sizeHint(const QStyleOptionViewItem &option,
const QModelIndex &index) const override;
bool editorEvent(QEvent *event, QAbstractItemModel *model,
const QStyleOptionViewItem &option,
const QModelIndex &index) override;
QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option,
const QModelIndex &index) const override;
private:
QPixmap star;
};
#endif

View File

@ -0,0 +1,13 @@
TEMPLATE = app
INCLUDEPATH += .
HEADERS = bookdelegate.h bookwindow.h initdb.h
RESOURCES = books.qrc
SOURCES = bookdelegate.cpp main.cpp bookwindow.cpp
FORMS = bookwindow.ui
QT += sql widgets widgets
requires(qtConfig(tableview))
target.path = $$[QT_INSTALL_EXAMPLES]/sql/books
INSTALLS += target

View File

@ -0,0 +1,5 @@
<!DOCTYPE RCC><RCC version="1.0">
<qresource prefix="/">
<file>images/star.png</file>
</qresource>
</RCC>

View File

@ -0,0 +1,124 @@
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
#include "bookwindow.h"
#include "bookdelegate.h"
#include "initdb.h"
#include <QtSql>
BookWindow::BookWindow()
{
ui.setupUi(this);
if (!QSqlDatabase::drivers().contains("QSQLITE"))
QMessageBox::critical(
this,
"Unable to load database",
"This demo needs the SQLITE driver"
);
// Initialize the database:
QSqlError err = initDb();
if (err.type() != QSqlError::NoError) {
showError(err);
return;
}
// Create the data model:
model = new QSqlRelationalTableModel(ui.bookTable);
model->setEditStrategy(QSqlTableModel::OnManualSubmit);
model->setTable("books");
// Remember the indexes of the columns:
authorIdx = model->fieldIndex("author");
genreIdx = model->fieldIndex("genre");
// Set the relations to the other database tables:
model->setRelation(authorIdx, QSqlRelation("authors", "id", "name"));
model->setRelation(genreIdx, QSqlRelation("genres", "id", "name"));
// Set the localized header captions:
model->setHeaderData(authorIdx, Qt::Horizontal, tr("Author Name"));
model->setHeaderData(genreIdx, Qt::Horizontal, tr("Genre"));
model->setHeaderData(model->fieldIndex("title"),
Qt::Horizontal, tr("Title"));
model->setHeaderData(model->fieldIndex("year"), Qt::Horizontal, tr("Year"));
model->setHeaderData(model->fieldIndex("rating"),
Qt::Horizontal, tr("Rating"));
// Populate the model:
if (!model->select()) {
showError(model->lastError());
return;
}
// Set the model and hide the ID column:
ui.bookTable->setModel(model);
ui.bookTable->setItemDelegate(new BookDelegate(ui.bookTable));
ui.bookTable->setColumnHidden(model->fieldIndex("id"), true);
ui.bookTable->setSelectionMode(QAbstractItemView::SingleSelection);
// Initialize the Author combo box:
ui.authorEdit->setModel(model->relationModel(authorIdx));
ui.authorEdit->setModelColumn(
model->relationModel(authorIdx)->fieldIndex("name"));
ui.genreEdit->setModel(model->relationModel(genreIdx));
ui.genreEdit->setModelColumn(
model->relationModel(genreIdx)->fieldIndex("name"));
// Lock and prohibit resizing of the width of the rating column:
ui.bookTable->horizontalHeader()->setSectionResizeMode(
model->fieldIndex("rating"),
QHeaderView::ResizeToContents);
QDataWidgetMapper *mapper = new QDataWidgetMapper(this);
mapper->setModel(model);
mapper->setItemDelegate(new BookDelegate(this));
mapper->addMapping(ui.titleEdit, model->fieldIndex("title"));
mapper->addMapping(ui.yearEdit, model->fieldIndex("year"));
mapper->addMapping(ui.authorEdit, authorIdx);
mapper->addMapping(ui.genreEdit, genreIdx);
mapper->addMapping(ui.ratingEdit, model->fieldIndex("rating"));
connect(ui.bookTable->selectionModel(),
&QItemSelectionModel::currentRowChanged,
mapper,
&QDataWidgetMapper::setCurrentModelIndex
);
ui.bookTable->setCurrentIndex(model->index(0, 0));
createMenuBar();
}
void BookWindow::showError(const QSqlError &err)
{
QMessageBox::critical(this, "Unable to initialize Database",
"Error initializing database: " + err.text());
}
void BookWindow::createMenuBar()
{
QAction *quitAction = new QAction(tr("&Quit"), this);
QAction *aboutAction = new QAction(tr("&About"), this);
QAction *aboutQtAction = new QAction(tr("&About Qt"), this);
QMenu *fileMenu = menuBar()->addMenu(tr("&File"));
fileMenu->addAction(quitAction);
QMenu *helpMenu = menuBar()->addMenu(tr("&Help"));
helpMenu->addAction(aboutAction);
helpMenu->addAction(aboutQtAction);
connect(quitAction, &QAction::triggered, qApp, &QCoreApplication::quit);
connect(aboutAction, &QAction::triggered, this, &BookWindow::about);
connect(aboutQtAction, &QAction::triggered, qApp, &QApplication::aboutQt);
}
void BookWindow::about()
{
QMessageBox::about(this, tr("About Books"),
tr("<p>The <b>Books</b> example shows how to use Qt SQL classes "
"with a model/view framework."));
}

View File

@ -0,0 +1,31 @@
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
#ifndef BOOKWINDOW_H
#define BOOKWINDOW_H
#include <QtWidgets>
#include <QtSql>
#include "ui_bookwindow.h"
class BookWindow: public QMainWindow
{
Q_OBJECT
public:
BookWindow();
private slots:
void about();
private:
void showError(const QSqlError &err);
Ui::BookWindow ui;
QSqlRelationalTableModel *model;
int authorIdx, genreIdx;
void createMenuBar();
};
#endif

View File

@ -0,0 +1,164 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>BookWindow</class>
<widget class="QMainWindow" name="BookWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>601</width>
<height>420</height>
</rect>
</property>
<property name="windowTitle">
<string>Books</string>
</property>
<widget class="QWidget" name="centralWidget">
<layout class="QVBoxLayout">
<property name="spacing">
<number>6</number>
</property>
<property name="leftMargin">
<number>9</number>
</property>
<property name="topMargin">
<number>9</number>
</property>
<property name="rightMargin">
<number>9</number>
</property>
<property name="bottomMargin">
<number>9</number>
</property>
<item>
<widget class="QGroupBox" name="groupBox">
<property name="title">
<string/>
</property>
<layout class="QVBoxLayout">
<property name="spacing">
<number>6</number>
</property>
<property name="leftMargin">
<number>9</number>
</property>
<property name="topMargin">
<number>9</number>
</property>
<property name="rightMargin">
<number>9</number>
</property>
<property name="bottomMargin">
<number>9</number>
</property>
<item>
<widget class="QTableView" name="bookTable">
<property name="selectionBehavior">
<enum>QAbstractItemView::SelectRows</enum>
</property>
</widget>
</item>
<item>
<widget class="QGroupBox" name="groupBox_2">
<property name="title">
<string>Details</string>
</property>
<layout class="QFormLayout">
<item row="0" column="0">
<widget class="QLabel" name="label_5">
<property name="text">
<string>&lt;b&gt;Title:&lt;/b&gt;</string>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QLineEdit" name="titleEdit">
<property name="enabled">
<bool>true</bool>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QLabel" name="label_2">
<property name="text">
<string>&lt;b&gt;Author: &lt;/b&gt;</string>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QComboBox" name="authorEdit">
<property name="enabled">
<bool>true</bool>
</property>
</widget>
</item>
<item row="2" column="0">
<widget class="QLabel" name="label_3">
<property name="text">
<string>&lt;b&gt;Genre:&lt;/b&gt;</string>
</property>
</widget>
</item>
<item row="2" column="1">
<widget class="QComboBox" name="genreEdit">
<property name="enabled">
<bool>true</bool>
</property>
</widget>
</item>
<item row="3" column="0">
<widget class="QLabel" name="label_4">
<property name="text">
<string>&lt;b&gt;Year:&lt;/b&gt;</string>
</property>
</widget>
</item>
<item row="3" column="1">
<widget class="QSpinBox" name="yearEdit">
<property name="enabled">
<bool>true</bool>
</property>
<property name="prefix">
<string/>
</property>
<property name="minimum">
<number>-1000</number>
</property>
<property name="maximum">
<number>2100</number>
</property>
</widget>
</item>
<item row="4" column="0">
<widget class="QLabel" name="label">
<property name="text">
<string>&lt;b&gt;Rating:&lt;/b&gt;</string>
</property>
</widget>
</item>
<item row="4" column="1">
<widget class="QSpinBox" name="ratingEdit">
<property name="maximum">
<number>5</number>
</property>
</widget>
</item>
</layout>
</widget>
</item>
</layout>
</widget>
</item>
</layout>
</widget>
</widget>
<tabstops>
<tabstop>bookTable</tabstop>
<tabstop>titleEdit</tabstop>
<tabstop>authorEdit</tabstop>
<tabstop>genreEdit</tabstop>
<tabstop>yearEdit</tabstop>
</tabstops>
<resources/>
<connections/>
</ui>

Binary file not shown.

After

Width:  |  Height:  |  Size: 782 B

113
examples/sql/books/initdb.h Normal file
View File

@ -0,0 +1,113 @@
// Copyright (C) 2020 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
#ifndef INITDB_H
#define INITDB_H
#include <QtSql>
void addBook(QSqlQuery &q, const QString &title, int year, const QVariant &authorId,
const QVariant &genreId, int rating)
{
q.addBindValue(title);
q.addBindValue(year);
q.addBindValue(authorId);
q.addBindValue(genreId);
q.addBindValue(rating);
q.exec();
}
QVariant addGenre(QSqlQuery &q, const QString &name)
{
q.addBindValue(name);
q.exec();
return q.lastInsertId();
}
QVariant addAuthor(QSqlQuery &q, const QString &name, QDate birthdate)
{
q.addBindValue(name);
q.addBindValue(birthdate);
q.exec();
return q.lastInsertId();
}
const auto BOOKS_SQL = QLatin1String(R"(
create table books(id integer primary key, title varchar, author integer,
genre integer, year integer, rating integer)
)");
const auto AUTHORS_SQL = QLatin1String(R"(
create table authors(id integer primary key, name varchar, birthdate date)
)");
const auto GENRES_SQL = QLatin1String(R"(
create table genres(id integer primary key, name varchar)
)");
const auto INSERT_AUTHOR_SQL = QLatin1String(R"(
insert into authors(name, birthdate) values(?, ?)
)");
const auto INSERT_BOOK_SQL = QLatin1String(R"(
insert into books(title, year, author, genre, rating)
values(?, ?, ?, ?, ?)
)");
const auto INSERT_GENRE_SQL = QLatin1String(R"(
insert into genres(name) values(?)
)");
QSqlError initDb()
{
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName(":memory:");
if (!db.open())
return db.lastError();
QStringList tables = db.tables();
if (tables.contains("books", Qt::CaseInsensitive)
&& tables.contains("authors", Qt::CaseInsensitive))
return QSqlError();
QSqlQuery q;
if (!q.exec(BOOKS_SQL))
return q.lastError();
if (!q.exec(AUTHORS_SQL))
return q.lastError();
if (!q.exec(GENRES_SQL))
return q.lastError();
if (!q.prepare(INSERT_AUTHOR_SQL))
return q.lastError();
QVariant asimovId = addAuthor(q, QLatin1String("Isaac Asimov"), QDate(1920, 2, 1));
QVariant greeneId = addAuthor(q, QLatin1String("Graham Greene"), QDate(1904, 10, 2));
QVariant pratchettId = addAuthor(q, QLatin1String("Terry Pratchett"), QDate(1948, 4, 28));
if (!q.prepare(INSERT_GENRE_SQL))
return q.lastError();
QVariant sfiction = addGenre(q, QLatin1String("Science Fiction"));
QVariant fiction = addGenre(q, QLatin1String("Fiction"));
QVariant fantasy = addGenre(q, QLatin1String("Fantasy"));
if (!q.prepare(INSERT_BOOK_SQL))
return q.lastError();
addBook(q, QLatin1String("Foundation"), 1951, asimovId, sfiction, 3);
addBook(q, QLatin1String("Foundation and Empire"), 1952, asimovId, sfiction, 4);
addBook(q, QLatin1String("Second Foundation"), 1953, asimovId, sfiction, 3);
addBook(q, QLatin1String("Foundation's Edge"), 1982, asimovId, sfiction, 3);
addBook(q, QLatin1String("Foundation and Earth"), 1986, asimovId, sfiction, 4);
addBook(q, QLatin1String("Prelude to Foundation"), 1988, asimovId, sfiction, 3);
addBook(q, QLatin1String("Forward the Foundation"), 1993, asimovId, sfiction, 3);
addBook(q, QLatin1String("The Power and the Glory"), 1940, greeneId, fiction, 4);
addBook(q, QLatin1String("The Third Man"), 1950, greeneId, fiction, 5);
addBook(q, QLatin1String("Our Man in Havana"), 1958, greeneId, fiction, 4);
addBook(q, QLatin1String("Guards! Guards!"), 1989, pratchettId, fantasy, 3);
addBook(q, QLatin1String("Night Watch"), 2002, pratchettId, fantasy, 3);
addBook(q, QLatin1String("Going Postal"), 2004, pratchettId, fantasy, 3);
return QSqlError();
}
#endif

View File

@ -0,0 +1,18 @@
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
#include "bookwindow.h"
#include <QtWidgets>
int main(int argc, char * argv[])
{
Q_INIT_RESOURCE(books);
QApplication app(argc, argv);
BookWindow win;
win.show();
return app.exec();
}