I get an error with setGeometry
in a very simple program in Qt5.
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QLabel* m_photo = new QLabel;
m_photo->setPixmap(QPixmap("test.jpg"));
m_photo->show();
return a.exec();
}
Error: setGeometry: Unable to set geometry 6x16+640+300 on
QWidgetWindow/'QLabelClassWindow'. Resulting geometry: 160x16+640+300
(frame: 9, 38, 9, 9, custom margin: 0, 0, 0, 0, minimum size: 0x0,
maximum size: 16777215x16777215).
I see Qt adding custom widget to a layout, but I did not understand the comment.
What am I doing wrong?
Probably you get this error because you don't use setGeometry()
, you should set geometry yourself. Try this:
m_photo->setGeometry(200,200,200,200);
Better way: label should have same size as picture. To do this you can use QPixmap
method width()
and height
QLabel* m_photo = new QLabel;
QPixmap px("G:/2/qt.jpg");
m_photo->setPixmap(px);
m_photo->setGeometry(200,200,px.width(),px.height());
m_photo->show();
Edit.
I understood why you get this error. It is very simple, your picture doesn't load! Why? Very simple too: probably your picture(test.jpg
) was putted near exe file, but Qt doesn't see this file(because Qt use another build directory)
Solution: put test.jpg
in correct directory or set pixmap full path(ad I do "G:/2/test.jpg"
for example). Also use this code: check is your picture load successfully.
QLabel* m_photo = new QLabel;
QPixmap px("G:/2/qt.jpg");
if(!px.isNull())
{
m_photo->setPixmap(px);
m_photo->show();
}
else
qDebug() << "Cannot find picture";
Is it work now?