自定义小部件的Qt样式表

jec*_*kal 32 qt qtstylesheets

我当前的项目中有几个自定义小部件.我希望将样式表应用到它们中,当我在Qt Creator中这样做时,它似乎有效.但是,在执行程序时,不使用样式表.Qt小部件的样式表正常工作.

有人有建议吗?这是一些相关的代码.

WidgetUnits.h

#ifndef WIDGETUNITS_H
#define WIDGETUNITS_H

#include <QList>

#include <QWidget>
#include <QPainter>

#include <Widgets/JECButton.h>

#include <Unit.h>
#include <Time.h>

namespace Ui
{
    class WidgetUnits;
}

class WidgetUnits : public QWidget
{
    Q_OBJECT

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

    void setNumTimes(const int& numTimes);

public slots:
    void updatePictures(const Time* time);

protected:
    void paintEvent(QPaintEvent *event);
private:
    void checkNewQueue(const QList<QList<Unit*>*>* units);
    Ui::WidgetUnits *ui;

    const int pictureWidth;                         // The width of the Unit pictures.
    const int pictureHeight;                        // The height of the Unit pictures.

    QList<QList<JECButton*>*> buttonPictures;       // The Units' pictures. The outer QList stores the QList of pictures for a given tick.
                                                    // The inner QList stores the JECButtons for the specific tick.
};
Run Code Online (Sandbox Code Playgroud)

WidgetUnits.cpp

#include "WidgetUnits.h"
#include "ui_WidgetUnits.h"

WidgetUnits::WidgetUnits(QWidget *parent):
    QWidget(parent),
    ui(new Ui::WidgetUnits),
    pictureWidth(36),
    pictureHeight(36)
{
    ui->setupUi(this);
}

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

void WidgetUnits::updatePictures(const Time *time)
{
    // Only showing units that started to get built this turn.
    checkNewQueue(time->getUnits());
    checkNewQueue(time->getBuildings());
    checkNewQueue(time->getUpgrades());

    // Updating the position of the remaining pictures (after some were removed).
    // Checking the maximum number of Units made in one tick.
    int maxNewQueue = 0;
    for (int a = 0; a < buttonPictures.length(); ++a)
    {
        if (buttonPictures.at(a)->length() > maxNewQueue)
        {
            maxNewQueue = buttonPictures.at(a)->length();
        }
    }

    if (buttonPictures.length() > 0)
    {
        this->setGeometry(0, 0, buttonPictures.length() * 130,
                          maxNewQueue * (pictureWidth + 10) + 20);

        QList<JECButton*>* tickButtons = 0;
        for (int a = 0; a < buttonPictures.length(); ++a)
        {
            tickButtons = buttonPictures.at(a);
            for (int b = 0; b < tickButtons->length(); ++b)
            {
                tickButtons->at(b)->move(a * 130, b * (pictureHeight + 10));
            }
        }
    }
    update();
}

void WidgetUnits::checkNewQueue(const QList<QList<Unit *> *> *units)
{
    if (units != 0)
    {
        const Unit* currentUnit = 0;
        JECButton* currentButton = 0;
        for (int a = 0; a < units->length(); ++a)
        {
            buttonPictures.append(new QList<JECButton*>());

            for (int b = 0; b < units->at(a)->length(); ++b)
            {
                currentUnit = units->at(a)->at(b);

                // Verifying that there is an item in the queue and the queue action was started this turn.
                if (currentUnit->getQueue() != 0 && currentUnit->getAction()->getTimeStart() == currentUnit->getAction()->getTimeCurrent()
                        && (currentUnit->getAction()->getType() == Action::BUILD || currentUnit->getAction()->getType() == Action::TRAIN ||
                            currentUnit->getAction()->getType() == Action::UPGRADE))
                {
                    buttonPictures.last()->append(new JECButton(this));
                    currentButton = buttonPictures.last()->last();

                    QImage* image = new QImage(currentUnit->getQueue()->getUnitBase()->getImage().scaled(pictureWidth, pictureHeight));
                    currentButton->setImage(*image);
                    currentButton->setGeometry(0, 0, currentButton->getImage().width(),
                                                       currentButton->getImage().height());
                    currentButton->setColorHover(QColor(0, 0, 225));
                    currentButton->setColorPressed(QColor(120, 120, 120));
                    currentButton->setImageOwner(true);
                    currentButton->setVisible(true);
                }
            }
        }
    }
}

void WidgetUnits::setNumTimes(const int &numTimes)
{
    // Appending new button lists for added ticks.
    for (int a = buttonPictures.length(); a < numTimes; ++a)
    {
        buttonPictures.append(new QList<JECButton*>());
    }
}

void WidgetUnits::paintEvent(QPaintEvent *event)
{
    QWidget::paintEvent(event);
}
Run Code Online (Sandbox Code Playgroud)

任何帮助将不胜感激.

小部件是可见的 - 我设置了一个工具提示,它显示了我(它的颜色与QScrollArea它的颜色相同).

JEC

Rom*_*lov 58

我遇到了类似的问题,并使用jecjackal的评论解决了这个问题.正如sjwarner所说,以答案的形式会更加引人注目.所以我会提供它.为了任何未来的观众的利益.再说一遍,这不是我的答案!欣赏它的jecjackal!

正如在Qt的样式表参考中所说,将CSS样式应用于从QWidget继承的自定义窗口小部件需要以这种方式重新实现paintEvent():

 void CustomWidget::paintEvent(QPaintEvent *)
 {
     QStyleOption opt;
     opt.init(this);
     QPainter p(this);
     style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
 }
Run Code Online (Sandbox Code Playgroud)

如果不这样做,您的自定义小部件将仅支持背景,背景剪辑和背景原始属性.

你可以在这里阅读:Qt Stylesheets在"Stylable Widgets列表" - > QWidget一节中引用.


Mar*_*rco 13

有一个答案比编写自己的更容易paintEvent:子类QFrame代替QWidget它,它会立即起作用:

class WidgetUnits : public QFrame
{
    Q_OBJECT
....
Run Code Online (Sandbox Code Playgroud)


men*_*oom 7

调用setAttribute(Qt::WA_StyledBackground, true)自定义小部件对我有用。


Ste*_*rdt 6

我在 pyside 上遇到了同样的问题。我发布我的解决方案只是为了完整性。这几乎就像 Pieter-Jan Busschaert 提出的在 PyQt 中一样。唯一的区别是您需要调用 initFrom 而不是 init

def paintEvent(self, evt):
    super(FreeDockWidget,self).paintEvent(evt)
    opt = QtGui.QStyleOption()
    opt.initFrom(self)
    p = QtGui.QPainter(self)
    s = self.style()
    s.drawPrimitive(QtGui.QStyle.PE_Widget, opt, p, self) 
Run Code Online (Sandbox Code Playgroud)

您需要确保的另一件事是您在 css 文件中以下列方式定义自定义小部件:

FreeDockWidget{...}
Run Code Online (Sandbox Code Playgroud)

并且不喜欢经常推荐

QDockWidget#FreeDockWidget{...}
Run Code Online (Sandbox Code Playgroud)


Pie*_*ert 5

为了完整起见,PyQt中存在同样的问题.您可以通过添加类似的代码将样式表应用于子类QWidget:

def paintEvent(self, pe):
  opt = QtGui.QStyleOption()
  opt.init(self)
  p = QtGui.QPainter(self)
  s = self.style()
  s.drawPrimitive(QtGui.QStyle.PE_Widget, opt, p, self)
Run Code Online (Sandbox Code Playgroud)