Qt setGeometry:无法设置几何 [英] Qt setGeometry: Unable to set geometry

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

问题描述

为什么?

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

widget.cpp

widget.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,0,400,300);在Widget构造函数中消除了该问题。但是,窗口的位置不会很好地位于屏幕中央。

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.

推荐答案

设置窗口小部件的最小尺寸(这样它将自动定位),例如:

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

您还应该将窗口小部件的父项放在 QMainWindow ,但仍然可以使用。

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

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

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