[英]How do I make QDockWidget expand to fill all the available space? And why can't I put more than two docks side by side?
I am creating an application in pyqt5 to plot multiple figures side by side and above/below each other, and I want to make the plots dockable.我正在 pyqt5 中创建一个应用程序来并排和上下绘制多个图形,并且我想让这些图可以停靠。 The result is not behaving the way I want/would expect and after a long time of searching the internet for answers/solutions I still don't know why.结果并没有按照我想要/期望的方式行事,经过长时间在互联网上搜索答案/解决方案后,我仍然不知道为什么。
So far this is my code:到目前为止,这是我的代码:
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure
from PyQt5 import QtWidgets, QtCore
import traceback
import logging
def exception_hook(exc_type, exc_value, exc_tb):
tb = "".join(traceback.format_exception(exc_type, exc_value, exc_tb))
logging.error('\n' + tb)
QtWidgets.QApplication.quit()
class PlotWindow(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super(PlotWindow, self).__init__(parent=parent)
self.setWindowTitle('plot window')
self.setCentralWidget(QtWidgets.QWidget(self))
def add_figure(self, figure):
mplCanvas = FigureCanvas(figure)
mplCanvas.setSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding)
dockableMplCanvas = QtWidgets.QDockWidget(self)
dockableMplCanvas.setWidget(mplCanvas)
self.addDockWidget(QtCore.Qt.RightDockWidgetArea, dockableMplCanvas)
if __name__ == '__main__':
import sys
logging.basicConfig(level=logging.DEBUG)
logging.getLogger('matplotlib').setLevel(logging.ERROR)
sys.excepthook = exception_hook
app = QtWidgets.QApplication(sys.argv)
pw = PlotWindow()
for _ in range(4):
fig = Figure(tight_layout=True)
ax = fig.add_subplot()
pw.add_figure(fig)
pw.show()
exit(app.exec_())
It results in the following:结果如下:
If I now rearrange the plots by first putting one of them on the left of the other three:如果我现在通过首先将其中一个放在其他三个的左侧来重新排列图:
..And then putting one underneath the one on the left: ..然后把一个放在左边的下面:
Now if I rescale the window then the space between the two columns of plots expands but the plots don't expand to fill the space:现在,如果我重新缩放窗口,那么两列图之间的空间会扩展,但图不会扩展以填充空间:
Why is that?这是为什么? and how can I fix it?我该如何解决?
The central widget is resizing to fill the space between the two columns of plots, the solution is to add:中央小部件正在调整大小以填充两列图之间的空间,解决方案是添加:
self.centralWidget().setFixedSize(0, 0)
to prevent the central widget from expanding防止中央小部件扩展
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.