Qt数据库编程

2023-11-06

Qt数据库编程


版本说明

版本 作者 日期 备注
0.1 loon 2018.10.25 初稿

目录

一、需求和目的

数据库在某些情况下,相对于文件操作更加方便,软件在某些情况下对于数据库也是必不可少的,因此,我们有必要了解和学习在Qt开发中如何使用数据库,由于sqlite文件数据库较为方便且Qt默认带有其引擎,所以以下总结基于sqlite。

二、使用说明

我们前面探索学习Qt时提到过帮助文档,那么如何使用Qt的SQL我们就来试一下,在Qt creator的帮助中搜索SQL,找到SQL Programming,里面提到一些建议和Database Classes以及这些类的用处:
在这里插入图片描述

1、Qt 5.6.0的数据库操作类:

These classes provide access to SQL databases.

QSql Contains miscellaneous identifiers used throughout the Qt SQL module
QSqlDatabase Represents a connection to a database
QSqlDriver Abstract base class for accessing specific SQL databases
QSqlDriverCreator Template class that provides a SQL driver factory for a specific driver type
QSqlDriverCreatorBase The base class for SQL driver factories
QSqlError SQL database error information
QSqlField Manipulates the fields in SQL database tables and views
QSqlIndex Functions to manipulate and describe database indexes
QSqlQuery Means of executing and manipulating SQL statements
QSqlQueryModel Read-only data model for SQL result sets
QSqlRecord Encapsulates a database record
QSqlRelationalTableModel Editable data model for a single database table, with foreign key support
QSqlResult Abstract interface for accessing data from specific SQL databases
QSqlTableModel Editable data model for a single database table

这些类包括在以下三层中:Driver Layer、SQL API Layer、User Interface Layer。

2、Driver Layer

驱动层是SQL api和特殊数据库之间的低等级的桥梁,不同的数据库其SQL api可能会有所不同,其支持的数据库种类包括:

QDB2 IBM DB2 (version 7.1 and above)
QIBASE Borland InterBase
QMYSQL MySQL
QOCI Oracle Call Interface Driver
QODBC Open Database Connectivity (ODBC) - Microsoft SQL Server and other ODBC-compliant databases
QPSQL PostgreSQL (versions 7.3 and above)
QSQLITE2 SQLite version 2
QSQLITE SQLite version 3
QTDS Sybase Adaptive Server Note: obsolete from Qt 4.7

我们要用的就是QSQLITE,支持SQLite3及其以上版本。关于如何编译对应的数据库,以插件形式加入到Qt,这里就不多说了,后续需要的话可以单独做个总结,这都是在帮助文档中有的。

在这里插入图片描述

3、SQL API Layer

These classes provide access to databases. Connections are made using the QSqlDatabase class. Database interaction is achieved by using the QSqlQuery class. In addition to QSqlDatabase and QSqlQuery, the SQL API layer is supported by QSqlError, QSqlField, QSqlIndex, and QSqlRecord.
这些类提供对数据库的访问。使用QSqlDatabase类建立连接。数据库交互是通过使用QSqlQuery类实现的。除了QSqlDatabase和QSqlQuery之外,SQL API层还支持QSqlError、QSqlField、QSqlIndex和QSqlRecord。

4、User Interface Layer

These classes link the data from a database to data-aware widgets. They include QSqlQueryModel, QSqlTableModel, and QSqlRelationalTableModel. These classes are designed to work with Qt’s model/view framework.
Note that a QCoreApplication object must be instantiated before using any of these classes.
这些类将数据从数据库链接到支持数据的小部件。它们包括QSqlQueryModel、QSqlTableModel和QSqlRelationalTableModel。这些类被设计用于Qt的模型/视图框架。
注意,QCoreApplication对象必须在使用这些类之前实例化。

5、Qt5.6.0 SQL programming使用流程

找到SQL programming,了解Qt如何进行SQL编程;
之后就是Connecting to Databases,看到如何连接数据库;
再接着 Executing SQL Statements,看如何执行sql语句;
再接着 Using the SQL Model Classes,使用SQL模块类进行数据查询等;
然后是Presenting Data in a Table View,将查询到的数据在表格视图中显示;
最后是Creating Data-Aware Forms,创建数据感知形式,以提交表单的形式插入数据或修改数据后在界面上呈现出来。

