是否可以将 Qt 样式表与 Qt Creator 中的升级小部件一起使用? [英] Is it possible to use Qt Style Sheets with promoted widgets in Qt Creator?

查看:53
本文介绍了是否可以将 Qt 样式表与 Qt Creator 中的升级小部件一起使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Qt 样式表对标准小部件进行大量重新设计.因此,在通过 #objectName 选择器为不同的小部件手动完成大部分工作后,我决定以某种方式对相似的小部件进行分组.

I'm trying to do some heavy redeisgning of standard widgets using Qt Style Sheets. So after doing most of it manually for different widgets by #objectName selectors I've decided to group similar widgets in some way.

例如,我有多个 QFrames,它们的作用类似于内部表单中的标题.我希望它们都具有相同的样式表.一种方法是使用命名约定(这是我的后备变体),即 QFrame[objectName|="prefix_"].但我想按类对我的小部件进行分组.所以我创建了简单的占位符类:

For example, I have multiple QFrames which act like headers in inner forms. And I want all of them to have the same stylesheet. One way of doing that is using naming convention (this is my fallback variant), i.e. QFrame[objectName|="prefix_"]. But I wanted to group my widgets by class. So I created simple placeholder class:

class HeaderFrame: public QFrame
{
public:
    HeaderFrame(QWidget *parent = NULL): QFrame(parent) {}
};

这让我可以将所有这些 QFrames 提升为 HeaderFrames.之后我尝试设置

Which allowed me to promote all these QFrames to HeaderFrames. After that I've tried setting

HeaderFrame { background-color: red; }

样式表到 MainWindow 对象本身(使其作用于所有 HeaderFrames)但它不起作用.QtCreator 表单设计器中没有任何变化,编译应用程序后也没有任何变化.我尝试了这个样式表的不同变体,但没有任何效果.

stylesheet to MainWindow object itself (to make it act on all HeaderFrames) but it won't work. Nothing is changed in QtCreator form designer and nothing is changed after compiling an application. I've tried different variants of this stylesheet but nothing works.

那么,是否只有 Qt 小部件(如 QLabel、QFrame 等)可用于以这种方式设置样式?或者有什么方法可以为您推广的小部件编写样式表?

So, is only Qt widgets (like QLabel, QFrame, etc.) are available for styling this way? Or there is some way to write stylesheet for your promoted widget?

推荐答案

是的,这是可能的.您应该记住的唯一一件事 - 派生小部件的基础应该支持样式表,并仔细重新实现它们的 PaintEvent.

yes,it is possible. The only thing you should keep in mind - base for your derived widgets should support style sheets, and reimplement their PaintEvent carefully.

更新:例子班级:

class Header1Label : public QLabel
{
    Q_OBJECT
    public:
    Header1Label(QWidget *parent = 0):QLabel(parent){};
    ~Header1Label(){};
};

样式表:

Header1Label
{
    font-size:14px;
    font-weight:900;    
}

这篇关于是否可以将 Qt 样式表与 Qt Creator 中的升级小部件一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