QSS中使用qproperty-<property name>设置Qt对象属性值的例子

本文详细介绍了在Qt中使用QSS设置对象属性的方法,特别是针对QPixmap类型的属性设置,强调了配置WRITE属性的必要性,并通过一个具体的MyLabel类实例展示了如何正确实现这一过程。

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

对于在QSS中设置Qt对象属性值,QSS官方文档有如下描述

但在实际使用时,使用qproperty-pixmap这种方式设置属性时遇到过一些问题,特在此处总结一下,如下面的例程:

mylabel.h

#ifndef MYLABEL_H
#define MYLABEL_H

#include <QLabel>

class MyLabel : public QLabel
{
    Q_OBJECT
    //经测试,QPixmap类型必须配置WRITE,否则编译时会出现,如下错误:
    //error: no match for 'operator!=' (operand types are 'QPixmap' and 'QPixmap')
    Q_PROPERTY(QPixmap face MEMBER m_face WRITE setFace)

    //QString, int等,不需要配置WRITE也可以正常使用
    Q_PROPERTY(QString name MEMBER m_name)
public:
    explicit MyLabel(QWidget *parent = 0);

    void setFace(const QPixmap &pixmap);

protected:
    void paintEvent(QPaintEvent *event);

private:
    QPixmap m_face;
    QString m_name;
};

#endif // MYLABEL_H

mylabel.cpp

#include "mylabel.h"
#include <QPainter>

MyLabel::MyLabel(QWidget *parent)
    : QLabel(parent)
{

}

void MyLabel::setFace(const QPixmap &pixmap)
{
    m_face=pixmap;
}

void MyLabel::paintEvent(QPaintEvent *event)
{
    QPainter p(this);
    p.drawPixmap(0, 0, width(), height(), m_face);
    QPen pen=p.pen();
    pen.setColor(Qt::red);
    p.setPen(pen);
    QFont font=p.font();
    font.setPixelSize(25);
    font.setBold(true);
    p.setFont(font);
    p.drawText(QRect(0, 0, width(), height()), Qt::AlignCenter, m_name);
}

mainwidget.h

#ifndef MAINWIDGET_H
#define MAINWIDGET_H

#include <QWidget>

namespace Ui {
class MainWidget;
}

class MainWidget : public QWidget
{
    Q_OBJECT

public:
    explicit MainWidget(QWidget *parent = 0);
    ~MainWidget();

private slots:
    void on_buttonSetup_clicked();

private:
    Ui::MainWidget *ui;
};

#endif // MAINWIDGET_H

mainwidget.cpp

#include "mainwidget.h"
#include "ui_mainwidget.h"

MainWidget::MainWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::MainWidget)
{
    ui->setupUi(this);
}

MainWidget::~MainWidget()
{
    delete ui;
}

void MainWidget::on_buttonSetup_clicked()
{
    this->setStyleSheet(ui->textStyleSheet->toPlainText());
}

运行效果(点击Setup按钮后的运行效果):

(-------------完-----------)

