Issue
I have a QScrollArea and I scroll down automatically, but it does not work completely - it does scroll only until half. What might be wrong with my code? I use the vertical ScrollBar. Maybe there is a problem with setSizePolicy?
code:
import sys
from PySide import QtGui, QtCore
class Window(QtGui.QMainWindow):
def __init__(self):
super(Window, self).__init__()
self.setStyleSheet("QMainWindow {background: #010206;}");
self.inicar()
def inicar(self):
groupBox = QtGui.QGroupBox('', self)
groupBox.setGeometry(100, 100, 500, 340)
groupBox.setStyleSheet('QGroupBox {border: 2px solid #101010; border-radius: 3px;}');
label = QtGui.QLabel(self)
label.setText('prueba asda asd asdad asdasdadadasd prueba asda asd asdad asdasdadadasd')
label.setWordWrap(True)
label.setStyleSheet('QLabel {color: #6E6E6E;float: left;border: 2px solid #071918; border-radius: 3px; padding-top: 2px; padding-bottom: 2px; padding-left: 3px; padding-right: 2px;}');
label2 = QtGui.QLabel(self)
label2.setText('hola que tal asdasdasd asd asdasdad asdas')
label2.setWordWrap(True)
label2.setStyleSheet('QLabel {color: #2F6120;float: right; border: 2px solid #071918; border-radius: 3px; padding-top: 2px; padding-bottom: 2px; padding-left: 3px; padding-right: 2px;}');
label3 = QtGui.QLabel(self)
label3.setText('prueba')
label3.setWordWrap(True)
label3.setStyleSheet('QLabel {color: #6E6E6E;float: left;border: 2px solid #071918; border-radius: 3px; padding-top: 2px; padding-bottom: 2px; padding-left: 3px; padding-right: 2px;}');
label4 = QtGui.QLabel(self)
label4.setText('hola que tal asdasdasd asd asdasdad asdas')
label4.setWordWrap(True)
label4.setStyleSheet('QLabel {color: #2F6120;float: right; border: 2px solid #071918; border-radius: 3px; padding-top: 2px; padding-bottom: 2px; padding-left: 3px; padding-right: 2px;}');
label5 = QtGui.QLabel(self)
label5.setText('hola que tal asdasdasd asd asdasdad asdas')
label5.setWordWrap(True)
label5.setStyleSheet('QLabel {color: #2F6120;float: right; border: 2px solid #071918; border-radius: 3px; padding-top: 2px; padding-bottom: 2px; padding-left: 3px; padding-right: 2px;}');
label6 = QtGui.QLabel(self)
label6.setText('hola que tal asdasdasd asd asdasdad asdas')
label6.setWordWrap(True)
label6.setStyleSheet('QLabel {color: #2F6120;float: right; border: 2px solid #071918; border-radius: 3px; padding-top: 2px; padding-bottom: 2px; padding-left: 3px; padding-right: 2px;}');
label7 = QtGui.QLabel(self)
label7.setText('prueba asda asd asdad asdasdadadasd')
label7.setWordWrap(True)
label7.setStyleSheet('QLabel {color: #6E6E6E;float: left;border: 2px solid #071918; border-radius: 3px; padding-top: 2px; padding-bottom: 2px; padding-left: 3px; padding-right: 2px;}');
label8 = QtGui.QLabel(self)
label8.setText('prueba asda asd asdad asdasdadadasd prueba asda asd asdad asdasdadadasd')
label8.setWordWrap(True)
label8.setStyleSheet('QLabel {color: #6E6E6E;float: left;border: 2px solid #071918; border-radius: 3px; padding-top: 2px; padding-bottom: 2px; padding-left: 3px; padding-right: 2px;}');
label9 = QtGui.QLabel(self)
label9.setText('hola que tal asdasdasd asd asdasdad asdas')
label9.setWordWrap(True)
label9.setStyleSheet('QLabel {color: #2F6120;float: right; border: 2px solid #071918; border-radius: 3px; padding-top: 2px; padding-bottom: 2px; padding-left: 3px; padding-right: 2px;}');
label10 = QtGui.QLabel(self)
label10.setText('prueba asda asd asdad asdasdadadasd')
label10.setWordWrap(True)
label10.setStyleSheet('QLabel {color: #6E6E6E;border: 2px solid #071918; border-radius: 3px; padding-top: 2px; padding-bottom: 2px; padding-left: 3px; padding-right: 2px;}');
label11 = QtGui.QLabel(self)
label11.setText('hola que tal asdasdasd asd asdasdad asdas')
label11.setWordWrap(True)
label11.setStyleSheet('QLabel {color: #2F6120; border: 2px solid #071918; border-radius: 3px; padding-top: 2px; padding-bottom: 2px; padding-left: 3px; padding-right: 2px;}');
label12 = QtGui.QLabel(self)
label12.setText('hola que tal asdasdasd asd asdasdad asdas')
label12.setWordWrap(True)
label12.setStyleSheet('QLabel {color: #2F6120;float: right; border: 2px solid #071918; border-radius: 3px; padding-top: 2px; padding-bottom: 2px; padding-left: 3px; padding-right: 2px;}');
label13 = QtGui.QLabel(self)
label13.setText('hola que tal asdasdasd asd asdasdad asdas')
label13.setWordWrap(True)
label13.setStyleSheet('QLabel {color: #2F6120;float: right; border: 2px solid #071918; border-radius: 3px; padding-top: 2px; padding-bottom: 2px; padding-left: 3px; padding-right: 2px;}');
label14 = QtGui.QLabel(self)
label14.setText('prueba asda asd asdad asdasdadadasd')
label14.setWordWrap(True)
label14.setStyleSheet('QLabel {color: #6E6E6E;float: left;border: 2px solid #071918; border-radius: 3px; padding-top: 2px; padding-bottom: 2px; padding-left: 3px; padding-right: 2px;}');
vbox = QtGui.QVBoxLayout()
vbox.addWidget(label)
vbox.addWidget(label2)
vbox.addWidget(label3)
vbox.addWidget(label4)
vbox.addWidget(label5)
vbox.addWidget(label6)
vbox.addWidget(label7)
vbox.addWidget(label8)
vbox.addWidget(label9)
vbox.addWidget(label10)
vbox.addWidget(label11)
vbox.addWidget(label12)
vbox.addWidget(label13)
vbox.addWidget(label14)
vbox.addStretch(1)
groupBox.setLayout(vbox)
scroll = QtGui.QScrollArea(self)
scroll.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOn)
scroll.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
scroll.setWidgetResizable(True)
scroll.setGeometry(100, 80, 500, 340)
scroll.setStyleSheet('QScrollArea {background: #010206; border: 2px solid #101010; border-radius: 3px;}');
scroll.setWidget(groupBox)
label.setSizePolicy(QtGui.QSizePolicy.Fixed, QtGui.QSizePolicy.Fixed)
label2.setSizePolicy(QtGui.QSizePolicy.Fixed, QtGui.QSizePolicy.Fixed)
label3.setSizePolicy(QtGui.QSizePolicy.Fixed, QtGui.QSizePolicy.Fixed)
label4.setSizePolicy(QtGui.QSizePolicy.Fixed, QtGui.QSizePolicy.Fixed)
label5.setSizePolicy(QtGui.QSizePolicy.Fixed, QtGui.QSizePolicy.Fixed)
label6.setSizePolicy(QtGui.QSizePolicy.Fixed, QtGui.QSizePolicy.Fixed)
label7.setSizePolicy(QtGui.QSizePolicy.Fixed, QtGui.QSizePolicy.Fixed)
label8.setSizePolicy(QtGui.QSizePolicy.Fixed, QtGui.QSizePolicy.Fixed)
label9.setSizePolicy(QtGui.QSizePolicy.Fixed, QtGui.QSizePolicy.Fixed)
label10.setSizePolicy(QtGui.QSizePolicy.Fixed, QtGui.QSizePolicy.Fixed)
label11.setSizePolicy(QtGui.QSizePolicy.Fixed, QtGui.QSizePolicy.Fixed)
label12.setSizePolicy(QtGui.QSizePolicy.Fixed, QtGui.QSizePolicy.Fixed)
label13.setSizePolicy(QtGui.QSizePolicy.Fixed, QtGui.QSizePolicy.Fixed)
label14.setSizePolicy(QtGui.QSizePolicy.Fixed, QtGui.QSizePolicy.Fixed)
maximumY = scroll.verticalScrollBar().maximum()
scroll.verticalScrollBar().setSliderPosition(maximumY)
self.setGeometry(350, 145, 730, 500)
self.setFixedSize(730, 500)
self.setWindowTitle('Mensajes')
self.show()
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
w = Window()
sys.exit(app.exec_())
Solution
The geometry of widget are not initiated. So you have to wait it after initiate successful, You can set set slider position. In your code is after call method self.show()
;
.
.
label14.setSizePolicy(QtGui.QSizePolicy.Fixed, QtGui.QSizePolicy.Fixed)
self.setGeometry(350, 145, 730, 500)
self.setFixedSize(730, 500)
self.setWindowTitle('Mensajes')
self.show()
maximumY = scroll.verticalScrollBar().maximum()
scroll.verticalScrollBar().setSliderPosition(maximumY)
In the other-hand, You can create your custom QtGui.QScrollArea
to auto scroll down when size of QtGui.QScrollArea
has been changed. Implement method QAbstractScrollArea.resizeEvent (self, QResizeEvent);
class QScrollEndArea (QtGui.QScrollArea):
def resizeEvent (self, eventQResizeEvent):
QtGui.QScrollArea.resizeEvent(self, eventQResizeEvent)
self.verticalScrollBar().setSliderPosition(self.verticalScrollBar().maximum())
And replace QtGui.QScrollArea
to your custom QScrollEndArea
in your code, benefit is your not to handle set position size in your main widget, it handle itself;
scroll = QScrollEndArea(self)
Answered By - Kitsune Meyoko
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.