Expanding Weirdness
-
Hi,
So I'm having an issue where a QTextBox is getting cut off after a certain point of expanding. It seems to be acting like it has some kind of maximum width despite me never setting a maximum width. Here is my code:from pyqtgraph import QtCore, QtGui import sys from sensor_msgs.msg import Imu, MagneticField, NavSatFix from PyQt5 import QtCore, QtGui from PyQt5.QtCore import Qt import numpy as np import sys class statusUI(QtGui.QPushButton): def __init__(self): QtGui.QPushButton.__init__(self) #set the widget's height self.setFixedHeight(200) statusUILayout = QtGui.QHBoxLayout(self) self.scrollArea = dataScrollArea() statusUILayout.addWidget(self.scrollArea) self.setUpMagUI() def setUpMagUI(self): #create mag labels self.magneticFieldCovNameLabel = QtGui.QLabel("Magnetic Field Covariance: ") self.magneticFieldCovNumberLabel = QtGui.QLabel(str([0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]).translate(None, "'")) self.magneticFieldCovNameLabel.setAlignment(Qt.AlignLeft) self.magneticFieldCovNumberLabel.setAlignment(Qt.AlignLeft) self.scrollArea.addLine(self.magneticFieldCovNameLabel, self.magneticFieldCovNumberLabel) self.scrollArea.addStretch() class dataScrollArea(QtGui.QScrollArea): def __init__(self): QtGui.QScrollArea.__init__(self) self.setWidgetResizable(True) # making qwidget object content = QtGui.QWidget(self) self.setWidget(content) self.statusesLayout = QtGui.QVBoxLayout(content) self.scrollbar = self.verticalScrollBar() def addLine(self, widget1, widget2): lineWidget = QtGui.QFrame() lineWidget.setFrameStyle(QtGui.QFrame.StyledPanel) lineLayout = QtGui.QHBoxLayout() lineWidget.setLayout(lineLayout) lineLayout.setAlignment(Qt.AlignLeft) lineLayout.addWidget(widget1) lineLayout.addWidget(widget2) self.statusesLayout.addWidget(lineWidget) widget2.setWordWrap(True) def addStretch(self): self.statusesLayout.addStretch() app = QtGui.QApplication(sys.argv) main = QtGui.QWidget() layout = QtGui.QHBoxLayout(main) main.show() testWidget = QtGui.QHBoxLayout() layout.addWidget(statusUI()) sys.exit(app.exec_())
It seems to start expanding just fine.
But when I get to the last 0.000, it starts to take a lot more space than it usually would when expanding...
And then it just cuts off the last 0.000 for some reason.
Can someone tell me why this is happening? Thanks for any help!
I think I'm using PyQt5, although I have to import from pyqtgraph else it doesn't work for some reason. -
@Hexrin The problem is mainly caused by the line
lineLayout.setAlignment(Qt.AlignLeft)
that you probably used so that the first column occupies the minimum space but also makes the second column compress. Instead I use stretch factors.import sys from pyqtgraph import QtCore, QtGui class statusUI(QtGui.QPushButton): def __init__(self): QtGui.QPushButton.__init__(self) self.setMinimumHeight(200) statusUILayout = QtGui.QHBoxLayout(self) self.scrollArea = dataScrollArea() statusUILayout.addWidget(self.scrollArea) self.setUpMagUI() def setUpMagUI(self): text = str( [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0] ).translate({None: "'"}) self.magneticFieldCovNameLabel = QtGui.QLabel("Magnetic Field Covariance: ") self.magneticFieldCovNumberLabel = QtGui.QLabel(text) self.magneticFieldCovNameLabel.setAlignment(QtCore.Qt.AlignLeft) self.magneticFieldCovNumberLabel.setAlignment(QtCore.Qt.AlignLeft) self.scrollArea.addLine( self.magneticFieldCovNameLabel, self.magneticFieldCovNumberLabel ) self.scrollArea.addStretch() class dataScrollArea(QtGui.QScrollArea): def __init__(self): QtGui.QScrollArea.__init__(self) self.setWidgetResizable(True) content = QtGui.QWidget() self.setWidget(content) self.statusesLayout = QtGui.QVBoxLayout(content) def addLine(self, widget1, widget2): widget2.setWordWrap(True) lineWidget = QtGui.QFrame() lineLayout = QtGui.QHBoxLayout(lineWidget) lineWidget.setLayout(lineLayout) lineLayout.addWidget(widget1) lineLayout.addWidget(widget2, stretch=1) self.statusesLayout.addWidget(lineWidget) def addStretch(self): self.statusesLayout.addStretch() app = QtGui.QApplication(sys.argv) main = QtGui.QWidget() main.resize(640, 480) layout = QtGui.QHBoxLayout(main) main.show() testWidget = QtGui.QHBoxLayout() layout.addWidget(statusUI()) sys.exit(app.exec_())
Output:
Please for a next time remove unnecessary imports and also note that
from pyqtgraph import QtCore, QtGui
andfrom PyQt5 import QtCore, QtGui
may conflict causing silent bugs. -
Hi,
Please reduce your script to a minimal reproducible case.
One main thing is that your statusUI widget is given way to much information about its parent and layout. That's not its role to insert itself in some layout. This is something for the widget creating a statusUI to handle.
-
@Hexrin The problem is mainly caused by the line
lineLayout.setAlignment(Qt.AlignLeft)
that you probably used so that the first column occupies the minimum space but also makes the second column compress. Instead I use stretch factors.import sys from pyqtgraph import QtCore, QtGui class statusUI(QtGui.QPushButton): def __init__(self): QtGui.QPushButton.__init__(self) self.setMinimumHeight(200) statusUILayout = QtGui.QHBoxLayout(self) self.scrollArea = dataScrollArea() statusUILayout.addWidget(self.scrollArea) self.setUpMagUI() def setUpMagUI(self): text = str( [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0] ).translate({None: "'"}) self.magneticFieldCovNameLabel = QtGui.QLabel("Magnetic Field Covariance: ") self.magneticFieldCovNumberLabel = QtGui.QLabel(text) self.magneticFieldCovNameLabel.setAlignment(QtCore.Qt.AlignLeft) self.magneticFieldCovNumberLabel.setAlignment(QtCore.Qt.AlignLeft) self.scrollArea.addLine( self.magneticFieldCovNameLabel, self.magneticFieldCovNumberLabel ) self.scrollArea.addStretch() class dataScrollArea(QtGui.QScrollArea): def __init__(self): QtGui.QScrollArea.__init__(self) self.setWidgetResizable(True) content = QtGui.QWidget() self.setWidget(content) self.statusesLayout = QtGui.QVBoxLayout(content) def addLine(self, widget1, widget2): widget2.setWordWrap(True) lineWidget = QtGui.QFrame() lineLayout = QtGui.QHBoxLayout(lineWidget) lineWidget.setLayout(lineLayout) lineLayout.addWidget(widget1) lineLayout.addWidget(widget2, stretch=1) self.statusesLayout.addWidget(lineWidget) def addStretch(self): self.statusesLayout.addStretch() app = QtGui.QApplication(sys.argv) main = QtGui.QWidget() main.resize(640, 480) layout = QtGui.QHBoxLayout(main) main.show() testWidget = QtGui.QHBoxLayout() layout.addWidget(statusUI()) sys.exit(app.exec_())
Output:
Please for a next time remove unnecessary imports and also note that
from pyqtgraph import QtCore, QtGui
andfrom PyQt5 import QtCore, QtGui
may conflict causing silent bugs.