C++ Qt setGeometry:无法设置几何

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31230975/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 13:54:24  来源:igfitidea点击:

Qt setGeometry: Unable to set geometry

c++qt

提问by KcFnMi

Why?

为什么?

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).

The project is:

该项目是:

project.pro

项目.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

小部件.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

widget.cpp

小部件.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();
}

Adding setGeometry(0, 0, 400, 300); in Widget constructor removes the issue. But the window will not be positioned beautifully at the center of the screen.

添加 setGeometry(0, 0, 400, 300); 在 Widget 构造函数中消除了这个问题。但是窗口不会漂亮地定位在屏幕的中心。

回答by Miki

This warning happens (at least to me) when the size of the widget results to be very small.

当小部件的大小非常小时,会发生此警告(至少对我而言)。

Set a minimum size for your widget (so it will be automatically positioned), like:

为您的小部件设置最小尺寸(这样它会自动定位),例如:

 // Widget constructor

 QVBoxLayout* vLayout = new QVBoxLayout();
 setLayout(vLayout);

 setMinimumSize(200,200);

You should also parent your widget to a QMainWindow, but will still work.

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