为什么设置 QLabel 的像素图不起作用?
我创建了一个 QLabel
的子类,打算在 QGraphicsView
中使用它。它充当一个可移动的“点”,可以单击并在图形视图周围拖动。
创建自定义类并将其显示在图形视图中并不是一个问题;但是,尝试让自定义 QLabel 用我想要的图像绘制自身并没有发生。我的自定义 QLabel 类的构造函数如下所示:
TrackerPoint::TrackerPoint(QWidget *parent) :
QLabel(parent)
{
this->setFixedSize( 40, 40 );
QPixmap pixmap( ":/images/target.png" );
this->setPixmap( pixmap );
this->setMask( pixmap.mask() );
}
我确保图像目录存在于运行应用程序的工作目录中。如果它完全相关的话,我的 QRC 文件就像这样:
<RCC>
<qresource prefix="/images">
<file>images/target.png</file>
</qresource>
</RCC>
我已经尝试处理这个问题好几天了——任何关于为什么图像没有出现的想法都会很可爱。 (这是否与我在 QLabel 的构造函数中设置像素图有关?)
I've created a subclass of QLabel
that I intend to use in a QGraphicsView
. It serves as a movable "point" that one can click on and drag around the graphics view.
Creating the custom class and having it displayed in the graphics view hasn't been an issue; however, trying to get the custom QLabel
to paint itself with the image I want isn't happening. The constructor for my custom QLabel
class is like so:
TrackerPoint::TrackerPoint(QWidget *parent) :
QLabel(parent)
{
this->setFixedSize( 40, 40 );
QPixmap pixmap( ":/images/target.png" );
this->setPixmap( pixmap );
this->setMask( pixmap.mask() );
}
I've ensured that the images directory exists in the working directory that the application is run from. If it is relevant at all, my QRC file is like so:
<RCC>
<qresource prefix="/images">
<file>images/target.png</file>
</qresource>
</RCC>
I've been trying to deal with this problem for days -- any ideas as to why the image isn't appearing would be lovely. (Does it have to do with the fact that I'm setting a pixmap in the constructor of the QLabel
?)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您有:
我认为这将导致资源路径中出现双
images
,即:/images/images/target.png
。要解决此问题,请删除prefix="/images"
或将alias="target.png"
放入file
标记中。为了更清楚地显示错误所在,您可以编写代码,以便它使用 QPixmap::load ,因为可以检查错误:
或者您可以更进一步并使用 QImageReader 它可以给出详细的错误消息。
You have:
I think that this will result in a double
images
in the resource path, i.e.:/images/images/target.png
. To fix that, either remove theprefix="/images"
or putalias="target.png"
in thefile
tag.To make it clearer where the error is, you could write your code so that it uses
QPixmap::load
, since that can be checked for errors:Or you could go even further and use
QImageReader
which can give detailed error messages.