Qt qss issue summary

1. Set up the setObjectName in QWidget, it is ineffective.

   Solution rewrite paintEvent.

 

#ifndef BROWSEWIDGET_H
#define BROWSEWIDGET_H

#include <QObject>
#include <QWidget>
#include <QLabel>

class BrowseWidget : public QWidget
{
    Q_OBJECT
public:
    explicit BrowseWidget(QWidget *parent = nullptr);

signals:
protected:
    void paintEvent(QPaintEvent *event);
public slots:
private:
    void initUI();
};

#endif // BROWSEWIDGET_H
#include "browsewidget.h"
#include<QVBoxLayout>
#include<QHBoxLayout>
#include <QStylePainter>
#include <QStyleOption>
BrowseWidget::BrowseWidget(QWidget *parent) : QWidget(parent)
{
    init ();
    this->setObjectName("BrowseWidget");
}
void BrowseWidget::paintEvent(QPaintEvent *event)
{
    QStylePainter painter(this);
    QStyleOption opt;
    opt.initFrom(this);
    opt.rect = rect ();
    painter.drawPrimitive(QStyle::PE_Widget, opt);
    QWidget::paintEvent(event);
}

void BrowseWidget::initUI()
{
    QVBoxLayout *mainVLayout=new QVBoxLayout(this);
    QLabel * LBL = new new QLabel ( " slice browse " );
    mainVLayout->addWidget(lbl);
    setLayout (mainVLayout);
}

 

Guess you like

Origin www.cnblogs.com/ike_li/p/11493443.html