如何将固定标头添加到QScrollArea?

我目前有一个QScrollArea定义:

self.results_grid_scrollarea = QScrollArea()
self.results_grid_widget = QWidget()
self.results_grid_layout = QGridLayout()
self.results_grid_layout.setSizeConstraint(QLayout.SetMinAndMaxSize)
self.results_grid_widget.setLayout(self.results_grid_layout)
self.results_grid_scrollarea.setWidgetResizable(True)
self.results_grid_scrollarea.setWidget(self.results_grid_widget)
self.results_grid_scrollarea.setViewportMargins(0,20,0,0)

它非常愉快地嵌套在其他布局/小部件中,按预期调整大小等 .

为了提供网格列的 Headers ,我正在使用另一个位于滚动区域正上方的QGridLayout - 这可以工作......但看起来有点奇怪,即使在适当设置样式时,尤其是当按需(垂直)滚动条出现或消失时根据需要, Headers 不再与网格列正确对齐 . 这是我认识的美学事物......但我有点挑剔;)

其他小部件以其他方式以编程方式添加/删除到 self.results_grid_layout . 我最近刚刚添加的最后一行,因为我认为使用创建的边距区域很容易,docs用于setViewportMargins状态:

设置滚动区域周围的边距 . 这对于具有“锁定”行和列的电子表格等应用程序非常有用 . 边际空间是空白的;将小部件放在未使用的区域 .

但我不能为我的生活找到如何实现这一目标,而且我的GoogleFu今天已经抛弃了我,或者关于如何实现这一目标的信息/示例很少 .

我的头告诉我,我可以只分配一个小部件,由布局控制(包含任意数量的其他小部件)到滚动区 - 正如我所做的那样 . 如果我添加一个QHeaderview作为例如gridlayout的第0行,它将只显示在视口的边缘下方并滚动其余的布局?或者我错过了什么,只是看不到树木?

我只是学习Python / Qt,所以任何帮助,指针和/或示例(最好使用Python但不是必需的)将不胜感激!


编辑:按照目前给出的建议(我认为),我提出了以下小测试程序来尝试:

import sys
from PySide.QtCore import *
from PySide.QtGui import *

class MainWindow(QMainWindow):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        self.setMinimumSize(640, 480)

        self.container_widget = QWidget()
        self.container_layout = QVBoxLayout()
        self.container_widget.setLayout(self.container_layout)
        self.setCentralWidget(self.container_widget)

        self.info_label = QLabel(
            "Here you can see the problem.... I hope!\n"
            "Once the window is resized everything behaves itself.")
        self.info_label.setWordWrap(True)

        self.headings_widget = QWidget()
        self.headings_layout = QGridLayout()
        self.headings_widget.setLayout(self.headings_layout)
        self.headings_layout.setContentsMargins(1,1,0,0)

        self.heading_label1 = QLabel("Column 1")
        self.heading_label1.setContentsMargins(16,0,0,0)
        self.heading_label2 = QLabel("Col 2")
        self.heading_label2.setAlignment(Qt.AlignCenter)
        self.heading_label2.setMaximumWidth(65)
        self.heading_label3 = QLabel("Column 3")
        self.heading_label3.setContentsMargins(8,0,0,0)
        self.headings_layout.addWidget(self.heading_label1,0,0)
        self.headings_layout.addWidget(self.heading_label2,0,1)
        self.headings_layout.addWidget(self.heading_label3,0,2)
        self.headings_widget.setStyleSheet(
            "background: green; border-bottom: 1px solid black;" )

        self.grid_scrollarea = QScrollArea()
        self.grid_widget = QWidget()
        self.grid_layout = QGridLayout()
        self.grid_layout.setSizeConstraint(QLayout.SetMinAndMaxSize)
        self.grid_widget.setLayout(self.grid_layout)
        self.grid_scrollarea.setWidgetResizable(True)
        self.grid_scrollarea.setWidget(self.grid_widget)
        self.grid_scrollarea.setViewportMargins(0,30,0,0)
        self.headings_widget.setParent(self.grid_scrollarea)
        ### Add some linedits to the scrollarea just to test
        rows_to_add = 10
        ## Setting the above to a value greater than will fit in the initial
        ## window will cause the lineedits added below to display correctly,
        ## however - using the 10 above, the lineedits do not expand to fill
        ## the scrollarea's width until you resize the window horizontally.
        ## What's the best way to fix this odd initial behaviour?
        for i in range(rows_to_add):
            col1 = QLineEdit()
            col2 = QLineEdit()
            col2.setMaximumWidth(65)
            col3 = QLineEdit()
            row = self.grid_layout.rowCount()
            self.grid_layout.addWidget(col1,row,0)
            self.grid_layout.addWidget(col2,row,1)
            self.grid_layout.addWidget(col3,row,2)
        ### Define Results group to hold the above sections
        self.test_group = QGroupBox("Results")
        self.test_layout = QVBoxLayout()
        self.test_group.setLayout(self.test_layout)
        self.test_layout.addWidget(self.info_label)
        self.test_layout.addWidget(self.grid_scrollarea)
        ### Add everything to the main layout
        self.container_layout.addWidget(self.test_group)


    def resizeEvent(self, event):
        scrollarea_vpsize = self.grid_scrollarea.viewport().size()
        scrollarea_visible_size = self.grid_scrollarea.rect()
        desired_width = scrollarea_vpsize.width()
        desired_height = scrollarea_visible_size.height()
        desired_height =  desired_height - scrollarea_vpsize.height()
        new_geom = QRect(0,0,desired_width+1,desired_height-1)
        self.headings_widget.setGeometry(new_geom)


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

