为什么设置 QLabel 的像素图不起作用?

发布于 2024-10-10 08:11:10 字数 727 浏览 0 评论 0原文

我创建了一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不美如何 2024-10-17 08:11:10

您有:

<qresource prefix="/images">
<file>images/target.png</file>
</qresource>

我认为这将导致资源路径中出现双images,即:/images/images/target.png。要解决此问题,请删除 prefix="/images" 或将 alias="target.png" 放入 file 标记中。

为了更清楚地显示错误所在,您可以编写代码,以便它使用 QPixmap::load ,因为可以检查错误:

QPixmap pixmap;
if (!pixmap.load( ":/images/target.png" )) {
    qWarning("Failed to load images/target.png");
}
this->setPixmap( pixmap );

或者您可以更进一步并使用 QImageReader 它可以给出详细的错误消息。

You have:

<qresource prefix="/images">
<file>images/target.png</file>
</qresource>

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 the prefix="/images" or put alias="target.png" in the file 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:

QPixmap pixmap;
if (!pixmap.load( ":/images/target.png" )) {
    qWarning("Failed to load images/target.png");
}
this->setPixmap( pixmap );

Or you could go even further and use QImageReader which can give detailed error messages.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文