pyqt - 从父级附近的qt设计者定位子窗口小部件

时间:2014-06-27 15:49:57

标签: python pyqt4 pyside qt-designer pyqt5

我使用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时,但我希望尽可能避免这种情况。

1 个答案:

答案 0 :(得分:1)

class MainWindow中的

您需要使用self.TreeViewWidget.setGeometry(x, y, width, height),或者您可以使用move(x,y)函数。

查看setGeometrymove函数的文档