垂直滚动条不会完全向下

时间:2014-09-15 21:50:32

标签: python qt scroll pyside

我有一个QScrollArea并且我自动向下滚动,但它不能完全运行 - 它只滚动到一半。我的代码可能有什么问题?我使用垂直ScrollBar。也许setSizePolicy存在问题?

代码:

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

1 个答案:

答案 0 :(得分:0)

未启动窗口小部件的几何体。因此,您必须在启动成功后等待它,您可以设置设置滑块位置。在您的代码中是调用方法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)

另一方面,您可以创建自定义QtGui.QScrollArea,以便在QtGui.QScrollArea的尺寸发生变化时自动向下滚动。实施方法QAbstractScrollArea.resizeEvent (self, QResizeEvent);

class QScrollEndArea (QtGui.QScrollArea):
    def resizeEvent (self, eventQResizeEvent):
        QtGui.QScrollArea.resizeEvent(self, eventQResizeEvent)
        self.verticalScrollBar().setSliderPosition(self.verticalScrollBar().maximum())

在您的代码中将QtGui.QScrollArea替换为您的自定义QScrollEndArea,这样做的好处就是您无法处理主窗口小部件中的设置位置大小,它会自行处理;

scroll = QScrollEndArea(self)