QtSql编程

SQL编程概述 这些类提供了访问SQL数据库的接口。 类名 描述 QSql 包含了在整个Qt SQL模块中使用的各种标识符 QSqlDatabase 处理数据库的连接 QSqlDriver 用于访问特定 SQL 数据库的抽象基类 QSqlDriverCreator 提供特定驱动程序类型的 SQL 驱动程序工厂的模板类 QSqlDriverCreatorBase SQL 驱动程序工厂的基类 QSqlError SQL数据库错误信息 QSqlField 操作 SQL 数据库表和视图中的字段 QSqlIndex 用于操作和描述数据库索引的函数 QSqlQuery 执行和操作 SQL 语句的方法 QSqlQueryModel QSql查询模型 QSqlRecord f封装数据库记录 QSqlRelationalTableModel 具有外键支持的单个数据库表的可编辑数据 QSqlResult 用于访问特定 SQL 数据库中数据的抽象接口 QSqlTableModel 单个数据库表的可编辑数据模型 SQL类分为以下三层: 驱动层 包含以下类 QSqlDriver, QSqlDriverCreator, QSqlDriverCreatorBase, QSqlDriverPlugin, 和 QSqlResult。 这一层提供了特定数据库和 SQL API 之间的底层桥梁。 SQL API层 这些类提供对数据库的访问。数据库连接通过 QSqlDatabase 类建立。数据库交互则通过 QSqlQuery 类实现。除了 QSqlDatabase 和 QSqlQuery 之外,SQL API 层还支持 QSqlError、QSqlField、QSqlIndex 和 QSqlRecord。 ...

2026年4月3日 21:49:10 · 4 分钟 · 710 字 · 向洵

Arch linux安装教程

Arch Linux官方镜像下载 国内镜像推荐 中国境内的镜像站 使用Ventoy制作启动盘 Ventoy文档 连接到互联网 确保启用了网络接口 1 ip link 连接wifi 连接wifi使用iwd pacman -S iwd 进入交互式提示符 iwctl 列出wifi设备 [iwd]# device list 扫描wifi station wlan0 scan 连接wifi [iwd]# station wlan0 connect “wifi名” 更新系统时间 timedatectl 创建硬盘分区 列出分区 fdisk -l 使用分区工具进行分区 cfdisk /dev/要被分区的磁盘 分区方案 挂载点 分区 分区类型 注释 建议大小 /boot /dev/efi系统分区 EFI system EFI系统分区 至少1GB [SWAP] /dev/swap系统分区 Linux swap 交换空间 至少4GB / /dev/root分区 Linux root(x86-64) 根目录 剩余空间(大于50GB够用) 格式化分区 1 2 3 # mkfs.ext4 /dev/root_partition(根分区) # mkfs.fat -F 32 /dev/efi_system_partition(EFI 系统分区) # mkswap /dev/swap_partition(交换空间分区) 挂载分区 1 2 3 # mount /dev/root_partition(根分区) /mnt # mount --mkdir /dev/efi_system_partition /mnt/boot (挂载EFI系统分区) # swapon /dev/swap_partition(启用交换空间分区) 安装系统 选择镜像 需安装的软件包会从文件/etc/pacman.d/mirrorlist中所列的镜像站下载。下载软件包时,列表中越靠前的镜像站会拥有越高的优先级。 ...

2026年3月29日 11:52:25 · 2 分钟 · 238 字 · 向洵

C++算法训练

Calculate average 计算平均值 描述 Write a function which calculates the average of the numbers in a given array. Note: Empty arrays should return 0. 我的解决方案 1 2 3 4 5 6 7 8 9 10 11 12 13 #include <vector> double calc_average(const std::vector<double>& values) { if(values.empty()) return 0.0; double sum{}; for(const auto& v : values) { sum += v; } return sum / values.size(); } 另外的解决方案 1 2 3 4 5 6 7 8 9 #include <vector> #include <numeric> double calc_average(const std::vector<double>& values) { if(values.empty()) return 0.0; double res = std::accumulate(vec.begin(), vec.end(), 0) / values.size(); return res } 1 2 3 4 double calc_average(const std::vector<double>& values) { return values.empty() ? 0.0 : accumulate(values.begin(), values.end(), 0.0) / values.size(); } 要点与总结 vector可以通过empty()函数判断是否为空(或使用size()方法与0比较) ...

