2011-09-01 97 views
24

我在当前项目中有几个自定义小部件。我希望将样式表应用于它们,当我在Qt Creator中使用样式表时,它似乎可行。但是,在执行该程序时,不使用样式表。 Qt窗口小部件的样式表工作正常。用于自定义小部件的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. 
}; 

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); 
} 

任何帮助将不胜感激。

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

JEC

+0

你能展示相应的样式表吗? – alexisdm

+0

style sheet = background:rgb(170,0,255); \ nborder:2px纯黑色; – jecjackal

+5

搜索了几个小时后,我发现了这个http://developer.qt.nokia.com/forums/viewthread/7340该页面引用的代码是样式表工作所必需的。 – jecjackal

回答

45

我有一个类似的问题,它是使用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); 
} 

没有做你的自定义部件将仅支持后台,背景剪辑和背景源属性。

你可以在这里阅读:Qt Stylesheets reference在“Stylable Widgets列表” - > QWidget部分。

4

为了完整,PyQt中存在同样的问题。

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) 
8

有一个答案不是写自己的paintEvent容易得多:子类QFrame而不是QWidget,它会工作的时候了:

class WidgetUnits : public QFrame 
{ 
    Q_OBJECT 
.... 
可以通过添加类似的代码应用样式表到子类的QWidget
+1

我发现这是一个解决问题的少得多干扰的方式。 它很简单,只需为每个自定义小部件查找/替换一行,并通过在默认构造函数中将QWidget(父)替换为QFrame(父),将其转换为cpp文件计数器部分。 – Yattabyte

3

我和pyside有同样的问题。我只是为了完整而发布我的解决方案。 就像Pieter-Jan Busschaert提出的PyQt一样。 唯一的区别是,你需要调用,而不是初始化initFrom

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) 

一个你需要确保另一件事是,你在你的CSS文件中定义您的自定义插件的方式如下:

​​

和不像经常推荐的那样

QDockWidget#FreeDockWidget{...} 
0

调用setAttribute(Qt::WA_StyledBackground, true)为自定义小部件为我工作。