QtCreator的一些自定义

本文介绍如何在Qt Creator中设置个性化配色方案,并通过CSS自定义代码区域以外的颜色,包括如何导入XML文件和使用QSS来调整界面外观。
一、配色方案:
配置方法:工具->选项->文本编辑器->配色方案
如果对这些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助手好像带有这两篇文章。
<think>我们正在讨论在Qt Creator中添加和使用自定义窗口控件。根据引用[2]和引用[4],我们可以通过以下步骤实现: 1. **创建自定义控件**:首先,我们需要创建一个自定义控件。这通常是通过继承Qt的现有控件类(如QWidget)并重写其方法来实现的(引用[3])。 2. **将自定义控件编译为插件**:为了让Qt Creator能够识别自定义控件,我们需要将其编译为插件。这通常涉及到编写一个插件类,并实现Qt的插件接口(引用[2], [4])。 3. **在Qt Creator中注册插件**: - 将编译好的插件文件(通常是动态库,如.so、.dll)放置到Qt Creator的插件目录中(引用[2])。 - 重启Qt Creator,然后进入UI设计界面,点击“工具” -> “Form Editor” -> “About Qt Designer Plugins”,在弹窗中点“刷新”(引用[2])。 4. **使用自定义控件**: - 刷新后,在控件盒子的“自定义窗口部件”部分就可以找到自定义控件(引用[4])。 - 然后就可以像标准控件一样拖拽到窗体上使用(引用[2], [4])。 5. **在项目中使用自定义控件**: - 如果要在项目中使用自定义控件,还需要在项目中添加对自定义控件源文件和头文件的引用(引用[5])。 - 对于使用qmake的项目,可以通过添加外部库(引用自定义控件的项目文件)的方式(引用[5])。 具体步骤: ### 步骤1:创建自定义控件 假设我们有一个自定义控件类`MyCustomWidget`,继承自`QWidget`。 ### 步骤2:创建插件 编写一个插件类,例如: ```cpp #include <QtDesigner/QDesignerCustomWidgetInterface> class MyCustomWidgetPlugin : public QObject, public QDesignerCustomWidgetInterface { Q_OBJECT Q_PLUGIN_METADATA(IID "org.qt-project.Qt.QDesignerCustomWidgetInterface") Q_INTERFACES(QDesignerCustomWidgetInterface) public: MyCustomWidgetPlugin(QObject *parent = nullptr); QString name() const override; QString group() const override; QString toolTip() const override; QString whatsThis() const override; QString includeFile() const override; QIcon icon() const override; bool isContainer() const override; QWidget *createWidget(QWidget *parent) override; }; ``` 然后实现这些函数,其中`createWidget`函数返回自定义控件的实例。 ### 步骤3:编译插件 在.pro文件中添加: ```qmake TEMPLATE = lib CONFIG += plugin designer ``` 然后编译生成插件库。 ### 步骤4:将插件放入Qt Creator的插件目录 将生成的插件库文件复制到Qt Creator的设计器插件目录,例如: - Windows: `Qt安装目录/Tools/QtCreator/bin/plugins/designer` - Linux: `/usr/lib/x86_64-linux-gnu/qtcreator/plugins/designer` 或类似路径 ### 步骤5:在Qt Creator中使用 重启Qt Creator,打开UI文件,在控件箱中就可以看到自定义控件。 ### 步骤6:在项目中引用自定义控件 如果自定义控件作为插件使用,在UI设计时拖拽添加后,编译项目时需要确保项目链接了自定义控件的库。或者按照引用[5]的方法,将自定义控件的源文件添加到项目中(不推荐,因为这样每次修改控件都需要重新编译整个项目),或者将控件编译为动态库,然后在项目中链接。 对于使用qmake的项目,可以在.pro文件中添加: ```qmake LIBS += -L/path/to/your/plugin -lMyCustomWidget INCLUDEPATH += /path/to/your/headers ``` 注意:以上步骤适用于Qt5和Qt6,但具体路径可能因版本和操作系统而异。 §§相关问题§§ 1. 如何创建一个自定义Qt控件? 2. 如何将自定义控件编译为Qt Creator可识别的插件? 3. 在Qt项目中如何使用自定义控件插件? 4. 自定义控件在Qt Creator中不显示怎么办? 5. 如何为自定义控件添加信号和槽?</think>### Qt Creator 添加和使用自定义窗口控件指南 #### 一、核心步骤 1. **创建自定义控件** - 继承 Qt 控件基类(如 `QWidget`),重写事件处理、绘制逻辑等 - 示例代码框架: ```cpp class CustomWidget : public QWidget { Q_OBJECT // 必须包含宏 public: explicit CustomWidget(QWidget *parent = nullptr); // 添加自定义方法和属性... protected: void paintEvent(QPaintEvent *event) override; // 重写其他事件处理... }; ``` 2. **编译为插件** - 创建插件类继承 `QDesignerCustomWidgetInterface` - 在 `.pro` 文件中添加配置: ```qmake TEMPLATE = lib CONFIG += plugin designer TARGET = $$qtLibraryTarget(customwidgetplugin) ``` 3. **注册到 Qt Creator** - 将生成的插件文件(`.dll`/`.so`)放入 Qt Creator 插件目录: - Windows: `Qt安装路径/Tools/QtCreator/bin/plugins/designer` - Linux: `/usr/lib/qtcreator/plugins/designer` - **重启 Qt Creator** 后进入设计模式[^2] 4. **在 UI 中使用** - 打开 UI 文件(`.ui`) - 在控件箱底部 **"自定义窗口部件"** 区域找到您的控件 - 直接拖拽到窗体即可使用[^4] #### 二、项目集成方法 1. **qmake 项目(推荐)** ```qmake # 在项目.pro文件中添加 LIBS += -L/path/to/plugin -lcustomwidget INCLUDEPATH += /path/to/headers ``` 2. **CMake 项目** ```cmake target_link_libraries(your_app PRIVATE customwidget) include_directories(/path/to/headers) ``` #### 三、验证使用效果 1. 在 `mainwindow.cpp` 中手动创建: ```cpp #include "customwidget.h" // ... CustomWidget *widget = new CustomWidget(this); ui->verticalLayout->addWidget(widget); ``` #### 四、常见问题解决 - **控件不显示**:检查插件路径是否正确,重启后点击 **工具 → Form Editor → About Qt Designer Plugins → 刷新**[^2] - **编译错误**:确保插件与 Qt Creator 使用相同编译器版本 - **运行时崩溃**:检查控件的资源初始化(如 Q_INIT_RESOURCE) > 关键提示:自定义控件支持完整的事件处理、信号槽机制和样式表定制,可通过重写 `Q_SIGNALS` 和 `Q_SLOTS` 增强交互性[^3]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值