2026年3月22日 14:40:46 · 9 分钟 · 1875 字 · 向洵

C++笔记

左值引用 const的左值引用 非const的引用不可以绑定到const变量上 1 2 3 4 5 6 7 int main() { const int x { 5 }; // x is a non-modifiable (const) lvalue int& ref { x }; // error: ref can not bind to non-modifiable lvalue return 0; } const 的左值引用也可以绑定到可修改的左值 1 2 3 4 5 6 7 #include <iostream> int main() { int x { 5 }; const int& ref { x }; // 不可以通过ref修改x return 0; } 绑定到临时对象的常量引用延长了临时对象的生命周期 1 2 3 4 5 6 7 8 9 10 11 #include <iostream> int main() { const int& ref { 5 }; // int& ref { 5 } 是不合法的,因为非const左值引用只能绑定到左值 std::cout << ref << '\n'; // Therefore, we can safely use it here return 0; } // Both ref and the temporary object die here 通过左值引用传递 一些类类型复制成本高昂,特别是当我们几乎会立即销毁这些副本时。例如: ...

2026年3月22日 10:38:58 · 6 分钟 · 1234 字 · 向洵

C++基础

变量赋值和初始化 变量赋值 1 2 int width; // 定义一个被命名为width的整型变量 width = 5; // 将5赋值到width上 默认情况下,赋值会将=运算符右侧的值复制到运算符左侧的变量中。这称为复制赋值。 变量初始化 不同形式的初始化 1 2 3 4 5 6 7 8 9 int a; // 默认初始化 // 传统初始化风格 int b = 5; // 复制初始化 int c ( 6 ); // 直接初始化 // 现代初始化风格 int d { 7 }; // 列表初始化 int e {}; // value-initialization (empty braces) 默认初始化 当没有提供初始化器时(例如上面的变量 a),这称为默认初始化。在许多情况下,默认初始化不执行任何初始化,并使变量具有不确定值 复制初始化 继承自C语言风格 直接初始化 没啥卵用 1 int width(5); 列表初始化 列表初始化有两种形式: 1 2 int width { 5 }; // 推荐使用 int height = { 6 }; // 使用少 值初始化和零初始化 当使用一组空的花括号初始化变量时,会发生一种特殊的列表初始化形式,称为值初始化。在大多数情况下,值初始化会隐式将变量初始化为零(或给定类型最接近零的值)。在发生置零的情况下,这称为零初始化。 1 int width {}; // 值初始化和零初始化 问:我应该用 { 0 } 还是 {} 来初始化? 当实际使用初始值时,使用直接列表初始化 ...

2026年3月21日 10:09:15 · 7 分钟 · 1442 字 · 向洵

cmake常用指令

git初始化 1 git init // git 仓库初始化 .gitignore 书写 1 foldername/ // 忽略整个目录

2026年3月20日 17:02:29 · 1 分钟 · 13 字 · 向洵

Qt Examples

计算器示例注释 caculator.h 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 #pragma once #include <QWidget> QT_BEGIN_NAMESPACE class QLineEdit; QT_END_NAMESPACE class Button; //! [0] class Calculator : public QWidget { Q_OBJECT public: Calculator(QWidget *parent = nullptr); private slots: void digitClicked(); void unaryOperatorClicked(); void additiveOperatorClicked(); void multiplicativeOperatorClicked(); void equalClicked(); void pointClicked(); void changeSignClicked(); void backspaceClicked(); void clear(); // 将sumSoFar 重置为零 void clearAll(); // MC void clearMemory(); // MR void readMemory(); // MS void setMemory(); // M+ void addToMemory(); //! [0] //! [1] private: //! [1] //! [2] template<typename PointerToMemberFunction> Button *createButton(const QString &text, const PointerToMemberFunction &member); void abortOperation(); bool calculate(double rightOperand, const QString &pendingOperator); // 含存储在计算器内存中的数值(使用MS,M+, 或MC )。 double sumInMemory; // 存储当前累积的数值。当用户点击= 时,sumSoFar 将被重新计算并显示在显示屏上。 double sumSoFar; // 在进行乘除运算时存储临时值。 double factorSoFar; // 存储用户最后点击的加法运算符? QString pendingAdditiveOperator; // 存储用户最后点击的乘法运算符? QString pendingMultiplicativeOperator; // 当计算器等待用户输入操作数时,true 。 bool waitingForOperand; QLineEdit *display; enum { NumDigitButtons = 10 }; Button *digitButtons[NumDigitButtons]; }; caculator.cpp ...

2026年3月20日 16:18:01 · 8 分钟 · 1538 字 · 向洵

QtWidgets指南

创建一个简易的窗口 1 2 3 4 5 6 7 8 9 int main(int argc, char *argv[]) { QApplication a(argc, argv); QWidget w; w.resize(800, 600); w.setWindowTitle("你好 服了"); w.show(); return a.exec(); } 使用布局 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 int main(int argc, char *argv[]) { QApplication app(argc, argv); QWidget window; QLabel *label = new QLabel(QApplication::translate("windowlayout", "Name:")); QLineEdit *lineEdit = new QLineEdit(); QHBoxLayout *layout = new QHBoxLayout(); layout->addWidget(label); layout->addWidget(lineEdit); window.setLayout(layout); window.setWindowTitle("草了"); window.show(); return app.exec(); } 上面在window上设置布局window.setLayout(layout)时,两个部件和布局本身都被 “重新父对象化”,成为窗口的子窗口。 qt嵌套布局 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 #include <QtWidgets> int main(int argc, char *argv[]) { QApplication app(argc, argv); QWidget window; QLabel *queryLabel = new QLabel( QApplication::translate("nestedlayouts", "Query:")); QLineEdit *queryEdit = new QLineEdit(); QTableView *resultView = new QTableView(); QHBoxLayout *queryLayout = new QHBoxLayout();、 // 这里将queryLabel和queryEdit加入到布局中,但此时这些组件还没有父组件 queryLayout->addWidget(queryLabel); queryLayout->addWidget(queryEdit); QVBoxLayout *mainLayout = new QVBoxLayout(); // 将布局添加到另外一个布局时,则子布局的父对象是是其父布局 mainLayout->addLayout(queryLayout); mainLayout->addWidget(resultView); // 当给一个组件设置布局时,其布局内的所有组件都成为其子组件 // 但queryLayout的父组件还是mainLayout,这个关系没有发生改变 // mainLayout由于没有父布局,则其父组件变为了window window.setLayout(mainLayout); // Set up the model and configure the view... window.setWindowTitle( QApplication::translate("nestedlayouts", "Nested layouts")); window.show(); return app.exec(); } Qt元对象系统 元对象系统基于以下三个方面: ...

2026年3月15日 15:22:43 · 1 分钟 · 201 字 · 向洵

C++输入输出

通过cin(istream)读取输入 1 2 char buf[10]{}; std::cin >> buf; // 如果输入超过10个字符会缓冲区溢出 可以使用setw限制输入,以下代码会读取前9个字符,最后一个字符用于\0。 需要注意的是cin通过»读取遇到空白字符会结束读取。 1 2 3 #include <iomanip> char buf[10]{}; std::cin >> std::setw(10) >> buf; 提取和空白符 1 2 3 4 5 6 7 8 int main() { char ch{}; while (std::cin >> ch) std::cout << ch; return 0; } 当用户输入以下内容时 Hello my name is Alex 提取运算符会跳过空格和换行符。因此,输出是 HellomynameisAlex 通过get函数读取 1 2 3 4 5 6 7 8 int main() { char ch{}; while (std::cin.get(ch)) std::cout << ch; return 0; } 当用户输入以下内容时 Hello my name is Alex 不会跳过空格和换行符。输出是 Hello my name is Alex ...

2026年3月14日 15:53:32 · 5 分钟 · 882 字 · 向洵

我的第一篇博客

hugo的使用命令 1 2 3 hugo new posts/文章名字.md # 添加文章 hugo new posts/文章名字/index.md # 用于叶子资源捆绑 hugo server -D # 启动本地服务器 1 2 3 int main() { return 0; } Markdown语法 标题语法 这是一级标题 这是二级标题 这是三级标题 一级标题可选语法 二级标题可选语法 段落语法 Don’t put tabs or spaces in front of your paragraphs. Keep lines left-aligned like this. 换行语法 First line with two spaces after. And the next line. 强调语法 这是粗体 这是斜体 引用语法 我是一个引用 列表语法 有序列表 第一项 第二项 第三项 无序列表 我 还 爱 她 天 人 合 一 代码语法 行内代码 At the command prompt, type 我是行内代码. ...

2026年3月13日 19:54:56 · 1 分钟 · 151 字 · 向洵