PyQt5 QgraphicsView ancho y alto

0

archivo: main.py

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'main.ui'
#
# Created by: PyQt5 UI code generator 5.15.2
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again.  Do not edit this file unless you know what you are doing.


from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(800, 600)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.gridLayout = QtWidgets.QGridLayout(self.centralwidget)
        self.gridLayout.setContentsMargins(0, 0, 0, 0)
        self.gridLayout.setSpacing(0)
        self.gridLayout.setObjectName("gridLayout")
        self.scrollArea = QtWidgets.QScrollArea(self.centralwidget)
        self.scrollArea.setWidgetResizable(True)
        self.scrollArea.setObjectName("scrollArea")
        self.scrollAreaWidgetContents = QtWidgets.QWidget()
        self.scrollAreaWidgetContents.setGeometry(QtCore.QRect(0, 0, 798, 598))
        self.scrollAreaWidgetContents.setObjectName("scrollAreaWidgetContents")
        self.gridLayout_2 = QtWidgets.QGridLayout(self.scrollAreaWidgetContents)
        self.gridLayout_2.setContentsMargins(0, 0, 0, 0)
        self.gridLayout_2.setSpacing(0)
        self.gridLayout_2.setObjectName("gridLayout_2")
        self.graphicsView = QtWidgets.QGraphicsView(self.scrollAreaWidgetContents)
        self.graphicsView.setMinimumSize(QtCore.QSize(0, 0))
        self.graphicsView.setObjectName("graphicsView")
        self.gridLayout_2.addWidget(self.graphicsView, 0, 0, 1, 1)
        self.scrollArea.setWidget(self.scrollAreaWidgetContents)
        self.gridLayout.addWidget(self.scrollArea, 0, 0, 1, 1)
        MainWindow.setCentralWidget(self.centralwidget)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

archivo: run_me.py

from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import Qt
from main import Ui_MainWindow as Ui_MainWindow

import sys

class Main_Code:
    def __init__(self):
        self.app = QtWidgets.QApplication(sys.argv)
        self.MainWindow = QtWidgets.QMainWindow()
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self.MainWindow)
        self.MainWindow.showMaximized()
        
        self.set_input_data()
        self.set_view_variables()
        self.print_lines()
        
        sys.exit(self.app.exec_())
    
    
    def set_input_data(self):
        self.schedule_items = []
        self.schedule_items.append({"title":"Title for schedule item 1","duration milliseconds":1000*51})
        self.schedule_items.append({"title":"Title for schedule item 2","duration milliseconds":1000*120})
        self.schedule_items.append({"title":"Title for schedule item 3","duration milliseconds":1000*500})
        self.live_duration = 1000*60*60*2.5 #2 hours and 30 minutes
    
        
    def set_view_variables(self):
        self.header_timeline_height = 40
        self.schedule_items_height_spacing = 50
        self.schedule_item_timeline_height = 40
        self.schedule_item_movable_height = 120
        
        #get QGraphicsView dimensions
        self.ui.graphicsView.setAlignment(Qt.AlignTop | Qt.AlignLeft)
        self.available_width = self.ui.graphicsView.width()
        self.available_height = self.ui.graphicsView.height() 
        self.ui.graphicsView.resize(self.available_width, self.available_height)
        
        
    def print_lines(self):
        #print timeline line (top)
        self.MainWindow.setStyleSheet("background-color: black;")
        self.ui.graphicsView.setStyleSheet("background-color: yellow;")
        self.scene = QtWidgets.QGraphicsScene()
        self.scene.setBackgroundBrush(QtGui.QBrush(Qt.red))
        self.ui.graphicsView.setScene(self.scene)
        self.ui.graphicsView.setSceneRect(0,0,self.available_width,self.available_height)
        
        self.timeline_line_top = QtWidgets.QGraphicsLineItem(0,self.MainWindow.height(),self.available_width,0)
        self.scene.addItem( self.timeline_line_top )
        
program = Main_Code()

Estoy tratando de trazar una línea desde la esquina inferior izquierda hasta la esquina superior derecha.

Con el código anterior, la salida es: ingrese la descripción de la imagen aquí

Creo que esto es quizás un problema de escala (variables self.available_width, self.available_height).

¿Que estoy haciendo mal?

1

El cambio de tamaño (y muchas otras propiedades geométricas) no se actualizan sincrónicamente, sino que las banderas (como maximezed) o el tamaño requerido (usando el método resize ()) se envían al sistema operativo (a través de una biblioteca) para modificar la ventana existente y luego el sistema operativo lo implementa y devuelve los valores que obtuvo después de aplicar los cambios. Por lo que no es recomendable hacer cálculos al instante sino darle un retraso:

from PyQt5 import QtCore, QtGui, QtWidgets
from main import Ui_MainWindow as Ui_MainWindow

import sys


class Main_Code:
    def __init__(self):
        self.app = QtWidgets.QApplication(sys.argv)
        self.MainWindow = QtWidgets.QMainWindow()
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self.MainWindow)
        self.MainWindow.showMaximized()

        self.init_ui()

        sys.exit(self.app.exec_())

    def init_ui(self):
        self.ui.graphicsView.setAlignment(QtCore.Qt.AlignTop | QtCore.Qt.AlignLeft)

        self.MainWindow.setStyleSheet("background-color: black;")
        self.ui.graphicsView.setStyleSheet("background-color: yellow;")
        self.scene = QtWidgets.QGraphicsScene()
        self.scene.setBackgroundBrush(QtGui.QBrush(QtCore.Qt.red))
        self.ui.graphicsView.setScene(self.scene)

        QtCore.QTimer.singleShot(100, self.draw_line)

    def draw_line(self):
        r = self.ui.graphicsView.mapToScene(
            self.ui.graphicsView.viewport().rect()
        ).boundingRect()
        self.timeline_line_top = QtWidgets.QGraphicsLineItem(
            QtCore.QLineF(r.bottomLeft(), r.topRight())
        )
        self.scene.addItem(self.timeline_line_top)


program = Main_Code()