简体   繁体   English

如何在主窗口的中心位置打开 QDialog 小部件

[英]how to open a QDialog widget on the center position of the main window

Im looking for a way to open a QDialog widget on the center position of the main window.我正在寻找一种在主窗口的中心位置打开QDialog widget的方法。 I have set the position of the mainwindow to center.我已将主窗口的位置设置为居中。

 centerPoint = qtw.QDesktopWidget().availableGeometry().center()
        qtRectangle.moveCenter(centerPoint)

to folow the dialog widget to the postion of the main window i have set it to跟随对话框小部件到主窗口的位置,我已将其设置为

msgb.move(self.pos().x(), self.pos().y())

the dialog window follows the positon of the main window , but it opens on the top left side of the main window, how can I change its position to the center of the main window ?对话窗口跟随主窗口的位置,但它在主窗口的左上角打开,如何将其位置更改为主窗口的中心?

#!/usr/bin/env python

"""
startscreen

base window remit to specific tests

"""

import os
import sys
from PyQt5 import QtWidgets as qtw
from PyQt5 import QtCore as qtc




class Startscreen(qtw.QWidget):
    '''
    remit to one of three tests if widgets toggled/clicked
    hide its self after
    '''

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        # your code will go here

        # interface

        # position
        qtRectangle = self.frameGeometry()
        centerPoint = qtw.QDesktopWidget().availableGeometry().center()
        qtRectangle.moveCenter(centerPoint)
        self.move(qtRectangle.topLeft())
        # size
        self.resize(700, 410)
        # frame title
        self.setWindowTitle("Lambda")

        # heading
        heading_label = qtw.QLabel("Lambda Version 1.0")
        heading_label.setAlignment(qtc.Qt.AlignHCenter)

        # active user
        activeuser_label = qtw.QLabel('Benutzer: ' + os.getlogin())
        activeuser_label.setStyleSheet("background-color: rgb(234, 246, 22)")
        activeuser_label.setAlignment(qtc.Qt.AlignRight | qtc.Qt.AlignTop)

        # groubox for widget positioning
        self.groupbox = qtw.QGroupBox(self)
        # groupbox.setAlignment(qtc.Qt.AlignHCenter)

        # layout and widgets
        vlayout = qtw.QVBoxLayout()
        vlayout.setAlignment(qtc.Qt.AlignHCenter)

        self.particlesize_radiobutton = qtw.QRadioButton("test1")
        vlayout.addWidget(self.particlesize_radiobutton)
        self.dimensionalchange_radiobutton = qtw.QRadioButton("test2")
        vlayout.addWidget(self.dimensionalchange_radiobutton)
        self.dimensionalchangecook_radiobutton = qtw.QRadioButton("test3")
        vlayout.addWidget(self.dimensionalchangecook_radiobutton)
        self.select_button = qtw.QPushButton('select')
        vlayout.addWidget(self.select_button)

        self.groupbox.setLayout(vlayout)

        # mainlayout
        main_layout = qtw.QFormLayout()
        main_layout.addRow(activeuser_label)
        main_layout.addRow(heading_label)
        main_layout.setVerticalSpacing(40)
        main_layout.addRow(self.groupbox)

        self.setLayout(main_layout)

        # functionality
        self.select_button.clicked.connect(self.open_box)


        self.show()


    def open_box(self):
        msgb = qtw.QMessageBox()
        msgb.setWindowTitle("title")
        msgb.setText("hier sthet was")
        msgb.move(self.pos().x(), self.pos().y())
        run = msgb.exec_()

        # msgb = qtw.QMessageBox()
        # msgb.addButton()
        # if x open new windwo
        #



if __name__ == '__main__':
    app = qtw.QApplication(sys.argv)
    w = Startscreen()
    sys.exit(app.exec_())


A widget has a position relative to its parent, and if it does not have a parent then it will be relative to the screen.小部件具有相对于其父级的位置,如果它没有父级,则它将相对于屏幕。 And in the case of msgb it belongs to the second case so you will have to convert the coordinate of the center of the window to global coordinates (that is to say with respect to the screen).在 msgb 的情况下,它属于第二种情况,因此您必须将窗口中心的坐标转换为全局坐标(即相对于屏幕)。 Even doing the above it will not be centered because the position is with respect to the topleft, that is, the msgb topleft will be in the center of the screen which is not desirable so you have to also take into account the size of the msgb.即使执行上述操作,它也不会居中,因为位置相对于左上角,即 msgb topleft 将位于屏幕的中心,这是不可取的,因此您还必须考虑 msgb 的大小. And the size of the msgb before and after it is displayed is different so with a QTimer it will be enough:并且显示前后的 msgb 大小不同,因此使用 QTimer 就足够了:

def open_box(self):
    msgb = qtw.QMessageBox()
    msgb.setWindowTitle("title")
    msgb.setText("hier sthet was")
    qtc.QTimer.singleShot( 0, lambda: msgb.move( self.mapToGlobal(self.rect().center() - msgb.rect().center()) ), )
    run = msgb.exec_()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM