当前位置: 首页 > news >正文

Qt 小技巧和解决方案合集(持续更新)

  • Q_GLOBAL_STATIC 宏用于创建非 POD 类型的全局静态变量。它确保仅在第一次访问时创建静态变量实例,并提供线程安全的 exists 和 isDestroyed 函数。

  • 当 QMenu 同时拥有 QWidgetAction 和 QAction,此时从 QAction 移动到 QWidgetAction,QAction 的状态仍为 selected 状态,这可以通过为 QWidgetAction 的 Widget 调用 setMouseTracking(true) 来解决。

  • 设置占位符文本的颜色:

    QLineEdit* edt = new QLineEdit();
    QPalette palette = edt->palette();
    palette.setColor(QPalette::PlaceholderText, QColor(195, 200, 213));
    edt->setPalette(palette);
    
  • 实现渐变色滑块的进度条:

    QProgressBar {border-radius: 4px 4px 4px 4px;background: #4378FF;
    }
    QProgressBar::chunk {background: qlineargradient(x1:0, y1:0, x2:1, y2:0, stop:0 rgba(0,0,0,0), stop: 0.5 #00d1ff, stop:1 rgba(0, 0,0,0));
    }
    
  • 可以通过为控件设置 mask 来从外部裁切它的绘制区域,以实现异形绘制的效果。不过该效果不支持抗锯齿:

    auto frame = new QWidget(parent, Qt::Popup);
    frame->setStyleSheet("background-color: red; border: 1px solid green; border-radius: 6px;");QPainterPath path;
    path.addRoundedRect(frame->rect(), 6, 6);
    frame->setMask(path.toFillPolygon().toPolygon());frame->show();
    
  • 可以通过 QSS 设置一些复合 Widget 的子部件的位置:

    QComboBox {border: 1px solid gray;border-radius: 3px;padding: 1px 18px 1px 3px;min-width: 6em;
    }QComboBox:on { /* shift the text when the popup opens */padding-top: 3px;padding-left: 4px;
    }QComboBox::drop-down {subcontrol-origin: padding;subcontrol-position: center right; /*	position: absolute; */right: 10px;width: 15px;height: 10px;border-left: 1px solid darkgray;border-top-right-radius: 3px; /* same radius as the QComboBox */border-bottom-right-radius: 3px;border-right: 1px solid red;
    }QComboBox::down-arrow:on { /* shift the arrow when popup is open */top: 1px;left: 1px;
    }
    
  • 通过添加配置参数 CONFIG += no_keywords 可取消 Qt 自身的关键字定义,改用 Q_SIGNALS 等 Q_ 开头的宏。

  • 临时取消关键字定义,如 signals:

    // 以下头文件关键字冲突, 临时取消 signals
    #undef signals
    #include <gdk/gdkx.h>
    #include <gtk/gtk.h>
    #include <X11/Xlib.h>
    #define signals
    
  • 如果需要立即刷新小部件的画面,其中一种方案是参考 QAbstractButton::mousePressEvent 方法的做法:

    repaint(); //flush paint event before invoking potentially expensive operation
    QApplication::flush();
    
  • 查看 QStyleSheetStyle::setProperties 的源码可以了解到 qss 关键字 qproperty-*** 所支持的数据类型。例如:

    qproperty-[QRect]: rect(X Y W H);
    qproperty-[QSize]: wpx hpx;
    
  • 当 QMenu 上没有可见的 action 时调用 popup 或 exec 方法将出现警告 setMouseGrabEnabled: Not setting mouse grab for invisible window QWidgetWindow/‘QMenuClassWindow’,可以通过调用 isEmpty 方法来检查 QMenu 是否为空来规避。

  • 在鼠标悬停或离开小部件后 QApplication 会自动为小部件设置或移除 WA_UnderMouse 属性,参见 QApplicationPrivate::notify_helper 源码。

  • ToolTip 实现换行和插入图标:

    // ToolTip 支持 HTML
    setToolTip("Hello <font color='red'><b>World</b></font>"
    "<ul>"
    "<li>Item1</li>"
    "<li>Item2</1i>"
    "<1i>Item3</1i>"
    "</ul>");
    setToolTip("<img src=':/files/images/stop.png'> Hello world");
    

    进一步的用法可以参见:https://www.youtube.com/watch?v=X9JD8gKGZ00

  • 通过在 pro 文件中添加 QT += ***-private 可使用相应 Qt 模块的私有 API,但这将会导致你的工程与编译时所使用的 Qt 版本相绑定。

  • 可以将 qt.conf 文件嵌入至程序的资源文件中:

    <!DOCTYPE RCC><RCC version="1.0"><qresource prefix="/qt/etc/"><file>qt.conf</file></qresource>
    </RCC>
    
http://www.jsqmd.com/news/111568/

相关文章:

  • es: minimum_should_match的作用
  • mpg123 库播放mp3
  • 2025最新虾青素保健品PQQ口服抗衰产品公司首推育培生:循证医学赋能,精准守护健康本源 - 全局中转站
  • 第二十一
  • ArchLinux 禁用 nouveau
  • Linux的PWM子系统简析
  • 第十六
  • 实用指南:Spring Boot、Redis、RabbitMQ 在项目中的核心作用详解
  • 第十七
  • VXDIAG VCX-FD GM Intelligent All-in-One Diagnostic Tool for Chevrolet, Buick, Cadillac, Opel, Holden
  • 10.31澳洲网安法规与现有法规对比
  • 通达信判断是指数
  • 第十八
  • 2025最新补精生精产品补肾壮阳补品品牌top10推荐!循证医学精准营养优质品牌榜单发布,助力两性健康养护 - 全局中转站
  • 2025最新补精生精产品补肾壮阳补品品牌top10推荐!循证医学精准营养优质品牌榜单发布,助力两性健康养护 - 全局中转站
  • 12-18午夜盘思
  • Cursor 开源替代
  • uni-app + DevEco 鸿蒙跨平台应用开发实战1-环境安装分享 - 实践
  • VXDIAG VCX SE DOIP Diagnostic Tool: 13-in-1 for 13+ European/American Car Brands
  • 揭秘:如何用0.02/张调用Openai官方GPT Image 1.5?国内直连方案大公开
  • IOI 2026 中国国家集训队作业(试题泛做)个人题解
  • Spring Boot 学习笔记:项目搭建、分层架构与核心技术集成 - 详解
  • 鼻窦炎
  • xinference初探
  • 20251218
  • Manacher 算法详解:线性时间求最长回文子串
  • 海南封关,你应该知道的10个新常识
  • 搜维尔科技:全新上线!Xsens Link机器人套装和Xsens Humanoid软件:专为机器人创新者打造
  • Linux核心命令 - 十里
  • 10379_基于SSM的校园跑腿服务平台