ignoring_gravity
ignoring_gravity

Reputation: 10476

Make widget as big as possible

Here is some code I've written (and/or adapted from other sources):

import numpy as np
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure
from PyQt5.QtWidgets import (QGraphicsView, QGraphicsScene, QMainWindow,
                             QApplication, QWidget, QVBoxLayout, 
                             QDesktopWidget)
from PyQt5.QtGui import QBrush
from PyQt5.QtCore import Qt
import sys

class View(QGraphicsView):

    def __init__(self):
        super(View, self).__init__()

        self.initScene()

    def initScene(self):     

        self.scene = QGraphicsScene()
        self.canvas = Fig()
        self.setBackgroundBrush(QBrush(Qt.red))
        self.canvas.draw()
        self.setScene(self.scene)
        self.scene.addWidget(self.canvas)

class Fig(FigureCanvas):

    def __init__(self, *args,**kwargs):
        self.factor = kwargs.pop("factor", 2)
        FigureCanvas.__init__(self, Figure(), *args,**kwargs)
        self.plot()

    def plot(self):
        self.ax = self.figure.add_subplot(111)
        data = np.random.rand(1000)
        self.ax.plot(data, '-')


class Window(QMainWindow):

    def __init__(self):

        QMainWindow.__init__(self)

        desktop = QDesktopWidget()
        rect = desktop.availableGeometry()
        self.setGeometry(rect.width()/10, rect.height()/10, rect.width()/1.2,
                   rect.height()/1.2)

        self.view = View()
        self.setCentralWidget(self.view)

app = QApplication(sys.argv)
window = Window()
window.show()
app.exec_()

It produces the following window as its output: enter image description here

I would like the plot in the middle to occupy as much space as possible, so that the red background becomes invisible.

This happens if I exclude the command setting the size of the window, that is indeed what happens. However, the window is then too small - I need to make it bigger.

I have tried using self.view.setGeometry, but it doesn't seem to make a difference. I've had a look at the available modules in the documentation, but can't tell which might help.

Upvotes: 0

Views: 76

Answers (1)

eyllanesc
eyllanesc

Reputation: 243897

If you want to establish that the plot is shown covering all the available space within the window I see it unnecessary to use QGraphicsView since Fig is a QWidget that can be used directly in setCentralWidget():

class Fig(FigureCanvas):
    def __init__(self, *args,**kwargs):
        self.factor = kwargs.pop("factor", 2)
        FigureCanvas.__init__(self, Figure(), *args,**kwargs)
        self.plot()

    def plot(self):
        self.ax = self.figure.add_subplot(111)
        data = np.random.rand(1000)
        self.ax.plot(data, '-')


class Window(QMainWindow):

    def __init__(self):
        QMainWindow.__init__(self)
        desktop = QDesktopWidget()
        rect = desktop.availableGeometry()
        self.setGeometry(rect.width()/10, rect.height()/10, rect.width()/1.2,
                   rect.height()/1.2)

        self.canvas = Fig()
        self.canvas.draw()
        self.setCentralWidget(self.canvas)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = Window()
    window.show()
    sys.exit(app.exec_())

update: Using QScrollArea

class Fig(FigureCanvas):
    def __init__(self, *args,**kwargs):
        self.factor = kwargs.pop("factor", 2)
        FigureCanvas.__init__(self, Figure(), *args,**kwargs)
        self.plot()

    def plot(self):
        self.ax = self.figure.add_subplot(111)
        data = np.random.rand(1000)
        self.ax.plot(data, '-')

    def showEvent(self, event):
        self.setFixedSize(self.size())
        FigureCanvas.showEvent(self, event)


class Window(QMainWindow):

    def __init__(self):
        QMainWindow.__init__(self)
        desktop = QDesktopWidget()
        rect = desktop.availableGeometry()
        self.setGeometry(rect.width()/10, rect.height()/10, rect.width()/1.2,
                   rect.height()/1.2)

        self.canvas = Fig()
        self.canvas.draw()

        scrollArea = QScrollArea()
        scrollArea.setWidgetResizable(True)
        scrollArea.setWidget(self.canvas)
        self.setCentralWidget(scrollArea)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = Window()
    window.show()
    sys.exit(app.exec_())

Upvotes: 2

Related Questions