QtCreator的一些自定义

本文介绍如何在Qt Creator中设置个性化配色方案,并通过CSS自定义代码区域以外的颜色,包括如何导入XML文件和使用QSS来调整界面外观。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、配色方案:
配置方法:工具->选项->文本编辑器->配色方案
如果对这些QtCreator自带的配色方案不满意,则需要手动进行添加
方法:
这些配色方案是以xml文件的形式存在于%QtCreatorDir%\share\qtcreator\styles\中的。
以下是一个vs风格的颜色方案
来自 
http://gurumeditation.org/files/VisualStudio.xml 
  1. <!-- Thanks for visiting http://gurumeditation.org -->
  2. <style-scheme version="1.0" name="VisualStudio">
  3. <style name="AddedLine" foreground="#00aa00"/>
  4. <style name="Comment" foreground="#008000"/>
  5. <style name="CurrentLine" foreground="#000000"/>
  6. <style name="CurrentLineNumber" foreground="#808080" bold="true"/>
  7. <style name="DiffFile" foreground="#000080"/>
  8. <style name="DiffLocation" foreground="#0000ff"/>
  9. <style name="DisabledCode" foreground="#000000" background="#efefef"/>
  10. <style name="Doxygen.Comment" foreground="#000080"/>
  11. <style name="Doxygen.Tag" foreground="#0000ff"/>
  12. <style name="Keyword" foreground="#0000ff"/>
  13. <style name="Label" foreground="#000000"/>
  14. <style name="LineNumber" foreground="#2b91af" background="#ffffff"/>
  15. <style name="Link" foreground="#0000ff"/>
  16. <style name="Number" foreground="#000000"/>
  17. <style name="Occurrences" foreground="#000000" background="#dcdcdc"/>
  18. <style name="Occurrences.Rename" foreground="#000000" background="#ffc8c8"/>
  19. <style name="Occurrences.Unused" foreground="#c0c0c0"/>
  20. <style name="Operator" foreground="#000000"/>
  21. <style name="Parentheses" foreground="#000000" bold="true"/>
  22. <style name="Preprocessor" foreground="#0000ff"/>
  23. <style name="RemovedLine" foreground="#ff0000"/>
  24. <style name="SearchResult" foreground="#000000" background="#ffef0b"/>
  25. <style name="SearchScope" foreground="#000000" background="#ffe8d5"/>
  26. <style name="Selection" foreground="#ffffff" background="#2b6ac5"/>
  27. <style name="String" foreground="#a31515"/>
  28. <style name="Text" foreground="#000000" background="#ffffff"/>
  29. <style name="Type" foreground="#0000ff"/>
  30. <style name="VisualWhitespace" foreground="#c0c0c0"/>
  31. </style-scheme>
上述代码中红色部分即为QtCreator颜色方案下拉菜单中的方案名称
另一个颜色方案
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <style-scheme version="1.0" name="MyDarkTheme">
  3.   <style name="AddedLine" foreground="#359f9f"/>
  4.   <style name="Comment" foreground="#2d9a9e"/>
  5.   <style name="CurrentLine" background="#232323"/>
  6.   <style name="CurrentLineNumber" foreground="#aaaaaa" bold="true"/>
  7.   <style name="DiffFile" foreground="#349e34"/>
  8.   <style name="DiffLocation" foreground="#89892d"/>
  9.   <style name="DisabledCode" foreground="#777777" background="#222222"/>
  10.   <style name="Doxygen.Comment" foreground="#3dbbbb"/>
  11.   <style name="Doxygen.Tag" foreground="#00a0a0"/>
  12.   <style name="Field" foreground="#618f5c"/>
  13.   <style name="Keyword" foreground="#acac39"/>
  14.   <style name="Label" foreground="#bfbf3f"/>
  15.   <style name="LineNumber" foreground="#888888" background="#232323"/>
  16.   <style name="Link" foreground="#0041c4"/>
  17.   <style name="Local" foreground="#696969"/>
  18.   <style name="Number" foreground="#8393c0"/>
  19.   <style name="Occurrences" background="#363636"/>
  20.   <style name="Occurrences.Rename" foreground="#ffaaaa" background="#553636"/>
  21.   <style name="Occurrences.Unused" foreground="#c0c0c0"/>
  22.   <style name="Operator" foreground="#aaaaaa"/>
  23.   <style name="Parentheses" foreground="#ff5555" background="#333333"/>
  24.   <style name="Preprocessor" foreground="#917bbc"/>
  25.   <style name="RemovedLine" foreground="#b43c3c"/>
  26.   <style name="SearchResult" background="#555500"/>
  27.   <style name="SearchScope" background="#222200"/>
  28.   <style name="Selection" foreground="#000000" background="#aaaaaa"/>
  29.   <style name="Static" foreground="#a69829"/>
  30.   <style name="String" foreground="#485abe"/>
  31.   <style name="Text" foreground="#a1a1a1" background="#000000"/>
  32.   <style name="Type" foreground="#317f13"/>
  33.   <style name="VirtualMethod" foreground="#a69174" italic="true"/>
  34.   <style name="VisualWhitespace" foreground="#c0c0c0"/>
  35. </style-scheme>
其中规则并不难,稍加学习理解即可以做出适合自己的颜色方案。

