PDA

View Full Version : Help me maintain aspect ratio of widget within QSplitter when resizing using PyQt5



SamQ
24th July 2021, 17:32
Hi all!

Ubuntu Linux OS 18.04
PyQt5

I have a custom graph widget & a QTextEdit widget in a vertical QSplitter, and a QTabWidget in a Horizontal QSplitter.

All resizes well, but I want the custom graph widget to keep a square aspect ratio whenever it is resized horizontally or vertically using the QSplitter.

I want the custom graph widget to get bigger and smaller upon resizing while maintaining a square aspect ratio. I scoured google, and looked at the docs, but due to many ways of applying options nothing is outstandingly clear.

Also, it would be nice if I could make the whole QSplitter expand when resizing the app mainwindow.

Could someone please show me how to do both these things?




from PyQt5 import QtWidgets, uic
from PyQt5.QtWidgets import QTabWidget, QTabBar
from pyqtgraph import PlotWidget
import pyqtgraph as pg
import sys
import os

class MainWindow(QtWidgets.QMainWindow):
def __init__(self, *args, **kwargs):
super(MainWindow, self).__init__(*args, **kwargs)

# Load the UI Page
self.ui = uic.loadUi("mainwindow.ui", self)
"""
ui->UtilityTabs_tabWidget->setStyleSheet("QTabWidget::pane {"
"border: 1px solid lightgray;"
"background-color: rgb(245, 245, 245); }"
"QTabBar::tab {"
"color: lightgray;"
"background-color: #654321;"
"border: 1px solid red;}"
"QTabBar::tab:selected {"
"color: black;"
"background-color: #EEEE9B;"
"margin-bottom: -1px; }");
"""
self.ui.tabWidget.setTabText(self.ui.tabWidget.ind exOf(self.ui.tab), " PGN ")
self.ui.tabWidget.setTabText(
self.ui.tabWidget.indexOf(self.ui.tab_2), " tab 2 "
)
self.ui.tabWidget.setStyleSheet(
"QTabWidget::pane {"
"border: 1px solid lightgray;}"
"QTabBar::tab {"
"color: lightgray;"
"background-color: #654321;"
"border: 1px solid red;}"
"QTabBar::tab:selected {"
"color: black;"
"background-color: #EEEE9B;"
"padding-left: 4px;"
"padding-right: 4px;"
"margin-bottom: -1px; }"
) # "width: 52px;" "background-color: rgb(245, 245, 245); }" "background-color: #654321; }"
# background-color: rgb(101, 67, 33); # brown
# self.graphicsProxyWidget().resizeEvent()
# self.ui.graphWidget.

self.plot(
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10], [30, 32, 34, 32, 33, 31, 29, 32, 35, 45]
)

def plot(self, hour, temperature):
self.graphWidget.plot(hour, temperature)


def main():
app = QtWidgets.QApplication(sys.argv)
main = MainWindow()
main.show()
sys.exit(app.exec_())

if __name__ == "__main__":
main()




SamQ