我使用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)
我在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)
子窗口小部件从父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()
我的问题是:如何将子窗口小部件放置在父窗口小部件附近,左上角/右上角将重合,而不是修改文件tree_view_widget_ui.py
,因为此代码是从{{{}}自动生成的1}}。我设法将子窗口小部件定位到目标位置,但仅在修改文件Qt Designer
时,但我希望尽可能避免这种情况。
答案 0 :(得分:1)
class MainWindow
中的:
您需要使用self.TreeViewWidget.setGeometry(x, y, width, height)
,或者您可以使用move(x,y)函数。
查看setGeometry和move函数的文档