本文介绍了pyqt:在Maya中调整qdockwidget的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经为Maya制作了工具.而且我在QMainWindow中使用了QDockWidget来实现整个UI组合.如您所知,我可以"saveState"和"restoreState"来保留QToolBar,QDockWidget等.

I've made a tool for Maya. And I used QDockWidget within QMainWindow for entire UI composition. As you and I know, I can "saveState" and "restoreState" to retain QToolBar, QDockWidget, and so on.

我不知道原因,但是在Maya中,我无法"restoreState"实际状态.它可以恢复QToolBar,QDockWidget的所有状态,但QDockWidget的大小除外.因此,我想尝试以编程方式调整QDockWidget的大小.有什么办法吗?

I don't know the reason, but in Maya I can't "restoreState" the actual state. It can restore all of the state for QToolBar, QDockWidget except the size of QDockWidget. So I want to try to resize QDockWidget programmatically. Is there any way to do that?

推荐答案

尝试重新实现 sizeHint (用于停靠小部件的顶级内容窗口小部件):

Try reimplementing the sizeHint for the top-level content widget of the dock-widget:

class MyWidget(QtGui.QWidget):
    _sizehint = None

    def setSizeHint(self, width, height):
        self._sizehint = QtCore.QSize(width, height)

    def sizeHint(self):
        if self._sizehint is not None:
            return self._sizehint
        return super(MyWidget, self).sizeHint()

更新

这是一个简单的演示,展示了一种实现此方法的方法:

Here's a simple demo that shows one way to implement this:

from PyQt4 import QtGui, QtCore

class DockContents(QtGui.QWidget):
    _sizehint = None

    def setSizeHint(self, width, height):
        self._sizehint = QtCore.QSize(width, height)

    def sizeHint(self):
        print('sizeHint:', self._sizehint)
        if self._sizehint is not None:
            return self._sizehint
        return super(MyWidget, self).sizeHint()

class Window(QtGui.QMainWindow):
    def __init__(self):
        QtGui.QMainWindow.__init__(self)

        self.setCentralWidget(QtGui.QTextEdit(self))

        self.dock = QtGui.QDockWidget('Tool Widget', self)
        self.addDockWidget(QtCore.Qt.LeftDockWidgetArea, self.dock)

        contents = DockContents(self)
        contents.setSizeHint(400, 100)
        layout = QtGui.QVBoxLayout(contents)
        layout.setContentsMargins(0, 0, 0, 0)
        self.toolwidget = QtGui.QListWidget(self)
        layout.addWidget(self.toolwidget)

        self.dock.setWidget(contents)

if __name__ == '__main__':

    import sys
    app = QtGui.QApplication(sys.argv)
    window = Window()
    window.setGeometry(500, 300, 600, 400)
    window.show()
    sys.exit(app.exec_())

这篇关于pyqt:在Maya中调整qdockwidget的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-10 21:53