-
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>
宁德市网站建设_网站建设公司_安全防护_seo优化
!!! 禁止转载 !!! 如果需要你可以添加引用或链接。
理由:我认为由于恣意转载,现在国内的技术博客充斥着大量的重复内容,有些甚至是错误的,这不利于技术探索。