if __name__ == '__main__':
   main()

这些方面的东西是你指向的方法吗?一切都按预期工作,正如我所追求的那样,除了用户调整窗口大小之前的一些奇怪的初始行为,一旦调整大小,所有内容都排成一行并且很好 . 我可能会再次过度思考或至少忽视某些事情......任何想法?

回答(2)

2 years ago

你可能会略微过度思考问题 .

您需要做的就是使用scrollarea视口的几何和当前边距来计算要放在边距中的任何小部件的几何 .

这些小部件的几何形状也需要在scrollarea的 resizeEvent 中更新 .

如果你看一下 QTableView 的源代码,我想你会发现它使用这种方法来管理它的头部视图(或类似的东西) .

EDIT

为了处理测试用例中的小调整大小问题,我建议您阅读 QRect 文档中的Coordinates部分(特别是第三段以后) .

通过重写您的测试用例,我能够更准确地调整大小:

import sys
from PySide.QtCore import *
from PySide.QtGui import *

class MainWindow(QMainWindow):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        self.setMinimumSize(640, 480)
        self.container_widget = QWidget()
        self.container_layout = QVBoxLayout()
        self.container_widget.setLayout(self.container_layout)
        self.setCentralWidget(self.container_widget)
        self.grid_scrollarea = ScrollArea(self)
        self.test_group = QGroupBox("Results")
        self.test_layout = QVBoxLayout()
        self.test_group.setLayout(self.test_layout)
        self.test_layout.addWidget(self.grid_scrollarea)
        self.container_layout.addWidget(self.test_group)

class ScrollArea(QScrollArea):
    def __init__(self, parent=None):
        QScrollArea.__init__(self, parent)
        self.grid_widget = QWidget()
        self.grid_layout = QGridLayout()
        self.grid_widget.setLayout(self.grid_layout)
        self.setWidgetResizable(True)
        self.setWidget(self.grid_widget)
        # save the margin values
        self.margins = QMargins(0, 30, 0, 0)
        self.setViewportMargins(self.margins)
        self.headings_widget = QWidget(self)
        self.headings_layout = QGridLayout()
        self.headings_widget.setLayout(self.headings_layout)
        self.headings_layout.setContentsMargins(1,1,0,0)
        self.heading_label1 = QLabel("Column 1")
        self.heading_label1.setContentsMargins(16,0,0,0)
        self.heading_label2 = QLabel("Col 2")
        self.heading_label2.setAlignment(Qt.AlignCenter)
        self.heading_label2.setMaximumWidth(65)
        self.heading_label3 = QLabel("Column 3")
        self.heading_label3.setContentsMargins(8,0,0,0)
        self.headings_layout.addWidget(self.heading_label1,0,0)
        self.headings_layout.addWidget(self.heading_label2,0,1)
        self.headings_layout.addWidget(self.heading_label3,0,2)
        self.headings_widget.setStyleSheet(
            "background: green; border-bottom: 1px solid black;" )
        rows_to_add = 10
        for i in range(rows_to_add):
            col1 = QLineEdit()
            col2 = QLineEdit()
            col2.setMaximumWidth(65)
            col3 = QLineEdit()
            row = self.grid_layout.rowCount()
            self.grid_layout.addWidget(col1,row,0)
            self.grid_layout.addWidget(col2,row,1)
            self.grid_layout.addWidget(col3,row,2)

    def resizeEvent(self, event):
        rect = self.viewport().geometry()
        self.headings_widget.setGeometry(
            rect.x(), rect.y() - self.margins.top(),
            rect.width() - 1, self.margins.top())
        QScrollArea.resizeEvent(self, event)

if __name__ == '__main__':

    app = QApplication(sys.argv)
    form = MainWindow()
    form.show()
    sys.exit(app.exec_())

2 years ago

我遇到了类似的问题并且有点不同地解决了它 . 而不是使用一个QScrollArea我使用两个并将下滚动区域的移动向前移动到顶部区域 . 下面的代码是做什么的

  • 它在QVBoxLayout中创建了两个QScrollArea小部件 .

  • 它禁用顶部QScrollArea滚动条的可见性并为其指定固定高度 .

  • 使用较低QScrollArea的水平滚动条的valueChanged信号,可以从较低的QScrollArea到顶部的水平滚动条值"forward",从而在窗口顶部生成一个固定的 Headers .

class MainWindow(QMainWindow):def init (self,parent = None):super(MainWindow,self) . init (父母)

widget = QWidget()
    self.setCentralWidget(widget)

    vLayout = QVBoxLayout()
    widget.setLayout(vLayout)

    # TOP
    scrollAreaTop = QScrollArea()
    scrollAreaTop.setWidgetResizable(True)
    scrollAreaTop.setFixedHeight(30)
    scrollAreaTop.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
    scrollAreaTop.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
    scrollAreaTop.setWidget(QLabel(" ".join([str(i) for i in range(100)])))

    # BOTTOM
    scrollAreaBottom = QScrollArea()
    scrollAreaBottom.setWidgetResizable(True)
    scrollAreaBottom.setWidget(QLabel("\n".join([" ".join([str(i) for i in range(100)]) for _ in range(10)])))
    scrollAreaBottom.horizontalScrollBar().valueChanged.connect(lambda value: scrollAreaTop.horizontalScrollBar().setValue(value))

    vLayout.addWidget(scrollAreaTop)
    vLayout.addWidget(scrollAreaBottom)

Window resulting from above code.