认识
The toolkit was called Qt because the letter Q looked appealing in Haavard’s Emacs typeface, and “t” was inspired by Xt, the X toolkit.
组成
QApplication, QGuiApplication and QCoreApplication
differences between QApplication, QGuiAppication, QCoreApplication classes
本文简单整理 QApplication, QGuiAppication, QCoreApplication 这三个对象之间的区别。
QCoreApplication | 基类。在命令行应用程序中,应该使用它。
QGuiApplication | 基类 + GUI 功能。在 QML 应用程序中,应该使用它。「QML」是一种用户界面标记语言,是一种指令使的语言,与 CSS 有些相似。
QApplication | 基类 + GUI + 对 Widget 支持。在 QtWidgets 应用程序中,应该使用它。
Event loop | 字面上翻译就是「事件循环」,详细的解释需要阅读文档和一些 Qt 相关的书籍。但简而说:事件循环是一个无限循环,它在应用程序的后台运行,处理从操作系统传入的事件(鼠标移动,点击,绘制事件,硬件事件等)以及内部通信(信号和插槽)。调用app.exec()时,事件循环开始,这也是“在执行app.exec()后,后续代码不会再继续执行”的原因。
性质
应用调试
环境变量:
QT_DEBUG_PLUGINS=1 | … make Qt print out diagnostic information about the each (C++) plugin it tries to load.
QML_IMPORT_TRACE=1 | … make QML print out diagnostic information from the import loading mechanism.
提供输入法支持
https://doc.qt.io/qt-6/qinputmethod.html
Fcitx 与 Qt 的协作方式主要有两种,代表了演进的历史:
- 延迟高:需要经过 X 服务器中转,路径长。
- 不稳定:容易因为程序或输入法的崩溃而导致另一方出现问题(焦点丢失、卡死等)。
- 功能有限:难以实现复杂的预编辑文本显示(如带下划线的拼音)和候选词窗口精确定位。
现代方式:IBus 兼容接口 或 Fcitx 自有 Qt 模块
构建
Qt Creator, Qt Designer, Qt Quick Designer
-「Wikipedia/Qt Creator#Editors」
-「Qt Designer vs Qt Quick Designer vs Qt Creator?」
Qt Creator 是 Qt 的 IDE,它大大简化了 Qt 的开发;
Qt Designer 是一个图形工具,可以让您构建 QWidget GUI;
Qt Quick Designer 与 Qt Designer 类似,但用于构建基于 QML 的 GUI;
Qt Quick Designer 与 Qt Designer 两者都内置于 Qt Creator;
安装 Qt Creator 工具
#!/bin/sh ################################################################################ # Kali GNU/Linux Rolling # # # Ubuntu 14.04 QtCreator Qt5 examples missing # # https://askubuntu.com/questions/450983/ubuntu-14-04-qtcreator-qt5-examples-missing # # Getting Started With Qt and Qt Creator on Linux # # https://www.ics.com/blog/getting-started-qt-and-qt-creator-linux # ################################################################################ # 安装基础 apt-get install qtcreator # 安装示例(不同的示例可能在不同的包中),该命令并未安装说有的示例包; apt-get install qtbase5-examples qtbase5-doc-html qtwebengine5-examples
应用
使 TabWidget 按钮居中
-「QTabwidget’s tab text alignment not working」
# 创建按钮
QLabel * button = new QLabel("text");
button->setAlignment(Qt::AlignLeft);
# 将原有的文本置空
ui->tabWidget->tabBar()->setTabText(index, "");
# 将按钮添加到组件上
ui->tabWidget->tabBar()->setTabButton(index, QTabBar::LeftSide, button);
参考
DeepSeek / Fcitx 输入法 与 其他 Qt 程序是如何协同工作的?