<?xml version="1.0" encoding="UTF-8"?> <ui version="4.0"> <class>MainWindow</class> <widget class="QMainWindow" name="MainWindow"> <property name="geometry"> <rect> <x>0</x> <y>0</y> <width>826</width> <height>565</height> </rect> </property> <property name="windowTitle"> <string>智能家居控制面板</string> </property> <widget class="QWidget" name="centralwidget"> <layout class="QVBoxLayout" name="verticalLayout"> <item> <layout class="QHBoxLayout" name="horizontalLayout_top"/> </item> <item> <layout class="QHBoxLayout" name="horizontalLayout_main"> <item> <layout class="QVBoxLayout" name="verticalLayout_left"> <item> <widget class="QGroupBox" name="groupBox_connection"> <property name="title"> <string>MQTT连接设置</string> </property> <layout class="QFormLayout" name="formLayout"> <item row="0" column="0"> <widget class="QLabel" name="label"> <property name="text"> <string>服务器:</string> </property> </widget> </item> <item row="0" column="1"> <widget class="QLineEdit" name="hostname"> <property name="text"> <string>mqtt.yyzlab.com.cn</string> </property> </widget> </item> <item row="1" column="0"> <widget class="QLabel" name="label_2"> <property name="text"> <string>端 口:</string> </property> </widget> </item> <item row="1" column="1"> <layout class="QHBoxLayout" name="horizontalLayout_port"> <item> <widget class="QLineEdit" name="le_port"> <property name="text"> <string>1883</string> </property> </widget> </item> <item> <widget class="QPushButton" name="pb_connect"> <property name="text"> <string>连接</string> </property> </widget> </item> </layout> </item> <item row="2" column="0"> <widget class="QLabel" name="label_3"> <property name="text"> <string>发布主题:</string> </property> </widget> </item> <item row="2" column="1"> <widget class="QLineEdit" name="pub_topic"> <property name="text"> <string>1753019767598/APP2AIOTSIM</string> </property> </widget> </item> <item row="3" column="0"> <widget class="QLabel" name="label_6"> <property name="text"> <string>订阅主题:</string> </property> </widget> </item> <item row="3" column="1"> <widget class="QLineEdit" name="sub_topic"> <property name="text"> <string>1753019767598/AIOTSIM2APP</string> </property> </widget> </item> <item row="4" column="0"> <widget class="QLabel" name="label_sensor_pub"> <property name="text"> <string>传感器发布主题:</string> </property> </widget> </item> <item row="4" column="1"> <widget class="QLineEdit" name="sensor_pub_topic"> <property name="text"> <string>1753019767598/Device2AIOTSIM</string> </property> </widget> </item> </layout> </widget> </item> <item> <widget class="QGroupBox" name="groupBox_control"> <property name="title"> <string>设备控制</string> </property> <layout class="QGridLayout" name="gridLayout_control"> <item row="0" column="0"> <layout class="QVBoxLayout" name="verticalLayout_led1"> <item> <widget class="QPushButton" name="pb_led"> <property name="text"> <string>开灯</string> </property> </widget> </item> <item> <widget class="QLabel" name="label_4"> <property name="text"> <string>电灯1</string> </property> <property name="alignment"> <set>Qt::AlignCenter</set> </property> </widget> </item> </layout> </item> <item row="0" column="1"> <layout class="QVBoxLayout" name="verticalLayout_led2"> <item> <widget class="QPushButton" name="pb_led_2"> <property name="text"> <string>开灯</string> </property> </widget> </item> <item> <widget class="QLabel" name="label_5"> <property name="text"> <string>电灯2</string> </property> <property name="alignment"> <set>Qt::AlignCenter</set> </property> </widget> </item> </layout> </item> <item row="0" column="2"> <layout class="QVBoxLayout" name="verticalLayout_led3"> <item> <widget class="QPushButton" name="pb_led_3"> <property name="text"> <string>开灯</string> </property> </widget> </item> <item> <widget class="QLabel" name="label_7"> <property name="text"> <string>电灯3</string> </property> <property name="alignment"> <set>Qt::AlignCenter</set> </property> </widget> </item> </layout> </item> <item row="1" column="0" colspan="3"> <layout class="QHBoxLayout" name="horizontalLayout_fan"> <item> <layout class="QVBoxLayout" name="verticalLayout_fan"> <item> <widget class="QPushButton" name="pb_fan"> <property name="text"> <string>风扇开</string> </property> </widget> </item> <item> <widget class="QPushButton" name="pb_doorlock"> <property name="text"> <string>门锁开</string> </property> </widget> </item> </layout> </item> <item> <layout class="QVBoxLayout" name="verticalLayout_fan_speed"> <item> <widget class="QPushButton" name="pb_fan_speed_up"> <property name="text"> <string>风扇加速</string> </property> </widget> </item> <item> <widget class="QPushButton" name="pb_fan_speed_down"> <property name="text"> <string>风扇减速</string> </property> </widget> </item> </layout> </item> <item> <layout class="QVBoxLayout" name="verticalLayout_beeper"> <item> <widget class="QPushButton" name="pb_beeper"> <property name="text"> <string>蜂鸣器开</string> </property> </widget> </item> <item> <widget class="QPushButton" name="disable"> <property name="text"> <string>关闭全部</string> </property> </widget> </item> </layout> </item> </layout> </item> </layout> </widget> </item> <item> <widget class="QGroupBox" name="groupBox_fan"> <property name="title"> <string>风扇设置</string> </property> <layout class="QFormLayout" name="formLayout_fan"> <item row="0" column="0"> <widget class="QLabel" name="label_fan_speed_title"> <property name="text"> <string>当前风速:</string> </property> </widget> </item> <item row="0" column="1"> <widget class="QLabel" name="label_fan_speed"> <property name="text"> <string>风速:--</string> </property> <property name="alignment"> <set>Qt::AlignCenter</set> </property> </widget> </item> <item row="1" column="0"> <widget class="QLabel" name="label_fan_speed_topic"> <property name="text"> <string>风速主题:</string> </property> </widget> </item> <item row="1" column="1"> <widget class="QLineEdit" name="fan_speed_topic"/> </item> </layout> </widget> </item> </layout> </item> <item> <layout class="QVBoxLayout" name="verticalLayout_right"> <item> <widget class="QGroupBox" name="groupBox_sensor"> <property name="title"> <string>传感器数据</string> </property> <layout class="QGridLayout" name="gridLayout_sensor"> <item row="1" column="0"> <widget class="QLabel" name="label_light"> <property name="text"> <string>光照:-- lux</string> </property> <property name="alignment"> <set>Qt::AlignCenter</set> </property> </widget> </item> <item row="1" column="1"> <widget class="QLabel" name="label_people"> <property name="text"> <string>人体红外:无人</string> </property> <property name="alignment"> <set>Qt::AlignCenter</set> </property> </widget> </item> <item row="0" column="0"> <widget class="QLabel" name="label_tem"> <property name="text"> <string>温度:-- ℃</string> </property> <property name="alignment"> <set>Qt::AlignCenter</set> </property> </widget> </item> <item row="0" column="1"> <widget class="QLabel" name="label_hum"> <property name="text"> <string>湿度:-- %</string> </property> <property name="alignment"> <set>Qt::AlignCenter</set> </property> </widget> </item> </layout> </widget> </item> <item> <widget class="QGroupBox" name="groupBox_camera"> <property name="title"> <string>摄像头</string> </property> <layout class="QVBoxLayout" name="verticalLayout_camera"> <item> <layout class="QHBoxLayout" name="horizontalLayout_camera"> <item> <widget class="QLabel" name="label_10"> <property name="text"> <string>摄像头</string> </property> </widget> </item> <item> <widget class="QPushButton" name="pb_camera"> <property name="text"> <string>开启摄像头</string> </property> </widget> </item> </layout> </item> <item> <widget class="QLabel" name="camera_label"> <property name="minimumSize"> <size> <width>400</width> <height>300</height> </size> </property> <property name="styleSheet"> <string notr="true">border: 2px solid gray;</string> </property> <property name="text"> <string>摄像头预览区域</string> </property> <property name="alignment"> <set>Qt::AlignCenter</set> </property> </widget> </item> </layout> </widget> </item> </layout> </item> </layout> </item> </layout> </widget> <widget class="QMenuBar" name="menubar"> <property name="geometry"> <rect> <x>0</x> <y>0</y> <width>826</width> <height>28</height> </rect> </property> </widget> <widget class="QStatusBar" name="statusbar"/> </widget> <resources/> <connections/> </ui> 对按钮和布局添加合适的颜色
最新发布
07-22
<?xml version="1.0" encoding="UTF-8"?> <ui version="4.0"> <class>Login</class> <widget class="QWidget" name="Login"> <property name="windowModality"> <enum>Qt::ApplicationModal</enum> </property> <property name="geometry"> <rect> <x>0</x> <y>0</y> <width>652</width> <height>385</height> </rect> </property> <property name="windowTitle"> <string>图书馆管理系统</string> </property> <widget class="QLabel" name="label"> <property name="geometry"> <rect> <x>20</x> <y>20</y> <width>111</width> <height>41</height> </rect> </property> <property name="text"> <string><html><head/><body><p><span style=" font-size:16pt;">用户登录</span></p></body></html></string> </property> <property name="textFormat"> <enum>Qt::AutoText</enum> </property> </widget> <widget class="QLabel" name="user"> <property name="geometry"> <rect> <x>150</x> <y>90</y> <width>60</width> <height>41</height> </rect> </property> <property name="text"> <string><html><head/><body><p><span style=" font-size:12pt;">用户名</span></p></body></html></string> </property> </widget> <widget class="QLabel" name="password"> <property name="geometry"> <rect> <x>160</x> <y>150</y> <width>60</width> <height>26</height> </rect> </property> <property name="text"> <string><html><head/><body><p><span style=" font-size:12pt;">密码</span></p></body></html></string> </property> </widget> <widget class="QLabel" name="identify"> <property name="geometry"> <rect> <x>140</x> <y>180</y> <width>91</width> <height>59</height> </rect> </property> <property name="text"> <string><html><head/><body><p><span style=" font-size:12pt;">身份类型</span></p></body></html></string> </property> </widget> <widget class="QLineEdit" name="userline"> <property name="geometry"> <rect> <x>290</x> <y>100</y> <width>191</width> <height>21</height> </rect> </property> </widget> <widget class="QLineEdit" name="pwline"> <property name="geometry"> <rect> <x>290</x> <y>150</y> <width>191</width> <height>21</height> </rect> </property> <property name="echoMode"> <enum>QLineEdit::Password</enum> </property> </widget> <widget class="QComboBox" name="idbox"> <property name="geometry"> <rect> <x>320</x> <y>200</y> <width>121</width> <height>22</height> </rect> </property> <item> <property name="text"> <string>读者</string> </property> </item> <item> <property name="text"> <string>图书管理员</string> </property> </item> <item> <property name="text"> <string>系统管理员</string> </property> </item> </widget> <widget class="QPushButton" name="loginbt"> <property name="geometry"> <rect> <x>250</x> <y>270</y> <width>93</width> <height>28</height> </rect> </property> <property name="text"> <string>登录</string> </property> </widget> <widget class="QPushButton" name="exitbt"> <property name="geometry"> <rect> <x>420</x> <y>270</y> <width>93</width> <height>28</height> </rect> </property> <property name="text"> <string>退出</string> </property> </widget> </widget> <resources/> <connections/> </ui>只修改当前代码美化页面,额外说明一下我发的代码用于python项目
05-15
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值