python-2.7 - 如何在PyQt4中将QDockWidget添加到QFrame

标签 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_()

最佳答案

这个怎么样?我将 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()

关于python-2.7 - 如何在PyQt4中将QDockWidget添加到QFrame,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14795786/

相关文章:

python - 收集所有停靠的小部件及其位置

python - 用python解析JSON : blank fields

python - 如何更改标题数据

python - 当我运行 python 程序时,如何阻止命令提示符窗口打开?

python - pyqt QTreeView中选择行和列

python - PyQt4:将操作系统日期/时间打印到标签

c++ - 在 QMainWindows 之间拖动 QDockWidgets

python - 如何在pyqt(QGIS)中捕捉QDockWidget的KeyPressedEvent

arrays - 将 numpy 数组更改为从零开始

python - 过程在导入时的执行方式与在其 native 模块中运行时的执行方式不同