{"id":31149449,"url":"https://github.com/sprinfall/qt-study","last_synced_at":"2026-05-18T09:06:14.017Z","repository":{"id":83655354,"uuid":"149246114","full_name":"sprinfall/qt-study","owner":"sprinfall","description":"Qt study notes and examples.","archived":false,"fork":false,"pushed_at":"2020-08-06T09:50:32.000Z","size":151,"stargazers_count":3,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-09-18T18:51:34.473Z","etag":null,"topics":["cplusplus","qt","qt5","study-notes"],"latest_commit_sha":null,"homepage":null,"language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/sprinfall.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2018-09-18T07:25:26.000Z","updated_at":"2023-07-12T07:24:45.000Z","dependencies_parsed_at":"2023-03-12T19:04:39.105Z","dependency_job_id":null,"html_url":"https://github.com/sprinfall/qt-study","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/sprinfall/qt-study","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sprinfall%2Fqt-study","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sprinfall%2Fqt-study/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sprinfall%2Fqt-study/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sprinfall%2Fqt-study/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sprinfall","download_url":"https://codeload.github.com/sprinfall/qt-study/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sprinfall%2Fqt-study/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":33172173,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-18T05:43:36.989Z","status":"ssl_error","status_checked_at":"2026-05-18T05:43:19.133Z","response_time":71,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cplusplus","qt","qt5","study-notes"],"created_at":"2025-09-18T17:52:16.944Z","updated_at":"2026-05-18T09:06:14.012Z","avatar_url":"https://github.com/sprinfall.png","language":"C++","funding_links":[],"categories":[],"sub_categories":[],"readme":"## 安装配置\n\n### Using Qt5 with CMake\n\n官方文档：http://doc.qt.io/qt-5/cmake-manual.html\n\n为了让 CMake 能够找到 Qt，在 CMake 里添加变量 `CMAKE_PREFIX_PATH`，比如：\n\n```\nCMAKE_PREFIX_PATH = C:\\Qt\\5.10.1\\msvc2013_64\n```\n\n注意：必须具体到特定的版本，仅指定安装根目录（`C:\\Qt`）是不够的。\n\n注意：`CMAKE_PREFIX_PATH` 可以指定多个目录，以分号分隔。但是 `CMAKE_PREFIX_PATH` 的类型需为 `FILEPATH` 而非 `STRING`。可以指定多个目录是你的程序可能还要寻找其他程序库，比如 VTK，这时候你应该把 VTK 的安装目录也加进来。\n\n当然，`CMAKE_PREFIX_PATH` 也可以为系统的环境变量，有时候这样会更方便，避免了每次 Configure 时都去设置。\n\n### Qt DLLs\n\n大家用的基本上都是动态库版的 Qt，你的程序需要找到 Qt 的 DLL，比如一个最简单 QtWidgets 程序就需要三个 DLL：\n\n* Qt5Widgets.dll （Debug 版为 Qt5Widgetsd.dll，下同）\n* Qt5Gui.dll\n* Qt5Core.dll\n\n最直接的方法就是添加 Qt 的 `bin` 目录（比如 `D:\\Qt\\5.10.1\\msvc2015_64\\bin`）到 `PATH` 环境变量。然后，必须重启 Windows 才能生效。当然，也可以把需要的 DLLs 拷贝到你的 EXE 所在目录，但那样太麻烦了。在开发过程中，添加 `PATH` 环境变量就足够了。\n\n### AUTOMOC\n\n用 CMake 作为 Qt 项目的构建系统时，经常要加上以下两句：\n\n```cmake\n# Find includes in corresponding build directories.\nset(CMAKE_INCLUDE_CURRENT_DIR ON)\n\n# Instruct CMake to run moc automatically when needed.\nset(CMAKE_AUTOMOC ON)\n```\n\n现在，假如有下面一个类的定义，声明了 `Q_OBJECT`，希望被 `moc` 程序处理，那么，`MyWidget` 的声明**必须位于头文件**，否则 `moc` 处理过程会报错！\n\n```cpp\nclass MyWidget : public QWidget {\n  Q_OBJECT\n\npublic:\n  MyWidget(QWidget* parent = nullptr) : QWidget(parent) {\n    setWindowTitle(tr(\"My Widget\"));\n  }\n};\n```\n\n因为我写的是一个例子程序，本想简单起见，全部代码放在同一个源文件里，结果就遇到了这种诡异的事情。\n\n## 核心\n\n### Console Output\n\n```cpp\n#include \u003cQTextStream\u003e\n\n// Print to stdout.\n// Example: qStdOut() \u003c\u003c \"std out!\";\nQTextStream\u0026 qStdOut() {\n  static QTextStream ts(stdout);\n  return ts;\n}\n```\n\n参考：https://stackoverflow.com/a/3886128\n\n### OS Macros\n\nE.g.,\n\n```cpp\n#include \u003cQtGlobal\u003e\n\n#ifdef Q_OS_WIN32\n```\n\nOr:\n\n```cpp\n#if defined (Q_OS_WIN32) || defined (Q_OS_MACOS)\n```\n\n### 编译时检查 Qt 版本\n\n比如 `QFontMetrics` 在 5.11 新引入的 `horizontalAdvance()`:\n\n```cpp\n#if (QT_VERSION \u003e= QT_VERSION_CHECK(5, 11, 0))\n  qInfo() \u003c\u003c metrics.horizontalAdvance(text);\n#endif\n```\n\n### 访问环境变量 Access Environment Variables\n\nhttp://doc.qt.io/qt-5/qtglobal.html#qgetenv\n\n```cpp\nQByteArray qgetenv(const char *varName)\n\nbool qputenv(const char *varName, const QByteArray \u0026value)\n```\n\n需要包含头文件 `\u003cQtGlobal\u003e`。\n\n### 系统标准路径 System Standard Paths\n\nhttp://doc.qt.io/qt-5/qstandardpaths.html\n\nQt 提供了类 `QStandardPaths`，类似于 wxWidgets 的 `wxStandardPaths`。\n\n### Qt 资源系统 Resource System\n\nhttps://doc.qt.io/qt-5/resources.html\n\n\n## GUI \u0026 Widgets\n\n### Screen Rectangle\n\n```cpp\nQRect screenGeometry = QApplication::desktop()-\u003escreenGeometry(this);\n```\n\n### Child Window Rectangle\n\n对子窗口来说，`geometry()` 返回的是相对于父窗口的坐标，通过 `mapFromParent()` 转换后，左上点才为 `(0, 0)`。\n\n而 `rect()` 返回的才是正常的 rectangle。\n\n```cpp\nconst QRect\u0026 rect = geometry();\nQPoint tl = mapFromParent(rect.topLeft());\n```\n\n### 设置控件背景色 Set Widget Background Color\n\nhttps://wiki.qt.io/How_to_Change_the_Background_Color_of_QWidget\n\n```cpp\nMyWidget::MyWidget(QWidget* parent)\n    : QWidget(parent), log_text_edit_(nullptr) {\n  // Set background.\n  QPalette pal = palette();\n  pal.setColor(QPalette::Background, Qt::GlobalColor::red);\n  setAutoFillBackground(true);\n  setPalette(pal);\n}\n```\n\n### 显示消息框 Show Message Box\n\n```cpp\nint reply = QMessageBox::question(\n                this,\n                tr(\"Test\"),\n                tr(\"Are you OK with it?\"),\n                QMessageBox::Yes | QMessageBox::No);\n\nif (reply == QMessageBox::Yes) {\n    qDebug() \u003c\u003c \"Yes was clicked.\";\n} else if (reply == QMessageBox::No) {\n    qDebug() \u003c\u003c \"No was clicked.\";\n} else {\n    qDebug() \u003c\u003c \"Canceled.\";\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsprinfall%2Fqt-study","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsprinfall%2Fqt-study","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsprinfall%2Fqt-study/lists"}