无法在 Qt 中设置几何图形 [英] Unable to set geometry in Qt

查看:100
本文介绍了无法在 Qt 中设置几何图形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Qt5 的一个非常简单的程序中遇到 setGeometry 错误.

int main(int argc, char *argv[]){QApplication a(argc, argv);QLabel* m_photo = 新的 QLabel;m_photo->setPixmap(QPixmap("test.jpg"));m_photo->show();返回 a.exec();}

<块引用>

错误:setGeometry:无法在上设置几何图形 6x16+640+300QWidgetWindow/'QLabelClassWindow'.结果几何:160x16+640+300(frame: 9, 38, 9, 9, 自定义边距: 0, 0, 0, 0, 最小尺寸: 0x0,最大尺寸:16777215x16777215).

我看到 Qt 向布局添加自定义小部件,但是我没看懂评论.
我做错了什么?

解决方案

可能是因为你没有使用 setGeometry() 而得到这个错误,你应该自己设置几何.试试这个:

m_photo->setGeometry(200,200,200,200);

更好的方法:标签应该与图片大小相同.为此,您可以使用 QPixmap 方法 width()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();

编辑.

我了解您收到此错误的原因.很简单,你的图片加载不出来!为什么?也很简单:可能你的图片(test.jpg)放在exe文件附近,但Qt没有看到这个文件(因为Qt使用另一个构建目录)

解决方案:将test.jpg放在正确的目录中或设置像素图的完整路径(例如我做的"G:/2/test.jpg").也使用此代码:检查您的图片加载是否成功.

 QLabel* m_photo = new QLabel;QPixmap px("G:/2/qt.jpg");if(!px.isNull()){m_photo->setPixmap(px);m_photo->show();}别的qDebug() <<"找不到图片";

现在可以了吗?

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?

这篇关于无法在 Qt 中设置几何图形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