二、代码区外的颜色
上述颜色方案的配置仅影响代码编辑区域,若要对其余区域进行自定义,需要用到Qt中的CSS: QSS
一个可用的配置文件如下

  1. QMainWindow,
  2. QAbstractItemView,
  3. QTreeView::branch,
  4. QTabBar::tab{
  5.     color: #EAEAEA;
  6.     background: #333333;
  7.     font-size: 9pt;
  8. }

  9. QAbstractItemView::item:selected {
  10.     color: #EAEAEA;
  11.     background-color: #151515;
  12. }

  13. QScrollBar {
  14.      border: none;
  15.      background: #494949;
  16.      height: 6px;
  17.      width: 6px;
  18.      margin: 0px;
  19. }

  20. QScrollBar::handle{
  21.      background: #DBDBDB;
  22.      min-width: 20px;
  23.      min-height: 20px;
  24. }

  25. QScrollBar::add-line, QScrollBar::sub-line {
  26.      background: none;
  27.      border: none;
  28. }

  29. QScrollBar::add-page, QScrollBar::sub-page {
  30.      background: none;
  31. }

  32. QTreeView::branch:closed:adjoins-item:has-children {
  33.      background: solid #777777;
  34.      margin: 6px;
  35.      height: 6px;
  36.      width: 6px;
  37.      border-radius: 3px;
  38. }

  39. QTabBar::tab:selected {
  40.      font: bold;
  41.      border-color: #9B9B9B;
  42.      border-bottom-color: #C2C7CB;
  43.  }

  44. QTabBar::tab:!selected {
  45.      margin-top: 2px;
  46.  }

  47. QHeaderView::section {
  48.      background-color: qlineargradient(x1:0, y1:0, x2:0, y2:1,
  49.                                        stop:0 #616161, stop: 0.5 #505050,
  50.                                        stop: 0.6 #434343, stop:1 #656565);
  51.      color: white;
  52.      padding-left: 4px;
  53.      border: 1px solid #6c6c6c;
  54. }

  55. QToolBar {
  56.      border-style: solid;
  57.      border-style: outset;
  58.      color: #EAEAEA;
  59.      background: #333333;
  60.      font-size: 9pt;
  61. }
将上述内容保存为css文件运行如下命令命令:

  1. qtcreator.exe -stylesheet yourconfig.css
从如下两篇文章中可以获得更多QSS的知识:  Qt Style Sheets Reference Qt Style Sheets Examples  .Qt助手好像带有这两篇文章。
### 如何在 Qt Creator 中创建和添加自定义类 在 Qt Creator 中创建和添加自定义类的过程可以通过以下方式实现。首先,用户需要明确自定义类的类型(例如 C++ 类或 QML 组件)。以下是针对不同场景的具体方法: #### 1. 创建 C++ 自定义类 在 Qt Creator 中,可以直接通过集成的功能创建一个新的 C++ 类: - 在项目文件中右键点击,选择 `Add New...`。 - 在弹出的对话框中,选择 `C++ Class`[^3]。 - 输入类名,并根据需求选择基类(如 QObject 或 QWidget)。 - 点击完成之后,Qt Creator 会自动生成头文件(`.h`)和源文件(`.cpp`),并将其添加到项目的 `.pro` 文件中。 #### 2. 注册 C++ 类为 QML 可用 如果希望将自定义的 C++ 类注册为 QML 可用,需要使用 `qmlRegisterType` 函数。例如,在 `main.cpp` 中添加如下代码: ```cpp #include <QQmlApplicationEngine> #include <QQmlContext> qmlRegisterType<MyCppClass>("MyModule", 1, 0, "MyClass"); ``` 上述代码将 `MyCppClass` 注册为 QML 模块中的一个类型,模块名为 `MyModule`,版本为 `1.0`,QML 中的名称为 `MyClass`[^4]。 #### 3. 创建 QML 自定义组件 对于 QML 自定义组件,可以按照以下步骤操作: - 在项目目录下新建一个以 `.qml` 结尾的文件,例如 `MyCustomComponent.qml`。 - 在该文件中定义组件内容。例如: ```qml // MyCustomComponent.qml import QtQuick 2.15 Item { width: 100 height: 100 Rectangle { anchors.fill: parent color: "blue" } } ``` - 将该文件放置在资源文件(`.qrc`)中,或者直接放在项目目录下[^1]。 #### 4. 导入 QML 自定义组件 在其他 QML 文件中使用自定义组件时,可以通过以下方式导入: - 如果组件位于项目根目录或子目录中,可以直接使用其文件名作为类型名。例如: ```qml MyCustomComponent { x: 50 y: 50 } ``` - 如果组件位于特定路径下,可以使用 `import` 语句导入。例如: ```qml import "components" MyCustomComponent { x: 50 y: 50 } ``` #### 5. 添加第三方插件 如果需要添加第三方插件(如 qwt_designer_plugin.dll),可以参考以下步骤: - 确保插件是基于正确的编译环境(如 MSVC)编译的[^2]。 - 将插件文件复制到 Qt Creator 的插件目录下,例如 `tools\designer\plugins`。 - 重启 Qt Creator 后,插件即可生效。 ### 注意事项 - 在添加自定义类或组件时,请确保项目文件(`.pro` 或 CMakeLists.txt)已正确配置。 - 如果遇到加载失败的问题,检查路径是否正确以及插件是否与当前 Qt 版本兼容。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值