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,40 @@
# Copyright (C) 2022 The Qt Company Ltd.
# SPDX-License-Identifier: BSD-3-Clause
cmake_minimum_required(VERSION 3.16)
project(querymodel LANGUAGES CXX)
if(NOT DEFINED INSTALL_EXAMPLESDIR)
set(INSTALL_EXAMPLESDIR "examples")
endif()
set(INSTALL_EXAMPLEDIR "${INSTALL_EXAMPLESDIR}/sql/querymodel")
find_package(Qt6 REQUIRED COMPONENTS Core Gui Sql Widgets)
qt_standard_project_setup()
qt_add_executable(querymodel
../connection.h
customsqlmodel.cpp customsqlmodel.h
editablesqlmodel.cpp editablesqlmodel.h
main.cpp
)
set_target_properties(querymodel PROPERTIES
WIN32_EXECUTABLE TRUE
MACOSX_BUNDLE TRUE
)
target_link_libraries(querymodel PRIVATE
Qt6::Core
Qt6::Gui
Qt6::Sql
Qt6::Widgets
)
install(TARGETS querymodel
RUNTIME DESTINATION "${INSTALL_EXAMPLEDIR}"
BUNDLE DESTINATION "${INSTALL_EXAMPLEDIR}"
LIBRARY DESTINATION "${INSTALL_EXAMPLEDIR}"
)

View File

@ -0,0 +1,27 @@
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
#include <QtWidgets>
#include "customsqlmodel.h"
CustomSqlModel::CustomSqlModel(QObject *parent)
: QSqlQueryModel(parent)
{
}
//! [0]
QVariant CustomSqlModel::data(const QModelIndex &index, int role) const
{
QVariant value = QSqlQueryModel::data(index, role);
if (value.isValid() && role == Qt::DisplayRole) {
if (index.column() == 0)
return value.toString().prepend('#');
else if (index.column() == 2)
return value.toString().toUpper();
}
if (role == Qt::ForegroundRole && index.column() == 1)
return QVariant::fromValue(QColor(Qt::blue));
return value;
}
//! [0]

View File

@ -0,0 +1,21 @@
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
#ifndef CUSTOMSQLMODEL_H
#define CUSTOMSQLMODEL_H
#include <QSqlQueryModel>
//! [0]
class CustomSqlModel : public QSqlQueryModel
{
Q_OBJECT
public:
CustomSqlModel(QObject *parent = nullptr);
QVariant data(const QModelIndex &item, int role) const override;
};
//! [0]
#endif

View File

@ -0,0 +1,72 @@
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
#include <QtSql>
#include "editablesqlmodel.h"
EditableSqlModel::EditableSqlModel(QObject *parent)
: QSqlQueryModel(parent)
{
}
//! [0]
Qt::ItemFlags EditableSqlModel::flags(
const QModelIndex &index) const
{
Qt::ItemFlags flags = QSqlQueryModel::flags(index);
if (index.column() == 1 || index.column() == 2)
flags |= Qt::ItemIsEditable;
return flags;
}
//! [0]
//! [1]
bool EditableSqlModel::setData(const QModelIndex &index, const QVariant &value, int /* role */)
{
if (index.column() < 1 || index.column() > 2)
return false;
QModelIndex primaryKeyIndex = QSqlQueryModel::index(index.row(), 0);
int id = data(primaryKeyIndex).toInt();
clear();
bool ok;
if (index.column() == 1) {
ok = setFirstName(id, value.toString());
} else {
ok = setLastName(id, value.toString());
}
refresh();
return ok;
}
//! [1]
void EditableSqlModel::refresh()
{
setQuery("select * from person");
setHeaderData(0, Qt::Horizontal, QObject::tr("ID"));
setHeaderData(1, Qt::Horizontal, QObject::tr("First name"));
setHeaderData(2, Qt::Horizontal, QObject::tr("Last name"));
}
//! [2]
bool EditableSqlModel::setFirstName(int personId, const QString &firstName)
{
QSqlQuery query;
query.prepare("update person set firstname = ? where id = ?");
query.addBindValue(firstName);
query.addBindValue(personId);
return query.exec();
}
//! [2]
bool EditableSqlModel::setLastName(int personId, const QString &lastName)
{
QSqlQuery query;
query.prepare("update person set lastname = ? where id = ?");
query.addBindValue(lastName);
query.addBindValue(personId);
return query.exec();
}

View File

@ -0,0 +1,25 @@
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
#ifndef EDITABLESQLMODEL_H
#define EDITABLESQLMODEL_H
#include <QSqlQueryModel>
class EditableSqlModel : public QSqlQueryModel
{
Q_OBJECT
public:
EditableSqlModel(QObject *parent = nullptr);
Qt::ItemFlags flags(const QModelIndex &index) const override;
bool setData(const QModelIndex &index, const QVariant &value, int role) override;
private:
bool setFirstName(int personId, const QString &firstName);
bool setLastName(int personId, const QString &lastName);
void refresh();
};
#endif

View File

@ -0,0 +1,54 @@
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
#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");
model->setHeaderData(0, Qt::Horizontal, QObject::tr("ID"));
model->setHeaderData(1, Qt::Horizontal, QObject::tr("First name"));
model->setHeaderData(2, Qt::Horizontal, QObject::tr("Last name"));
}
QTableView* createView(QSqlQueryModel *model, const QString &title = "")
{
QTableView *view = new QTableView;
view->setModel(model);
static int offset = 0;
view->setWindowTitle(title);
view->move(100 + offset, 100 + offset);
offset += 20;
view->show();
return view;
}
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
if (!createConnection())
return EXIT_FAILURE;
QSqlQueryModel plainModel;
EditableSqlModel editableModel;
CustomSqlModel customModel;
initializeModel(&plainModel);
initializeModel(&editableModel);
initializeModel(&customModel);
createView(&plainModel, QObject::tr("Plain Query Model"));
createView(&editableModel, QObject::tr("Editable Query Model"));
createView(&customModel, QObject::tr("Custom Query Model"));
return app.exec();
}

View File

@ -0,0 +1,13 @@
HEADERS = ../connection.h \
customsqlmodel.h \
editablesqlmodel.h
SOURCES = customsqlmodel.cpp \
editablesqlmodel.cpp \
main.cpp
QT += sql widgets
requires(qtConfig(tableview))
# install
target.path = $$[QT_INSTALL_EXAMPLES]/sql/querymodel
INSTALLS += target