您可以通過從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