gpt4 book ai didi

c++ - QDial如何添加资源图片?

转载 作者:行者123 更新时间:2023-11-28 04:54:03 25 4
gpt4 key购买 nike

QDial如何添加资源图片?

我已经为 QDial 完成了一个自定义类,但是我怎样才能在其中包含一个样式表,以便像我为按钮所做的那样添加资源图像?例如:

button1->setStyleSheet("border-image:url(:/resources/img/knob.png)");

最佳答案

QDial does not support stylesheets , 除了背景颜色。但是,这就是我的做法。

但有一个警告:这根本不完整,它只是让您了解如何去做。

在您的页眉中,为 QPixmap 设置一个属性,这将是您的背景图片:

class QCustomDial : public QDial
{
Q_OBJECT

Q_PROPERTY(QPixmap backgroundImage READ backgroundImage WRITE setBackgroundImage DESIGNABLE true)

QPixmap* m_background;

public:
QPixmap backgroundImage() { return *m_background; }

void setBackgroundImage(QPixmap pixmap)
{
*m_background = pixmap;
update();
}

private:
QPixmap* m_background;
};

然后,在您的 paintEvent 中,您必须绘制像素图:

void QCustomDial::paintEvent(QPaintEvent*)
{
QPainter painter(this);
...
QPoint start(0, 0); //whatever you want
painter.drawPixmap(start, *m_background);
...
}

最后,您在问题中想要的部分:样式表。现在您已经定义了一个 Q_PROPERTY,您可以从样式表中获取它:

QCustomDial {
qproperty-backgroundImage: url(:/resources/img/knob.png);
}

希望对您有所帮助。我还建议您阅读这篇关于自定义 QDial(part1part2)的博客。

关于c++ - QDial如何添加资源图片?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47561259/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com