English 中文(简体)
如何把Qt主形放在屏幕上?
原标题:how to center a Qt mainform on the screen?
  • 时间:2010-08-06 14:51:47
  •  标签:
  • qt
  • qt4
  • qt4.6

我在主要建筑师中尝试了这些工程:

QRect desktopRect = QApplication::desktop()->availableGeometry(this);
move(desktopRect.center() - frameGeometry().center());

QRect desktopRect = QApplication::desktop()->availableGeometry(this);
move(desktopRect.center() - rect().center());

但两者都把表格的底层右角放在屏幕的中心,而不是放在形式上。 任何想法?

最佳回答

我在我的主要建筑构造中尝试了这些工程。

这可能是问题。 此时此刻,你可能没有有效的地貌信息,因为这个物体没有明显可见。

当物体首次构造时,其基本位置是(0,0),其预期位置为(width,hels)

frame geometry at construction:  QRect(0,0 639x479) 

但经证明:

frame geometry rect:  QRect(476,337 968x507) 

因此,您现在可以依靠您的<代码>frameGeometry()信息。

www.un.org/Depts/DGACM/index_spanish.htm 顺便说一句,我假定,你可以轻易地按要求搬走,但为了完整起见,Im在Patrice s代码上投放,而后者则取决于地貌信息:

QRect desktopRect = QApplication::desktop()->availableGeometry(this);
QPoint center = desktopRect.center();

move(center.x() - width() * 0.5, center.y() - height() * 0.5);
问题回答

The move function (see QWidget doc) takes one QPoint or two int as parameter. This corresponds to the coordinates of the top-left corner of your Widget (relative to its parent; Here OS Desktop). Try:

QRect desktopRect = QApplication::desktop()->availableGeometry(this);
QPoint center = desktopRect.center();

move(center.x()-width*0.5, center.y()-height*0.5);

move(pos() + (QGuiApplication::primaryScreen()->geometry().center() - geometry().center()));
#include <QStyle>
#include <QDesktopWidget>

window->setGeometry(
    QStyle::alignedRect(
        Qt::LeftToRight,
        Qt::AlignCenter,
        window->size(),
        qApp->desktop()->availableGeometry()
    )
);

https://wiki.qt.io/How_to_Center_a_Window_on_the_Screen

流动(QGuiApplication:firstScreen()->geometry().center() - rect().center();

PyQTshed

# Center Window
desktopRect = QApplication.desktop().availableGeometry(self.window)
center = desktopRect.center();
self.window.move(center.x()-self.window.width()  * 0.5,
                 center.y()-self.window.height() * 0.5);   

假设有关窗口,另一个解决办法是800×800:

QRect rec = QApplication::desktop()->availableGeometry();
move(QPoint((rec.width()-800)/2, (rec.height()-800)/2));




相关问题
Qt: Do events get processed in order?

If I had a class A, where one of its functions does: void A::func() { emit first_signal(); emit second_signal(); } Assuming that a class B has 2 slots, one connected to first_signal, and the ...

How to determine how much free space on a drive in Qt?

I m using Qt and want a platform-independent way of getting the available free disk space. I know in Linux I can use statfs and in Windows I can use GetDiskFreeSpaceEx(). I know boost has a way, ...

Drag & drop with .ui files

I m having big troubles with drag & drop. I ve created a new Qt Designer Form Class in which I have one QListWidget and one QWidget. Now I want to enable dragging & dropping between these two ...

Link errors on Snow Leopard

I creating a small desktop application using Qt and Poco on Mac OS X Snow Leopard. Qt works fine, but once I started linking with Poco I get the following warning: ld: warning: in /Developer/SDKs/...

Showing two windows in Qt4

My friend and I have each created parts of a GUI using Qt 4. They both work independently and I am trying to integrate his form with the my main window. As of now this is the code I am using to try ...

Qt equivalent of .NET data binding?

Is there an equivalent of .NET s data binding in Qt? I want to populate some combo boxes and other widgets with QStrings that refer to specific entities in my database. However, it would be cleaner ...

热门标签