2015-11-05 312 views
0

我在MAC上的Qt Widget中開發了iPhone應用程序,但屏幕和所有元素的大小都存在問題,我設置了iPhone 4的所有元素和屏幕。當這個應用程序在iPhone 5上運行時,所有東西看起來都很小。所以我想要設置屏幕和所有元素的大小,以便在所有類型的手機和屏幕中看起來更好。在Qt Widget中設置大小文本框和按鈕應用程序

在小部件應用程序中,我無法直接在.qml文件中添加,只能通過拖放操作進行更改。

感謝advanace。

回答

0

您可以通過從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() 
{ 
} 
+0

感謝@talamaki,我是初學者,我理解你的概念,現在只是告訴我,我該如何使用H(r.height )和w(r.width)與文本框/按鈕?只給出由r.height和r.width設置的文本框/按鈕大小的示例代碼。之後,我將在我的應用程序中設置所有元素的大小。再次感謝。 而如果我沒有錯,我必須在mainwindow.cpp文件中添加此代碼... –

+0

您可以使用Qt佈局來實現。我更新了Qt佈局系統和一些示例代碼的鏈接。 – talamaki

相關問題