QScrollArea中的PyQt QScrollArea

时间:2015-04-11 22:45:21

标签: python pyqt pyqt4

我正在尝试使用多个水平子QScrollAreas文本和一个垂直容器QScrollArea。我们的想法是水平子QScrollAreas中的文本区域将始终具有相同的垂直高度,我希望有一个垂直QScrollArea来控制其中的数据。

下面的代码显示水平子QScrollAreas工作,但垂直QScrollArea没有检测到在其内部的窗口小部件内编辑的行不适合垂直。如果我改变

scroll.setWidgetResizable(True) 

对于垂直QScrollArea为False,垂直QScrollArea检测到里面的小部件不适合但我希望能够上下滚动所有lineEdits而不是父小部件。此外,我希望所有滚动条始终可见。这可能吗?

import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *

class Widget(QWidget):
    def __init__(self, parent= None):
        super(Widget, self).__init__()
        self.setGeometry(100, 100, 400, 400)
        baseWidget = QWidget()
        hBox = QHBoxLayout()
        hBox.addWidget(self.getWidget())
        hBox.addWidget(self.getWidget())
        baseWidget.setLayout(hBox)
        scroll = QScrollArea()
        scroll.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOn)
        scroll.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
        #when set to False all scrolls are not visible and can only scroll parent widget not the data areas
        scroll.setWidgetResizable(True)
        scroll.setWidget(baseWidget)
        vBox = QHBoxLayout()
        vBox.addWidget(scroll)
        self.setLayout(vBox)

    def getWidget(self):      
        widget = QWidget()
        layout = QVBoxLayout()
        for i in range(20):
            lineEdit = QLineEdit("row: "+str(i)+" data: "+str(list(range(10))))
            lineEdit.setMinimumWidth(250)
            layout.addWidget(lineEdit)
        widget.setLayout(layout)
        scroll = QScrollArea()
        scroll.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
        scroll.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOn)
        scroll.setWidgetResizable(False)
        scroll.setWidget(widget)
        return scroll

if __name__ == '__main__':
    app = QApplication(sys.argv)
    dialog = Widget()
    dialog.show()

1 个答案:

答案 0 :(得分:0)

答案可以在这里找到:

PyQt4 : is there any signal related to scrollbar?

只需要同步垂直滚动条并隐藏除一个以外的所有内容:

import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *

class Widget(QWidget):

    def __init__(self, parent= None):
        super(Widget, self).__init__()
        self.setGeometry(100, 100, 200, 200)
        baseWidget = QWidget()
        hBox = QHBoxLayout()
        lscrollArea = self.getWidget(False)
        rScrollArea = self.getWidget(True)
        rScrollArea.verticalScrollBar().valueChanged.connect(
        lscrollArea.verticalScrollBar().setValue)
        hBox.addWidget(lscrollArea)
        hBox.addWidget(rScrollArea)
        baseWidget.setLayout(hBox)
        vBox = QHBoxLayout()
        vBox.addWidget(baseWidget)
        self.setLayout(vBox)

    def getWidget(self, vScrollOn):      
        widget = QWidget()
        layout = QVBoxLayout()
        for i in range(20):
            lineEdit = QLineEdit("row: "+str(i)+" data: "+str(list(range(10))))
            lineEdit.setMinimumWidth(250)
            layout.addWidget(lineEdit)
        widget.setLayout(layout)
        scroll = QScrollArea()
        if vScrollOn:
            scroll.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOn)
        else:
            scroll.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
        scroll.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOn)
        scroll.setWidgetResizable(False)
        scroll.setWidget(widget)
        return scroll

if __name__ == '__main__':
    app = QApplication(sys.argv)
    dialog = Widget()
    dialog.show()
    app.exec_()