[英]pyqt - position child widget from qt designer near parent
I use Qt Designer
to create a child widget (layout) - tree_view_widget_ui.py
我使用
Qt Designer
创建子窗口小部件(布局) tree_view_widget_ui.py
class Ui_Form(object):
def setupUi(self, Form, parent = None):
Form.setObjectName(_fromUtf8("Form"))
Form.setEnabled(True)
Form.resize(200, 300)
Form.setMinimumSize(QtCore.QSize(200, 300))
self.gridLayout = QtGui.QGridLayout(Form)
self.gridLayout.setObjectName(_fromUtf8("gridLayout"))
self.treeWidget = QtGui.QTreeWidget(Form)
self.treeWidget.setEnabled(True)
self.treeWidget.setMinimumSize(QtCore.QSize(200, 300))
font = QtGui.QFont()
self.treeWidget.setFont(font)
self.treeWidget.setDragDropMode(QtGui.QAbstractItemView.DragDrop)
self.treeWidget.setObjectName(_fromUtf8("treeWidget"))
self.treeWidget.headerItem().setText(0, _fromUtf8("1"))
self.gridLayout.addWidget(self.treeWidget, 0, 0, 1, 1)
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
Form.setWindowTitle(_translate("Form", "Tree View", None))
self.treeWidget.setSortingEnabled(True)
and I initialize child user-interface in tree_view_widget.py
然后在
tree_view_widget.py
初始化子用户界面
from tree_view_widget_ui import Ui_Form
class TreeViewWidget(QWidget):
def __init__(self, parent=None, flags=0):
super(TreeViewWidget, self).__init__(parent)
self.ui = Ui_Form()
self.ui.setupUi(self, parent = parent)
And the child widget is intialized from parent main.py
and opened with signal: 子窗口小部件从父
main.py
初始化,并通过信号打开:
# tree widget
from tree_view_widget.tree_view_widget import TreeViewWidget
class MainWindow(QtGui.QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
self.flags = QtCore.Qt.Window
# tree view widget
self.TreeViewWidget = TreeViewWidget(parent=self)
self.TreeViewWidget.setWindowFlags(self.flags)
self.TreeViewWidget.show()
My question is: How can I position the child widget near parent widget that top left/right corners will coincide and not modifying file tree_view_widget_ui.py
as this code is automatically generated from Qt Designer
. 我的问题是:我如何将子窗口小部件放置在父窗口小部件附近,以使左/右上角重合,并且不修改文件
tree_view_widget_ui.py
因为此代码是从Qt Designer
自动生成的。 I managed to position child widget to target location but only when modifying file tree_view_widget_ui.py
but I would like to avoid that if possible. 我设法将子窗口小部件定位到目标位置,但是仅在修改文件
tree_view_widget_ui.py
这样做,但我想尽可能避免这种情况。
in class MainWindow
: 在
class MainWindow
:
You will want to use self.TreeViewWidget.setGeometry(x, y, width, height)
or you could use the move(x, y) function. 您将要使用
self.TreeViewWidget.setGeometry(x, y, width, height)
或使用move(x,y)函数。
See docs for setGeometry and move functions 请参阅文档以获取setGeometry和move函数
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.