如何在 PyQt5 中相对于父项 window 移动子项 window?
How to move a child window relative to a parent window in PyQt5?
我有以下代码,但我不希望新 window 设置在主 window 的中心。我更愿意将它向左移动一点(例如向左 20 像素,向上 20 像素),我已经尝试过 moveTo() 和 moveLeft(),但无法真正弄明白。我可以使用 topLeft() 进行管理,但它与主要 window 无关。下面的代码用于居中。问题是如何更改我的代码以获得上述结果?
class Form(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
self.ui = Ui_suffix_editor_panel()
self.ui.setupUi(self)
self.suffix_list = Suffix_window(parent=self)
self.ui.show.clicked.connect(self.show_all_suffix_list)
self.show()
def show_all_suffix_list(self):
self.suffix_list.ui.all_suffix_list.clear()
open_known_list = open("known.txt", "r")
for known in open_known_list.read().split('\n'):
self.suffix_list.ui.all_suffix_list.insertItem(0, known)
self.suffix_list.show()
class Suffix_window(QWidget):
def __init__(self, parent=None):
self.parent = parent
QWidget.__init__(self)
self.ui = Ui_suffix_widget()
self.ui.setupUi(self)
self.setWindowFlags(QtCore.Qt.FramelessWindowHint)
self.setAttribute(QtCore.Qt.WA_TranslucentBackground)
self.ui.exit_list_view.clicked.connect(lambda: self.close())
def showEvent(self, event):
if not event.spontaneous():
geo = self.geometry()
geo.moveLeft(self.parent.geometry().left())
QtCore.QTimer.singleShot(0, lambda: self.setGeometry(geo))
看起来像这样:
想要的结果:
解决这个问题的一种方法是首先使子矩形相对于父矩形居中,然后通过相对偏移量平移结果:
class Form(QMainWindow):
...
def show_all_suffix_list(self):
self.suffix_list.ui.all_suffix_list.clear()
open_known_list = open("known.txt", "r")
for known in open_known_list.read().split('\n'):
self.suffix_list.ui.all_suffix_list.insertItem(0, known)
# set initial size
rect = QtCore.QRect(0, 0, 300, 300)
# centre on parent
rect.moveCenter(self.geometry().center())
# adjust by relative offset (negative values go left/up)
rect.translate(QtCore.QPoint(-50, 0))
self.suffix_list.setGeometry(rect)
self.suffix_list.show()
我有以下代码,但我不希望新 window 设置在主 window 的中心。我更愿意将它向左移动一点(例如向左 20 像素,向上 20 像素),我已经尝试过 moveTo() 和 moveLeft(),但无法真正弄明白。我可以使用 topLeft() 进行管理,但它与主要 window 无关。下面的代码用于居中。问题是如何更改我的代码以获得上述结果?
class Form(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
self.ui = Ui_suffix_editor_panel()
self.ui.setupUi(self)
self.suffix_list = Suffix_window(parent=self)
self.ui.show.clicked.connect(self.show_all_suffix_list)
self.show()
def show_all_suffix_list(self):
self.suffix_list.ui.all_suffix_list.clear()
open_known_list = open("known.txt", "r")
for known in open_known_list.read().split('\n'):
self.suffix_list.ui.all_suffix_list.insertItem(0, known)
self.suffix_list.show()
class Suffix_window(QWidget):
def __init__(self, parent=None):
self.parent = parent
QWidget.__init__(self)
self.ui = Ui_suffix_widget()
self.ui.setupUi(self)
self.setWindowFlags(QtCore.Qt.FramelessWindowHint)
self.setAttribute(QtCore.Qt.WA_TranslucentBackground)
self.ui.exit_list_view.clicked.connect(lambda: self.close())
def showEvent(self, event):
if not event.spontaneous():
geo = self.geometry()
geo.moveLeft(self.parent.geometry().left())
QtCore.QTimer.singleShot(0, lambda: self.setGeometry(geo))
看起来像这样:
想要的结果:
解决这个问题的一种方法是首先使子矩形相对于父矩形居中,然后通过相对偏移量平移结果:
class Form(QMainWindow):
...
def show_all_suffix_list(self):
self.suffix_list.ui.all_suffix_list.clear()
open_known_list = open("known.txt", "r")
for known in open_known_list.read().split('\n'):
self.suffix_list.ui.all_suffix_list.insertItem(0, known)
# set initial size
rect = QtCore.QRect(0, 0, 300, 300)
# centre on parent
rect.moveCenter(self.geometry().center())
# adjust by relative offset (negative values go left/up)
rect.translate(QtCore.QPoint(-50, 0))
self.suffix_list.setGeometry(rect)
self.suffix_list.show()