自定义小部件的Qt样式表
我在当前项目中有几个自定义小部件。我希望对它们应用样式表,当我在QtCreator中这样做时,它似乎可以工作。但是,在执行程序时,不使用样式表。Qt小部件的样式表工作正常 有人有什么建议吗?下面是一些相关代码 WidgetUnits.h自定义小部件的Qt样式表,qt,qtstylesheets,Qt,Qtstylesheets,我在当前项目中有几个自定义小部件。我希望对它们应用样式表,当我在QtCreator中这样做时,它似乎可以工作。但是,在执行程序时,不使用样式表。Qt小部件的样式表工作正常 有人有什么建议吗?下面是一些相关代码 WidgetUnits.h #ifndef WIDGETUNITS_H #define WIDGETUNITS_H #include <QList> #include <QWidget> #include <QPainter> #include &
#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.
};
\ifndef WIDGETUNITS\u H
#定义WIDGETUNITS\u H
#包括
#包括
#包括
#包括
#包括
#包括
名称空间用户界面
{
类单位;
}
WidgetUnits类:公共QWidget
{
Q_对象
公众:
显式WidgetUnits(QWidget*parent=0);
~WidgetUnits();
void setNumTimes(常量int和numTimes);
公众时段:
无效更新特征(常数时间*时间);
受保护的:
无效油漆事件(QPaintEvent*事件);
私人:
void checkNewQueue(常量QList*单位);
Ui::WidgetUnits*Ui;
const int picturewith;//单位图片的宽度。
const int picturehight;//单位图片的高度。
QList按钮图片;//单元的图片。外部QList存储给定刻度的图片的QList。
//内部QList存储特定记号的JECButtons。
};
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);
}
#包括“WidgetUnits.h”
#包括“ui_WidgetUnits.h”
WidgetUnits::WidgetUnits(QWidget*父项):
QWidget(母公司),
ui(新ui::WidgetUnits),
图片宽度(36),
图八(36)
{
用户界面->设置用户界面(此);
}
WidgetUnits::~WidgetUnits()
{
删除用户界面;
}
void WidgetUnits::updatePictures(常量时间*时间)
{
//仅显示本回合开始建造的单元。
checkNewQueue(时间->获取单位());
检查新建队列(时间->获取建筑物());
checkNewQueue(时间->getUpgrades());
//更新剩余图片的位置(删除部分图片后)。
//检查一次勾选的最大单位数。
int maxNewQueue=0;
对于(int a=0;alength()>maxNewQueue)
{
maxNewQueue=buttonPictures.at(a)->length();
}
}
如果(buttonPictures.length()>0)
{
此->设置几何图形(0,0,buttonPictures.length()*130,
maxNewQueue*(图片宽度+10)+20);
QList*tickButtons=0;
对于(int a=0;alength();++b)
{
点击按钮->在(b)->移动(a*130,b*(图片高度+10));
}
}
}
更新();
}
void WidgetUnits::checkNewQueue(常量QList*单位)
{
如果(单位!=0)
{
常数单位*currentUnit=0;
按钮*currentButton=0;
对于(int a=0;alength();++a)
{
append(新的QList());
对于(int b=0;bat(a)->length();++b)
{
当前单位=单位->在(a)->在(b);
//验证队列中是否有项目,以及队列操作是否已在本回合启动。
如果(currentUnit->getQueue()!=0&¤tUnit->getAction()->getTimeStart()==currentUnit->getAction()->getTimeCurrent())
&&(currentUnit->getAction()->getType()==Action::BUILD | | currentUnit->getAction()->getType()==Action::TRAIN||
currentUnit->getAction()->getType()==Action::UPGRADE))
{
buttonPictures.last()->append(新的JECButton(this));
currentButton=buttonPictures.last()->last();
QImage*image=新的QImage(currentUnit->getQueue()->getUnitBase()->getImage().scaled(图片宽度,图片高度));
当前按钮->设置图像(*图像);
currentButton->setGeometry(0,0,currentButton->getImage().width(),
currentButton->getImage().height();
currentButton->setColorHover(QColor(0,0,225));
currentButton->setColorPressed(QColor(120、120、120));
currentButton->setImageOwner(真);
currentButton->setVisible(真);
}
}
}
}
}
void WidgetUnits::setNumTimes(常量int和numTimes)
{
//为添加的标记追加新按钮列表。
对于(int a=buttonPictures.length();a
任何帮助都将不胜感激
这个小部件是可见的——我设置了一个它显示给我的工具提示(它的颜色与QScrollArea
它所在的位置相同)
Jec我遇到了一个类似的问题,通过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);
}
如果不这样做,您的自定义小部件将只支持背景、背景剪辑和背景源属性
您可以在这里阅读:在“可设置样式的小部件列表”->QWidget一节中。为了完整起见,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)
有一个答案比编写自己的
paintEvent
容易得多:子类QFrame
而不是QWidget
,它会立即工作:
class WidgetUnits : public QFrame
{
Q_OBJECT
....
我和派赛德也有同样的问题。我发布我的解决方案只是为了完整性。 这几乎就像在PyQt中的Pieter Jan一样
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)
FreeDockWidget{...}
QDockWidget#FreeDockWidget{...}