0

file: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_())

file: 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()

I am trying to draw a line from the bottom left corner to the top right corner.

With the above code the output is: enter image description here

I think this is maybe a scale problem (variables self.available_width,self.available_height).

What I am doing wrong?

1 Answer 1

1

The resize (and many other geometric properties) are not updated synchronously but rather the flags (such as maximezed) or the required size (using resize() method) are sent to the OS (through a library) to modify the existing window and then the OS implements it and returns the values it got after applying the changes. So it is not advisable to do calculations instantly but to give it a delay:

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()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.