Qt setGeometry:无法设置几何体

Qt setGeometry: Unable to set geometry

本文关键字:设置 几何体 setGeometry Qt      更新时间:2023-10-16

为什么?

setGeometry: Unable to set geometry 22x22+320+145 on QWidgetWindow/'WidgetClassWindow'. Resulting geometry:  116x22+320+145 (frame: 8, 30, 8, 8, custom margin: 0, 0, 0, 0, minimum size: 22x22, maximum size: 16777215x16777215).

该项目是:

项目.pro

QT       += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = untitled5
TEMPLATE = app

SOURCES += main.cpp
        widget.cpp
HEADERS  += widget.h

widget.h

#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
class Widget : public QWidget
{
    Q_OBJECT
public:
    explicit Widget(QWidget *parent = 0);
    ~Widget();
private:
};
#endif // WIDGET_H

小工具.cpp

#include "widget.h"
#include <QVBoxLayout>
Widget::Widget(QWidget *parent) :
    QWidget(parent)
{
    QVBoxLayout *vLayout = new QVBoxLayout(this);
}
Widget::~Widget()
{
}

main.cpp

#include "widget.h"
#include <QApplication>
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Widget w;
    w.show();
    return a.exec();
}

添加setGeometry(0,0400300(;在Widget构造函数中删除了该问题。但是窗口不会很好地定位在屏幕的中心。

当小部件的大小非常小时,就会发生此警告(至少对我来说是这样(。

为你的小部件设置一个最小尺寸(这样它就会自动定位(,比如:

 // Widget constructor
 QVBoxLayout* vLayout = new QVBoxLayout();
 setLayout(vLayout);
 setMinimumSize(200,200);

您还应该将小部件作为QMainWindow的父级,但仍然可以工作。

尝试使用adjustSize()

看起来有问题:为什么我得到QWindowsWindow::setGeometry:无法用Qt 5.12.0 设置几何警告

可能的解决方案:Qt无法设置几何