如何使用qss设置QLabel的样式表?

时间:2015-07-23 03:12:43

标签: c++ qt qtstylesheets qlabel

在我的代码中,我定制了QLabel并定义了点击的信号。我想通过qss文件设置QLabel的样式。似乎悬停状态有效,但新闻状态不起作用 谁知道出了什么问题?

dlabel.h

#ifndef DLABEL_H
#define DLABEL_H

#include <QtWidgets>
#include <QtGui>
#include <QtCore>

class DLabel : public QLabel
{
    Q_OBJECT

public:
    DLabel(QWidget *parent = 0);
    int labelId;
    ~DLabel();
protected:
    void enterEvent(QEvent *e);
    void leaveEvent(QEvent *e);
    void mousePressEvent(QMouseEvent *ev);
    void mouseReleaseEvent(QMouseEvent *ev);
public slots:
    void labelclick();
    void labelpressed();
private:
    bool m_enter;
    bool m_leave;
signals:
    void enterArea();
    void leaveArea();
    void clicked();
    void pressed();
    void labelclicked(int labelId);
};  

dlabel.cpp

#include "dlabel.h"

DLabel::DLabel(QWidget *parent)
    : QLabel(parent)
{
    connect(this, SIGNAL(clicked()), this, SLOT(labelclick()));
}

DLabel::~DLabel()
{

}
void DLabel::enterEvent(QEvent *e) {
   Q_UNUSED(e);
   emit enterArea();
}
void DLabel::leaveEvent(QEvent *e) {

   Q_UNUSED(e);
   emit leaveArea();
}
void DLabel::mousePressEvent(QMouseEvent *ev) {
    Q_UNUSED(ev);
    emit pressed();
}
void DLabel::mouseReleaseEvent(QMouseEvent *ev) {
    Q_UNUSED(ev);
    emit clicked();
}
void DLabel::labelclick() {
    emit labelclicked(labelId);
}
void DLabel::labelpressed() {

}

.qss文件

QLabel{
    background-color: transparent;
    color: white;
    /*border: 2px solid rgba(255, 255, 255, 100);*/
    /*border-radius: 32px;*/
    /*border-color: white*/
}
QLabel:hover{
    background-color: transparent;
    color: red;
    /*border: 2px solid rgba(255, 255, 255, 100);*/
    /*border-radius: 32px;*/
    /*border-color: white*/
}
QLabel:pressed{
    background-color: transparent;
    color: white;
    /*border: 2px solid rgba(255, 255, 255, 100);*/
    /*border-radius: 32px;*/
    /*border-color: white*/
}

0 个答案:

没有答案