您可以通过从QDesktopWidget查询来动态设置屏幕大小。
QRect r = QApplication::desktop()->screenGeometry();
int h = r.height();
int w = r.width();
The Qt layout system提供的自动排列窗口小部件内子部件,以确保它们很好地利用了可用空间的一种简单而有效的方式。
请参阅下面的示例代码,其中显示全屏并为该文本标签和按钮分隔该空间。您可以调整窗口大小,直到小部件的最小大小限制为止。
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include <QVBoxLayout>
#include <QPushButton>
#include <QLabel>
#include <QApplication>
#include <QDesktopWidget>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent)
{
QWidget *central_widget = new QWidget;
QVBoxLayout *layout = new QVBoxLayout(central_widget);
QPushButton *button1 = new QPushButton("Button1");
layout->addWidget(button1);
QLabel *label1 = new QLabel();
label1->setText("Label1");
label1->setAlignment(Qt::AlignCenter);
layout->addWidget(label1);
setCentralWidget(central_widget);
QRect r = QApplication::desktop()->screenGeometry();
int h = r.height();
int w = r.width();
button1->setMinimumHeight(h/4);
button1->setMaximumHeight(h/2);
button1->setMinimumWidth(w/2);
button1->setMaximumWidth(w);
label1->setMinimumHeight(h/4);
label1->setMaximumHeight(h/2);
label1->setMinimumWidth(w/2);
label1->setMaximumWidth(w);
resize(w, h);
}
MainWindow::~MainWindow()
{
}
感谢@talamaki,我是初学者,我理解你的概念,现在只是告诉我,我该如何使用H(r.height )和w(r.width)与文本框/按钮?只给出由r.height和r.width设置的文本框/按钮大小的示例代码。之后,我将在我的应用程序中设置所有元素的大小。再次感谢。 而如果我没有错,我必须在mainwindow.cpp文件中添加此代码... –
您可以使用Qt布局来实现。我更新了Qt布局系统和一些示例代码的链接。 – talamaki