stylesheet无效

来源:互联网 发布:手机号抓取软件 编辑:程序博客网 时间:2024/05/11 18:15
//sheetstyle file, style.cssQWidget#setting_widget{    background-color: white;}//child widgetclass CSubWidget : public QWidget{    Q_OBJECTpublic:    explicit CSettingWidget(QWidget *parent = 0);};CSettingWidget::CSettingWidget(QWidget *parent)    : QWidget(parent){    setWindowFlags(Qt::WindowStaysOnTopHint);    setObjectName("subwidget");}//main widgetclass MainWindow : public QWidget{    Q_OBJECTpublic:    ~MainWindow();    explicit MainWindow(QWidget *parent = 0);private:    CSubWidget *m_pSubWidget;};MainWindow::MainWindow(QWidget *parent) :    QWidget(parent){    QFile file(":/style.css");    file.open(QIODevice::ReadOnly);    qApp->setStyleSheet(file.readAll());    m_pSubWidget = new CSubWidget(this);}

以上会发现, CSubWidget 设置的窗体样式是无效的,

查看文档会发现

QWidgetSupports only the background, background-clip and background-origin properties.If you subclass from QWidget, you need to provide a paintEvent for your custom QWidget as below:
void CustomWidget::paintEvent(QPaintEvent *){    QStyleOption opt;    opt.init(this);    QPainter p(this);    style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);}
The above code is a no-operation if there is no stylesheet set.Warning: Make sure you define the Q_OBJECT macro for your custom widget.

为CSubWidget 加上paintEvent就可以解决

0 0
原创粉丝点击