Custom QDockWidget display

孤者浪人 提交于 2019-12-12 08:40:41

问题


How would you get a display of dockwidgets/centralwidget in which the dockwidget in the Qt::BottomDockWidgetArea or Qt::TopDockWidgetArea doesn't take Qt::LeftDockWidgetArea nor Qt::RighDockWidgetArea space?

This is the actual display, with 2 dockwidgets and the central widget at the top right:

This would be the preferred display:


回答1:


you probably should use the QMainWindow's corner functionality to get the behavior you wanted.

Something like this may work (can't test whether its compiles, sorry):

mainWindow->setCorner(Qt::TopLeftCorner, Qt::LeftDockWidgetArea);
mainWindow->setCorner(Qt::TopRightCorner, Qt::RightDockWidgetArea);
mainWindow->setCorner(Qt::BottomLeftCorner, Qt::LeftDockWidgetArea);
mainWindow->setCorner(Qt::BottomRightCorner, Qt::RightDockWidgetArea);

See: * QMainWindow::setCorner(...)




回答2:


It seems that the (slightly bizarre) trick to get this working is to set a QMainWindow as the central widget of your main window.

Here's a PyQt port of this Qt FAQ example:

from PyQt4 import QtGui, QtCore

class MainWindow(QtGui.QMainWindow):
    def __init__(self):
        QtGui.QMainWindow.__init__(self)
        self.setWindowTitle('Extended Side Dock Areas')
        self.window = QtGui.QMainWindow(self)
        self.window.setCentralWidget(QtGui.QTextEdit(self.window))
        self.window.setWindowFlags(QtCore.Qt.Widget)
        self.setCentralWidget(self.window)
        self.dock1 = QtGui.QDockWidget(self.window)
        self.dock1.setWidget(QtGui.QTextEdit(self.dock1))
        self.window.addDockWidget(
            QtCore.Qt.BottomDockWidgetArea, self.dock1)
        self.dock2 = QtGui.QDockWidget(self)
        self.dock2.setAllowedAreas(
            QtCore.Qt.LeftDockWidgetArea | QtCore.Qt.RightDockWidgetArea)
        self.dock2.setWidget(QtGui.QLabel('Left Dock Area', self.dock2))
        self.addDockWidget(QtCore.Qt.LeftDockWidgetArea, self.dock2)
        self.dock3 = QtGui.QDockWidget(self)
        self.dock3.setWidget(QtGui.QLabel('Right Dock Area', self.dock3))
        self.addDockWidget(QtCore.Qt.RightDockWidgetArea, self.dock3)

if __name__ == '__main__':

    import sys
    app = QtGui.QApplication(sys.argv)
    window = MainWindow()
    window.show()
    sys.exit(app.exec_())


来源:https://stackoverflow.com/questions/13332832/custom-qdockwidget-display

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!