python - 添加多个停靠的小部件

标签 python python-3.x pyqt qt5 pyqt5

我尝试添加多个选项卡式 QDockWidget,但不知何故我只能同时停靠。

mwe代码:

from PyQt5.QtWidgets import *
from PyQt5.QtCore import *

class DockWindow(QDockWidget):
    def __init__(self, parent, name):
        super().__init__(parent)

        parent.addDockWidget(Qt.TopDockWidgetArea, self)
        self.setWindowTitle(name)
        child = parent.findChildren(DockWindow)

        if len(child) > 1:
            parent.tabifyDockWidget(self, child[0])
            self.raise_()


app = QApplication(sys.argv)
main = QMainWindow()

for i in range(10):
    DockWindow(main, str(i))


main.show()
sys.exit(qApp.exec_())

enter image description here

最佳答案

根据docs :

void QMainWindow::tabifyDockWidget(QDockWidget * first, QDockWidget * second)

Moves second dock widget on top of first dock widget, creating a tabbed docked area in the main window.

从第一个参数得出的结论必须是初始的QDockWidget,如果有child[0],第二个是新的QDockWidget,在你的情况下self。通过更改即可解决问题:

parent.tabifyDockWidget(self, child[0])

至:

parent.tabifyDockWidget(child[0], self)

屏幕截图:

enter image description here

关于python - 添加多个停靠的小部件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45765653/

相关文章:

python - 如何在Python插件中使用多个窗口

php - 纯基于网络的版本控制系统

python - 在 python pickle 文件中保留 numpy strides

python - 如何以正确的顺序打印Scrapy项目键?

python - 在 Python 3.5 和 3.6 之间切换

mysql - 在 mysql datetime 和 python timestamp 之间转换的正确方法是什么?

python - 使用 Apache Beam python 创建谷歌云数据流模板时出现 RuntimeValueProviderError

python - abs() 的错误操作数类型 : 'list'

python - PyQt5 为每个组合框选项呈现不同的复选框集

python - 制作一个在代码执行完成之前显示的窗口