2012-01-23 57 views

回答

1

在显示它之前,您需要在顶级小部件上使用setGeometry。通过QDesktopWidget,我可以想出最简单的方法来确定需要的几何图形。试试下面的例子中(创建一个QPushButton,按下它在移动各地的各种屏幕上的小部件),你会明白我的意思:

MainWindow::MainWindow(QWidget *parent) : 
    QMainWindow(parent), 
    ui(new Ui::MainWindow) 
{ 
    ui->setupUi(this); 
    connect(ui->pushButton, SIGNAL(released()), this, SLOT(ButtonPressed())); 
} 

MainWindow::~MainWindow() 
{ 
    delete ui; 
} 

void MainWindow::ButtonPressed() 
{ 
    qDebug() << QApplication::desktop()->screenCount(); 
    qDebug() << QApplication::desktop()->screenNumber(); 
    qDebug() << QApplication::desktop()->screenGeometry(this); 
} 

应该从有相当简单拿出一个可行的通用版本出一个用户的中心屏幕(如果存在)。

12

如果使用QtQuick,这是可能做到这一点:

import QtQuick 2.2 
import QtQuick.Controls 1.1 
import QtQuick.Window 2.0 

ApplicationWindow { 
    visible: true 
    width: 320 
    height: 480 
    Component.onCompleted: { 
     setX(Screen.width/2 - width/2); 
     setY(Screen.height/2 - height/2); 
    } 
} 
10

销售的回答要好得多,特别是因为没有提到小工具......反正,这里是他的回答更简单的版本:

import QtQuick 2.2 
import QtQuick.Controls 1.1 
import QtQuick.Window 2.0 

ApplicationWindow { 
    visible: true 
    x: Screen.width/2 - width/2 
    y: Screen.height/2 - height/2 
    width: 320 
    height: 480 
}