pyqt5 qgraphicsview宽度和高度

xfb7svmp  于 2021-09-08  发布在  Java
关注(0)|答案(1)|浏览(840)

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

文件: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()

我试图从左下角到右上角画一条线。
使用上述代码,输出为:

我认为这可能是一个尺度问题(变量self.available\u width,self.available\u height)。
我做错了什么?

sulc1iza

sulc1iza1#

调整大小(和许多其他几何特性)不会同步更新,而是将标志(如Maximized)或所需大小(使用resize()方法)发送到操作系统(通过库)以修改现有窗口,然后操作系统实现该窗口并返回应用更改后获得的值。因此,不建议立即进行计算,而是延迟:

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

相关问题