具体每一步在帮助中都描述的很清楚了,你只需要去阅读以下就可以了。

三、阅读及分析示例

OK,当你阅读完之后,我们再找个例子先大致看一下如何使用,就找Qt creator自带的,在示例中直接搜索SQL:

在这里插入图片描述

那么我们按照上面总结的流程去看一下每个例子,最后再自己写一个例子,看例子的过程中有不懂的API记得F1寻找帮助,这样的话,掌握Qt SQL Programming只是时间问题了。

下面就以第一个例子Books来说一下:

1、示例运行

打开示例,第一次打开需要点击配置进行项目配置,然后就可以编译运行了,运行结果如下:

在这里插入图片描述

2、源码分析

可以看到项目中有.pro工程文件、头文件、源文件、界面文件和资源。

首先看books.pro

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

target.path = $$[QT_INSTALL_EXAMPLES]/sql/books
INSTALLS += target


wince {
    CONFIG(debug, debug|release):sqlPlugins.files = $$QT_BUILD_TREE/plugins/sqldrivers/*d4.dll
    CONFIG(release, debug|release):sqlPlugins.files = $$QT_BUILD_TREE/plugins/sqldrivers/*[^d]4.dll
    sqlPlugins.path = sqldrivers
    INSTALLS += sqlPlugins
}

需要注意的时qmake需要添加sql,所以需要加上QT += sql,这个在Driver层中提到过,所以.pro中需要注意就是这个。

然后从main.cpp开始看起:

/****************************************************************************
**
** Copyright (C) 2015 The Qt Company Ltd.
** Contact: http://www.qt.io/licensing/
**
** This file is part of the demonstration applications of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL21$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see http://www.qt.io/terms-conditions. For further
** information use the contact form at http://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 or version 3 as published by the Free
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
** LICENSE.LGPLv3 included in the packaging of this file. Please review the
** following information to ensure the GNU Lesser General Public License
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** As a special exception, The Qt Company gives you certain additional
** rights. These rights are described in The Qt Company LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#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();
}

可以看到基类是widget,Q_INIT_RESOURCE(books)使用F1查看后发现是用来初始化资源文件books.qrc的。然后就是创建了win对象,那么我们接着去看BookWindow类吧。

bookwindow.h:

/****************************************************************************
**
** Copyright (C) 2015 The Qt Company Ltd.
** Contact: http://www.qt.io/licensing/
**
** This file is part of the demonstration applications of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL21$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see http://www.qt.io/terms-conditions. For further
** information use the contact form at http://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 or version 3 as published by the Free
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
** LICENSE.LGPLv3 included in the packaging of this file. Please review the
** following information to ensure the GNU Lesser General Public License
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** As a special exception, The Qt Company gives you certain additional
** rights. These rights are described in The Qt Company LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#ifndef BOOKWINDOW_H
#define BOOKWINDOW_H

#include <QtWidgets>
#include <QtSql>

#include "ui_bookwindow.h"


class BookWindow: public QMainWindow
{
    Q_OBJECT
public:
    BookWindow();

private:
    void showError(const QSqlError &err);
    Ui::BookWindow ui;
    QSqlRelationalTableModel *model;
    int authorIdx, genreIdx;
};

#endif

bookwindow.cpp:

/****************************************************************************
**
** Copyright (C) 2015 The Qt Company Ltd.
** Contact: http://www.qt.io/licensing/
**
** This file is part of the demonstration applications of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL21$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see http://www.qt.io/terms-conditions. For further
** information use the contact form at http://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 or version 3 as published by the Free
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
** LICENSE.LGPLv3 included in the packaging of this file. Please review the
** following information to ensure the GNU Lesser General Public License
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** As a special exception, The Qt Company gives you certain additional
** rights. These rights are described in The Qt Company LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#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"));

    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(), SIGNAL(currentRowChanged(QModelIndex,QModelIndex)),
            mapper, SLOT(setCurrentModelIndex(QModelIndex)));

    ui.bookTable->setCurrentIndex(model->index(0, 0));
}

void BookWindow::showError(const QSqlError &err)
{
    QMessageBox::critical(this, "Unable to initialize Database",
                "Error initializing database: " + err.text());
}

分析一下其构造函数的流程:1、设置驱动,选择数据库类型;2、初始化数据库;3、创建数据模型;4、记录列号;5、设置到其它数据库表的关系;6、填充模型;7、设置模型并隐藏ID列 ;8、初始化作者组合框 。

bookwindow.ui:

在这里插入图片描述

initdb.h:

/****************************************************************************
**
** Copyright (C) 2015 The Qt Company Ltd.
** Contact: http://www.qt.io/licensing/
**
** This file is part of the demonstration applications of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL21$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see http://www.qt.io/terms-conditions. For further
** information use the contact form at http://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 or version 3 as published by the Free
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
** LICENSE.LGPLv3 included in the packaging of this file. Please review the
** following information to ensure the GNU Lesser General Public License
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** As a special exception, The Qt Company gives you certain additional
** rights. These rights are described in The Qt Company LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#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, const QDate &birthdate)
{
    q.addBindValue(name);
    q.addBindValue(birthdate);
    q.exec();
    return q.lastInsertId();
}

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(QLatin1String("create table books(id integer primary key, title varchar, author integer, genre integer, year integer, rating integer)")))
        return q.lastError();
    if (!q.exec(QLatin1String("create table authors(id integer primary key, name varchar, birthdate date)")))
        return q.lastError();
    if (!q.exec(QLatin1String("create table genres(id integer primary key, name varchar)")))
        return q.lastError();

    if (!q.prepare(QLatin1String("insert into authors(name, birthdate) values(?, ?)")))
        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(QLatin1String("insert into genres(name) values(?)")))
        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(QLatin1String("insert into books(title, year, author, genre, rating) values(?, ?, ?, ?, ?)")))
        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

初始化数据库的流程是:1、重载addDatabase驱动为sqlite3;2、设置数据库名,这里“:memory:”代表存放在内存中,即程序完成后就释放掉了,如果要使用文件存储,则为文件路径;3、打开数据库;4、创建表格;5、插入数据,插入数据时对应用和数据库操作又做了一层封装。

最后是bookdelegate.cpp:

/****************************************************************************
**
** Copyright (C) 2015 The Qt Company Ltd.
** Contact: http://www.qt.io/licensing/
**
** This file is part of the demonstration applications of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL21$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see http://www.qt.io/terms-conditions. For further
** information use the contact form at http://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 or version 3 as published by the Free
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
** LICENSE.LGPLv3 included in the packaging of this file. Please review the
** following information to ensure the GNU Lesser General Public License
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** As a special exception, The Qt Company gives you certain additional
** rights. These rights are described in The Qt Company LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#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) {
        QStyleOptionViewItem opt = option;
        opt.rect.adjust(0, 0, -1, -1); // since we draw the grid ourselves
        QSqlRelationalDelegate::paint(painter, opt, 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;
        }
        drawFocus(painter, option, option.rect.adjusted(0, 0, -1, -1)); // since we draw the grid ourselves
    }

    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);

    return QSqlRelationalDelegate::sizeHint(option, index) + QSize(1, 1); // since we draw the grid ourselves
}

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->pos().x()
            - option.rect.x()) / star.width()), 5);
        model->setData(index, QVariant(stars));
        return false; //so that the selection can change
    }

    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;
}

可以看出其继承自QSqlRelationalDelegate,对于QSqlRelationalDelegate类根据帮助信息可以看出其是对QSqlRelationalTableModel中的数据进行显示和修改的一个类,所以该类的操作其实是对我们选择的数据模型的scoretable的重写操作。

四、最后

每个示例都代表了一个应用方向,时间允许的话可以都阅读分析一下,但是要掌握的核心内容就是如何使用数据库,然后显示数据库数据,搞清楚其流程然后自己写个例子熟悉一下,Qt的数据库编程基本上就没有什么大问题了。

其实整个的分析过程中我们可以将其看作一个拼图游戏,我们知道最终的效果,就像我们知道最终会是什么样子,然后我们去完成拼图,虽然单独某个图块不熟悉,但是只要方向不错,那么最终仍然会拼出结果。这些小的图块就类似于代码中的事件用法、c++语法、控件用法等,我们可能在初期对其不甚了解,但只要方向没错,最终总会得到想要的结果,只是时间长短而已,并且随着了解的深入,对每一个图块也会越来越熟悉。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Qt数据库编程 的相关文章

  • Qt常用部件介绍

    这里先给大家介绍 Designer 界面设计器 中例举的常用部件 以便对 Qt 的部件有一定认识 其具体用法后面再作介绍 布局管理组 Layouts 空间间隔组 弹簧 Spacers 按钮组 buttons 项目视图组 Item Views
  • QT CREATOR 插件开发:添加新的工程类型(下)

    Core BaseFileWizard 类Core BaseFileWizard在 coreplugin basefilewizard h 文件中声明 class CORE EXPORT BaseFileWizard public IWiz
  • Qt数据库编程

    Qt数据库编程 版本说明 版本 作者 日期 备注 0 1 loon 2018 10 25 初稿 目录 文章目录 Qt数据库编程 版本说明 目录 一 需求和目的 二 使用说明 1 Qt 5 6 0的数据库操作类 2 Driver Layer
  • 【Qt】QModbusDevice类

    1 概述 QModbusDevice类是Modbus类 QModbusServer和 QModbusClient的基类 Header include qmake QT serialbus Since Qt 5 8 Inherits QObj
  • Qt中的单例模式:实现一个单例的界面类

    文章目录 前言 一 什么是单例模式 二 单例模式的优缺点及使用场景 三 Qt中单例类的创建 四 单例类的使用 测试 总结 前言 本文主要讲述了使用加锁的懒汉式来实现单例 文中示例将一个界面类修改为单例类 并在主界面获取多次该类的实例来进行测
  • QT信号和槽

    系列文章目录 提示 这里可以添加系列文章的所有文章的目录 目录需要自己手动添加 例如 第一章 Python 机器学习入门之pandas的使用 提示 写完文章后 目录可以自动生成 如何生成可参考右边的帮助文档 文章目录 系列文章目录 前言 一
  • 自定义信号槽机制

    自定义信号槽机制 自定义信号 自定义槽 自定义信号和槽函数的使用 解决办法 如果想要在QT类中自定义信号槽 需要满足一些条件 并且有些事项也需要注意 要编写新的类并且让其继承Qt的某些标准类 这个新的子类必须从QObject类或者是QObj
  • Qt通讯 - MQTT

    部署 1 perl 要安装perl 可以参考perl 2 编译qt的MQTT源码 Qt MQTT SOURCECODE EMQX QMQTT 编译中可能遇到的问题 error
  • Qt中的QString与int、const char 、ASCII码互相转换

    1 QString 转 int bool ok QString str1 0xf8 int value1 str1 toInt ok 16 qDebug lt lt ok lt lt lt lt value1 true 248 QStrin
  • 基于Qt开发的游戏手柄小程序例子

    以前做过一个项目 用游戏手柄链接上位机软件 控制下位机执行一些机械动作 现在我将手柄控制的功能单独拿出来做了一个手柄检测的小程序 供开发者们拿去移植到自己的项目中用 这个程序是用 Qt creator5 12开发环境开发的 不过移植到VS
  • 什么是Qt信号槽机制

    1 信号和槽概述 信号槽是 Qt 框架引以为豪的机制之一 所谓信号槽 实际就是观察者模式 发布 订阅模式 当某个 事件 发生之后 比如 按钮检测到自己被点击了一下 它就会发出一个信号 signal 这种发出是没有目的的 类似广播 如果有对象
  • QT信号与槽的连接方式

    一 Qt AutoConnectionQt AutoConnection表示系统自动选择相应的连接方式 如果信号与槽在同一线程 就采用Qt DirectConnection 如果信号与槽不在同一线程 将采用Qt QueuedConnecti
  • Windows下的mingw-Qt开发环境安装及helloworld实现

    Windows下的mingw Qt开发环境安装及helloworld实现 我用的是Qt5 7 因此本次总结是基于Qt5 7 0的 我在自学的时候使用的IDE是Qt自带的Qt creator 上手简单 配置属于自己顺手的设置很方便 此外 如果
  • QT 解决“ qt creator 修改UI后,运行无改变”问题

    只需要将 项目 中的 Shadow build 勾选去掉 重新构建项目 运行即可看到修改后的效果
  • error: static assertion failed: Type is not registered, please use the Q_DECLARE_METATYPE macro to m

    error static assertion failed Type is not registered please use the Q DECLARE METATYPE macro to m 解决方案 报错信息如下 调用了类的静态函数导
  • Qt学习笔记——对release版本的.exe添加图标 程序发布

    Qt程序发布 1 将 ico图标文件拷贝到工程目录helloworld下 并重命名为Myico ico 2 在此工程目录下新建一个txt文档 输入 IDI ICON1 ICON DISCARDABLE Myico ico 另存为后缀名为pr
  • Qt创建一个自定义按钮

    1 概述 案例 编写一个自定义按钮 要求 1 给按钮添加自定义背景 2 监听按钮点击事件 2 代码案例 1 创建一个类让其继承QWidget 点击下一步下一步最后完成 2 打开MyPushButton 让其继承QPushButton 如下所
  • Qt QModbusTcpServer类

    1 概述 QModbusTcpServer类表示使用TCP服务器与Modbus客户端进行通信的Modbus服务器 Header include
  • Qmake VS Cmake 对比讲解

    用 cmake 构建Qt工程 对比qmake进行学习 cmake vs qmake qmake 是为 Qt 量身打造的 使用起来非常方便 cmake 使用上不如qmake简单直接 但复杂换来的是强大的功能 内置的 out of source
  • Qt Quick 工程创建

    一 简介 Qt Quick是Qt框架中的一个模块 用于创建现代 响应式的用户界面 它基于QML Qt Meta Object Language 语言和Qt Quick Controls库 提供了一种声明性的方式来构建用户界面 Qt Quic

随机推荐

  • Springboot使用Knife4j

    简述 knife4j是为Java MVC框架集成Swagger生成Api文档的增强解决方案 knife4j的前身是 swagger bootstrap ui 为了契合微服务的架构发展 由于原来 swagger bootstrap ui采用的
  • eclipse opengl java_eclipse安装openGL方法(完整版)

    学校上机使用在Windows上开发OpenGL 一般都会选择Visual Studio作为开发工具 不过我更喜欢Eclipse 在Windows上开发OpenGL所需的库一般会带有32这个后缀 跟Linux上的还不太一样 1 首先下载Ecl
  • 移入——归约技术

    归约 定义 我们可以将自底向上语法分析过程看成是建一个串w 归约 慰问发开始符号的过程 在归约中 一个与某产生式体相匹配的特定子串被替换为该产生式的头部的非终结符号 定义理解起来比较晦涩 我们来看个例子就知道了 已知有文法 E gt E T
  • SpringBoot slf4j的yaml日志配置不生效

    Spring boot工程中使用slf4j日志框架 发现日志配置总是不生效 我的yaml配置如下 logging level 全局日志级别 root info 具体到某个类的日志级别 打印所有访问请求日志 com xyz filter We
  • specialization of template.... in different namespace的解决

    代码来自DTL文档index htm struct Example tablename columnname int exampleInt DB EXAMPLE INT VALUE string exampleStr DB EXAMPLE
  • 理论总结

    作业1 2 1 Python程序是区分大小写的 2 解释程序对高级语言编写的程序是一边翻译 一边执行的 下次执行同样的程序时 还必须重新翻译 3 Python是一种用途广泛 解释型 面向对象的程序设计语言 4 程序设计语言包括机器语言 汇编
  • Into Clause VS Let Clause

    1 Into Clause 用来将select join 或者group的结果存储到一个临时变量中 目的 在之后的查询中需要使用此结果 例如 var developersGroupedByLanguage from d in develop
  • 面了一个测试工程师,明显感觉他背了很多面试题...

    最近有朋友去字节面试 面试前后进行了20天左右 包含4轮电话面试 1轮笔试 1轮主管视频面试 1轮hr视频面试 据他所说 80 的人都会栽在第一轮面试 要不是他面试前做足准备 估计都坚持不完后面几轮面试 其实 第一轮的电话面试除了一些常规的
  • LRU缓存淘汰算法

    概念理解 1 LRU是Least Recently Used的缩写 即最近最少使用页面置换算法 是为虚拟页式存储管理服务的 2 操作系统课程里有学过 在内存不够的场景下 淘汰就内容的策略 淘汰掉最不经常使用 LRU原理 可以用一个特殊的栈来
  • CentOS 7安装Gnome GUI 图形界面

    http www centoscn com image text config 2015 0528 5552 html
  • jwt在线解密工具分享

    前言 之前调用一个第三方api的时候 看到需要在Authorization填写bearer token 英文不好 看成了熊 bear 心里很疑惑 实际上 bearer 指的是持票人 Bearer Token用于授权访问资源 任何Bearer
  • splunk之获取数据(Ingesting Data)

    Ingesting Data 下载数据地址 http splk it f1data use uname in the Username field and 5p1unkbcup for the Password field
  • GDB调试命令详解

    GDB是什么 调试程序 程序中出现的语法错误可以借助编译器解决 但逻辑错误则只能靠自己解决 实际场景中解决逻辑错误最高效的方法 就是借助调试工具对程序进行调试 所谓调试 Debug 就是让代码一步一步慢慢执行 跟踪程序的运行过程 比如 可以
  • Vue如何实现反向代理(配置proxy)

    Vue如何实现反向代理 那问题来了 反向代理是什么 反向代理 Reverse Proxy 实际运行方式是指以代理服务器来接受internet上的连接请求 然后将请求转发给内部网络上的服务器 并将从服务器上得到的结果返回给internet上请
  • Mysql的B+树高度计算

    问题 假设B 树的高度是2 一行数据的记录大小是1K 主键ID是int类型 问 该B 树存放的总记录数 知识点 Mysql的默认存储引擎是Innodb Innodb的最小存储单位是页 一页大小等于16K B 树的叶子节点存放数据 内部节点存
  • 黑马 Spring_day01

    Spring day01 今日目标 掌握Spring相关概念 完成IOC DI的入门案例编写 掌握IOC的相关配置与使用 掌握DI的相关配置与使用 1 课程介绍 对于一门新技术 我们需要从为什么要学 学什么以及怎么学这三个方向入手来学习 那
  • 计算机提示d3dcompiler43.dll缺失怎么修复,多个解决方法分享

    在游戏玩家中 遇到游戏提示找不到 d3dcompiler43 dll 文件的情况并不罕见 这使得许多玩家在启动游戏时感到困扰 因为这意味着他们可能无法正常运行游戏 那么 d3dcompiler43 dll 文件到底是什么呢 为什么游戏会提示
  • 数据库实现学生管理系统

    1 QT将数据库分为三个层次 1 gt 数据库驱动层 QSqlDriver QSqlDriverCreator QSqlDriverCreatorBase QSqlDriverPlugin 2 gt sql接口层 QSqlDatabase
  • 在剪贴板上有大量信息,是否保留其内容, 以便此后粘贴到其他程序中? VBA 对策

    在剪贴板上有大量信息 是否保留其内容 以便此后粘贴到其他程序中 对策a 是文件关闭前 随便复制一个空单元格就可以了 对策b Application DisplayAlerts False 关闭任何提醒 但复制的信息将仍旧保存在剪贴板中 对策
  • Qt数据库编程

    Qt数据库编程 版本说明 版本 作者 日期 备注 0 1 loon 2018 10 25 初稿 目录 文章目录 Qt数据库编程 版本说明 目录 一 需求和目的 二 使用说明 1 Qt 5 6 0的数据库操作类 2 Driver Layer