C++ 如何使用QStyle.drawControl()呈现红色按钮?
使用以下代码,我尝试使用C++ 如何使用QStyle.drawControl()呈现红色按钮?,c++,qt,qt4,qstyle,C++,Qt,Qt4,Qstyle,使用以下代码,我尝试使用QStyle.drawControl()呈现红色按钮: #包括 #包括 类Widget:publicqwidget { 虚拟void paintEvent(QPaintEvent*事件) { QSTYLEOPTION按钮opt; opt.palete=Qpalete(Qt::红色); opt.state=QStyle::state|u Active | QStyle::state|u Enabled; opt.rect=QRect(50,25,100,50); 油漆工(本
QStyle.drawControl()
呈现红色按钮:
#包括
#包括
类Widget:publicqwidget
{
虚拟void paintEvent(QPaintEvent*事件)
{
QSTYLEOPTION按钮opt;
opt.palete=Qpalete(Qt::红色);
opt.state=QStyle::state|u Active | QStyle::state|u Enabled;
opt.rect=QRect(50,25,100,50);
油漆工(本);
style()->drawControl(QStyle::CE_按钮、选项和画师);
}
};
int main(int argc,字符**argv)
{
QApplication应用程序(argc、argv);
小部件w;
w、 调整大小(200100);
w、 show();
返回app.exec();
}
然而,我得到了以下结果:
如何使用QStyle.drawControl()
渲染红色按钮
我正在Windows XP上使用Qt 4.8.1和Visal Studio 2010。您试图做的事情似乎过于复杂。如果您只想要一个红色按钮,为什么不使用QPushButton的方法呢?它需要一个QString,您可以定义类似于CSS的按钮。在这里,我为您创建了一个红色按钮,类似于XP ui按钮:
QPushButton
{
background: qlineargradient(x1:0,y1:0,x2:0,y2:1, stop:0 #f4a3a3,stop: 1 #cc1212);
border-width: 1px;
border-color: #d91414;
border-style: solid;
padding: 5px;
padding-left:10px;
padding-right:10px;
border-radius: 3px;
color:#000;
}
QPushButton:hover
{
border-color: #e36666;
}
QPushButton:pressed
{
background:qlineargradient(x1:0,y1:0,x2:0,y2:1,stop: 0 #de8383, stop: 1 #ad0C0C);
border-color: #d91414;
}
现在您只需要将上面的代码作为字符串传递给buttons setStyleSheet()方法。如果您想要创建一个按钮小部件,默认情况下是红色的,那么扩展QPushButton类,创建一个包含上述内容的静态QString字段,并将按钮设置为构造函数中的样式表
样式表上更易于理解的示例:
按钮由本机样式引擎绘制,因此可能根本不使用调色板(请参见常见问题解答) 您可以使用带有样式表的实际按钮,该样式表作为最后一个参数传递给自己按钮的style
drawControl
函数
class Widget : public QWidget
{
// To allow the automatic deletion without parenting it
QScopedPointer<QPushButton> button;
public:
Widget() : button(new QPushButton) {
button->setStyleSheet("background-color: red");
}
virtual void paintEvent(QPaintEvent* event)
{
QStyleOptionButton opt;
opt.state = QStyle::State_Active | QStyle::State_Enabled;
opt.rect = QRect(50, 25, 100, 50);
QPainter painter(this);
button->style()->drawControl(QStyle::CE_PushButton, &opt, &painter,
button.data());
}
};
你在这个按钮上设置了一些文字吗?也许“Qt::red”属于文本颜色、背景/框架colors@KamilKlimek:“Qpalete::Qpalete(Qt::GlobalColor按钮)”状态的文档从按钮颜色构造调色板。其他颜色将基于此颜色自动计算。窗口也将是按钮颜色';请参阅。@Kamil Klimek:我还测试了其他几种构建调色板的方法,但似乎无论调色板是什么,按钮最终都会有相同的颜色。如果显式设置应用程序样式,它会起作用吗?也就是说,如果你调用app.setStyle(“windows”);你得到了不同的结果吗?@RA:是的,那么它就起作用了。正如Alexism答案中的链接所解释的,这个问题是针对Windows XP、Windows Vista和Mac风格的。我将Alexism的答案与您的样式表结合起来解决了这个问题。
class Widget : public QWidget
{
// To allow the automatic deletion without parenting it
QScopedPointer<QPushButton> button;
public:
Widget() : button(new QPushButton) {
button->setStyleSheet("background-color: red");
}
virtual void paintEvent(QPaintEvent* event)
{
QStyleOptionButton opt;
opt.state = QStyle::State_Active | QStyle::State_Enabled;
opt.rect = QRect(50, 25, 100, 50);
QPainter painter(this);
button->style()->drawControl(QStyle::CE_PushButton, &opt, &painter,
button.data());
}
};
class Widget : public QWidget {
QScopedPointer<QPushButton> button;
public:
Widget() : button(new QPushButton) {
QGraphicsColorizeEffect *effect = new QGraphicsColorizeEffect(button.data());
effect->setColor(Qt::red);
button->setGraphicsEffect(effect);
}
virtual void paintEvent(QPaintEvent* event) {
button->setFixedSize(100, 50);
button->render(this, QPoint(50, 25));
}
};