Qt qss问题总结

1.在QWidget中设定了setObjectName,就是不起作用。

   解决方法重写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)
{
    initUI();
    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 QLabel("切片浏览");
    mainVLayout->addWidget(lbl);
    setLayout(mainVLayout);
}

猜你喜欢

转载自www.cnblogs.com/ike_li/p/11493443.html