[英]Applying a layout to a QDockWidget in PyQt5
I'm trying to create a docked interface in PyQt5, creating a BoxLayout contining my various widgets, and then applying this layout to the QDockWidget. 我正在尝试在PyQt5中创建一个停靠接口,创建一个包含各种小部件的BoxLayout,然后将此布局应用于QDockWidget。
My approach so far has been: 到目前为止,我的方法是:
self.layout = QtWidgets.QBoxLayout(2)
self.layout.addWidget(self.widget_one)
self.layout.addWidget(self.widget_two)
self.docked = QtWidgets.QDockWidget()
self.docked.setLayout(self.layout)
self.docked.show()
I'm currently getting an error as follows: 我目前收到的错误如下:
QWidget::setLayout: Attempting to set QLayout "" on QDockWidget "",
which already has a layout
However, in my code above I can't see where I have added a layout previously, and can't find anything in the documentation to aid me in fixing this. 但是,在我上面的代码中,我无法看到我之前添加了布局的位置,并且无法在文档中找到任何内容来帮助我解决此问题。
Where have I gone wrong in my approach? 我的方法在哪里出错了?
QDockWidget
already have a default layout so that generates this error, the correct thing is to add an empty widget and in that widget place the layout you want: QDockWidget
已经有一个默认布局,因此生成此错误,正确的是添加一个空小部件,并在该小部件中放置您想要的布局:
self.layout = QtWidgets.QBoxLayout(2)
self.layout.addWidget(self.widget_one)
self.layout.addWidget(self.widget_two)
self.docked = QtWidgets.QDockWidget()
self.dockedWidget = QtWidgets.QWidget()
self.docked.setWidget(self.dockedWidget)
self.dockedWidget.setLayout(self.layout)
self.docked.show()
Example: 例:
class DockDemo(QMainWindow):
def __init__(self, parent=None):
super(DockDemo, self).__init__(parent)
self.setCentralWidget(QTextEdit())
self.docked = QDockWidget("Dockable", self)
self.addDockWidget(Qt.LeftDockWidgetArea, self.docked)
self.dockedWidget = QWidget(self)
self.docked.setWidget(self.dockedWidget)
self.setWindowTitle("Dock demo")
self.dockedWidget.setLayout(QVBoxLayout())
for i in range(5):
self.dockedWidget.layout().addWidget(QPushButton("{}".format(i)))
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
ex = DockDemo()
ex.show()
sys.exit(app.exec_())
Screenshot: 截图:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.