Discussion:
[Interest] Image on QLabel doesn't show at all
Tamás Nagy
2018-12-08 17:23:22 UTC
Permalink
Hi,

pixmap = new QPixmap(imageUrl.toString());
ui->imageLabel->setGeometry(QRect(QPoint(0,0), QPoint(pixmap->width(),
pixmap->height()) ));
ui->imageLabel->setPixmap(*pixmap);
ui->imageLabel->setStyleSheet("background-image: url(" +
imageUrl.toString() + ")");
ui->imageLabel->update();
ui->imageLabel->show();

My image is C:\untitled.bmp

I also have a second error: my signal is not found. It always tells me:

18:18:07: Debugging starts
QMetaObject::connectSlotsByName: No matching signal for on_graphics_changed()
QObject::connect: No such signal
MainWindow::MainWindow::graphicsChanged() in
..\untitled\mainwindow.cpp:11
QObject::connect: (sender name: 'MainWindow')
QObject::connect: (receiver name: 'MainWindow')
onecoreuap\shell\ext\thumbnailcache\lib\thumbcacheobj.cpp(2076)\thumbcache.dll!6440C772:
(caller: 643F9E61) ReturnHr(1) tid(94) 80070057 The parameter is
incorrect.
minio\profapi\registry.cpp(48)\profapi.dll!76176875: (caller:
29374375) ReturnHr(1) tid(3aa8) 80070002 The system cannot find the
file specified.
minio\profapi\registry.cpp(48)\profapi.dll!76176875: (caller:
29374375) ReturnHr(2) tid(1e64) 80070002 The system cannot find the
file specified.
shell\comdlg32\fileopensave.cpp(14413)\comdlg32.dll!75B2FE77: (caller:
75B5D851) ReturnHr(1) tid(94) 80004005 Unspecified error
CallContext:[\PickerModalLoop]
18:18:43: Debugging has finished

#ifndef MAINWINDOW_H
#define MAINWINDOW_H


#include <QMainWindow>
#include <qfiledialog.h>
#include <QImage>
#include <QGraphicsView>
#include <QGraphicsScene>
#include <QGraphicsPixmapItem>
#include <QMessageBox>

namespace Ui {
class MainWindow;
}


class MainWindow : public QMainWindow
{
Q_OBJECT

public:
explicit MainWindow(QWidget *parent = nullptr);
~MainWindow();

public slots:
void on_pushButton_clicked();
void on_graphics_changed();

private:
Ui::MainWindow *ui;
QString imageDir = "";
QUrl imageUrl;
QPixmap * pixmap;

signals:
void graphicsChanged();
};

#endif // MAINWINDOW_H



Tamas
Christian Ehrlicher
2018-12-08 19:08:46 UTC
Permalink
Post by Tamás Nagy
Hi,
pixmap = new QPixmap(imageUrl.toString());
ui->imageLabel->setGeometry(QRect(QPoint(0,0), QPoint(pixmap->width(),
pixmap->height()) ));
ui->imageLabel->setPixmap(*pixmap);
ui->imageLabel->setStyleSheet("background-image: url(" +
imageUrl.toString() + ")");
ui->imageLabel->update();
ui->imageLabel->show();
My image is C:\untitled.bmp
QPixmap can not load an image from an url - only from a local file or a
resource.

Christian
Henry Skoglund
2018-12-08 19:13:15 UTC
Permalink
Post by Tamás Nagy
Hi,
pixmap = new QPixmap(imageUrl.toString());
ui->imageLabel->setGeometry(QRect(QPoint(0,0), QPoint(pixmap->width(),
pixmap->height()) ));
ui->imageLabel->setPixmap(*pixmap);
ui->imageLabel->setStyleSheet("background-image: url(" +
imageUrl.toString() + ")");
ui->imageLabel->update();
ui->imageLabel->show();
My image is C:\untitled.bmp
18:18:07: Debugging starts
QMetaObject::connectSlotsByName: No matching signal for
on_graphics_changed()
Post by Tamás Nagy
QObject::connect: No such signal
MainWindow::MainWindow::graphicsChanged() in
..\untitled\mainwindow.cpp:11
QObject::connect: (sender name: 'MainWindow')
QObject::connect: (receiver name: 'MainWindow')
..
..

Hi, the 1st error is because you're doing a simple
imageUrl.toString() which returns % chars instead of backslashes, this
confuses QPixmap's constructor. To give QPixmap Windows-style filenames,
try:
pixmap = new QPixmap(imageUrl.toString(QUrl::PreferLocalFile));

And for making C:\untitled.bmp show up, try lose/commenting away that
->setStyleSheet() call, setPixmap(*pixmap); should suffice.


The 2nd error is actually a warning and then an error, the warning is
from setupUI() which sees your slot name on_graphics_changed(). Slot
names beginning with "on_" are kind of reserved, see more for example
here:
https://linux.m2osw.com/qtwarning-qmetaobjectconnectslotsbyname-no-matching-signal-onsomethingevent

And that 2nd error is most likely because on your line 11 in your
mainwindow.cpp (which I **cannot see** so I'm guessing :-) you wrote:
connect(this,SIGNAL(MainWindow::graphicsChanged()),this,SLOT(on_graphics_changed()));

Try change to
connect(this,SIGNAL(graphicsChanged()),this,SLOT(on_graphics_changed()));

Rgrds Henry
Thiago Macieira
2018-12-09 04:24:55 UTC
Permalink
Post by Tamás Nagy
pixmap = new QPixmap(imageUrl.toString());
This line is already wrong. QPixmap opens files paths, not URLs.
Post by Tamás Nagy
ui->imageLabel->setPixmap(*pixmap);
Why did you create QPixmap in the heap if you only used it here?
Post by Tamás Nagy
ui->imageLabel->setStyleSheet("background-image: url(" +
imageUrl.toString() + ")");
This is a conflicting instruction with the setPixmap above. One of the two
will not take effect. If the URL is correctly pointing to something that the
stylesheet could show, then QPixmap in the first line can't open it. If the
QUrl contains an invalid URL that happens to be turn to a valid file path that
QPixmap can open if you do url.toString(), then this stylesheet possibly
points to an invalid URL.
--
Thiago Macieira - thiago.macieira (AT) intel.com
Software Architect - Intel Open Source Technology Center
Loading...