如何在PyQt4中将QDockWidget添加到QFrame

时间:2013-02-10 07:47:59

标签: python-2.7 pyqt4 qdockwidget dockable

如何将QDockWidget添加到QFrame?因为QFrame没有addDockWidget !!!

from PyQt4.QtCore import *
from PyQt4.QtGui import *
import sys

app = QApplication( sys.argv )

qmainwin = QFrame()
#qmainwin.setWindowFlags(Qt.FramelessWindowHint)
s = QWidget()
vboxlayout = QVBoxLayout( )

spin1 = QSpinBox()
vboxlayout.addWidget( spin1 )

spin2 = QSpinBox()
vboxlayout.addWidget( spin2 )
s.setLayout( vboxlayout )

qdock = QDockWidget( )
qdock.setWindowFlags(Qt.FramelessWindowHint)
qdock.setWidget( s )
qmainwin.addDockWidget( Qt.TopDockWidgetArea, qdock )
qmainwin.show()

app.exec_()

1 个答案:

答案 0 :(得分:2)

这个怎么样?我将QDockWidget添加到QLayout,然后设置QFrame的布局。

import sys
from PyQt4 import QtGui, QtCore

class Example(QtGui.QWidget):

    def __init__(self):
        super(Example, self).__init__()

        self.initUI()

    def initUI(self):

        frame = QtGui.QFrame()
        frame.setFrameStyle(QtGui.QFrame.Panel |
                QtGui.QFrame.Plain)

        label = QtGui.QLabel('This is random text')

        dockWidget = QtGui.QDockWidget('Main', self)
        # set the widget to non-movable, non-floatable and non-closable
        dockWidget.setFeatures(dockWidget.NoDockWidgetFeatures)
        dockWidget.setWidget(label)

        # add the QDockWidget to the QLayout
        hbox = QtGui.QHBoxLayout()
        hbox.addWidget(dockWidget)

        # set the layout of the QFrame
        frame.setLayout(hbox)

        # create another QLayout to add QFrame
        vbox = QtGui.QVBoxLayout()
        vbox.addStretch(1)
        vbox.addWidget(frame)

        self.setLayout(vbox)

        self.setGeometry(300, 300, 500, 400)
        self.setWindowTitle('Test')

def main():

    app = QtGui.QApplication(sys.argv)
    ex = Example()
    ex.show()
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()