【问题标题】:PyQT - Group cells in QTableWidgetPyQT - QTableWidget 中的分组单元格
【发布时间】:2019-02-01 19:03:54
【问题描述】:

我正在寻找如何在 QTableWidget 中创建像 HTML 表格这样的标题,类似这样的东西:

我可以在 QTextEdit(单元格中包含图像的 HTML 表格)中执行此操作,但我需要这样的自定义单元格:

所以 QTextEdit 不适合这个。有没有可能,如果有,我该如何创建它?也许不是 QTableWidget,我只需要带有自定义单元格和自定义标题的可编辑表格。

我看到的只是插入一个大标题图像并根据该图像设置所有列,但是如何创建一个包含一个单元格的行?

【问题讨论】:

    标签: python-2.7 pyqt


    【解决方案1】:

    QTableView.setSpan(row, column, rowSpan, columnSpan)

    参数:
    行 – PySide.QtCore.int
    专栏 – PySide.QtCore.int
    rowSpan – PySide.QtCore.int
    columnSpan – PySide.QtCore.int

    将 (row , column ) 处的表格元素的跨度设置为 (rowSpanCount , columnSpanCount ) 指定的行数和列数。

    import sys
    from PyQt5.QtWidgets import (QWidget, QTableWidget, QHBoxLayout, QApplication, QTableWidgetItem )
    from PyQt5.QtGui     import QBrush, QColor #,  QFont 
    
    class Table(QWidget):
        def __init__(self):
            super().__init__()
            self.initUI()
    
        def initUI(self):
            self.setWindowTitle("QTableWidget - Example of a cell merge")
            self.resize(660,300 );
            conLayout = QHBoxLayout()
    
            tableWidget = QTableWidget()
            tableWidget.setRowCount(7)
            tableWidget.setColumnCount(6)
            conLayout.addWidget(tableWidget)
    
            # Hide headers
            tableWidget.horizontalHeader().setVisible(False)
            tableWidget.verticalHeader().setVisible(False)
    
            #tableWidget.setHorizontalHeaderLabels(['Column1','Column1','Column1'])  
    
            # Sets the span of the table element at (row , column ) to the number of rows 
            # and columns specified by (rowSpanCount , columnSpanCount ).
            tableWidget.setSpan(0, 0, 1, 6) 
            newItem = QTableWidgetItem("tableWidget.setSpan(0, 0, 1, 6)")  
            tableWidget.setItem(0, 0, newItem) 
    
            tableWidget.setSpan(3, 0, 3, 1)   
            newItem = QTableWidgetItem("tableWidget.setSpan(3, 0, 3, 1)")  
            tableWidget.setItem(3, 0, newItem)  
    
            newItem = QTableWidgetItem("Hello")  
            newItem.setForeground(QBrush(QColor(0, 255, 0)))
            tableWidget.setItem(3, 1, newItem)  
    
            newItem = QTableWidgetItem("pythoff") 
            newItem.setForeground(QBrush(QColor(255, 0, 0)))        
            tableWidget.setItem(3, 2, newItem)   
    
            self.setLayout(conLayout)
    
    if __name__ == '__main__':
        app = QApplication(sys.argv)
        example = Table()  
        example.show()   
        sys.exit(app.exec_())
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-02-09
      • 2011-01-04
      • 2012-04-05
      • 2023-04-02
      • 2017-06-17
      • 1970-01-01
      • 2013-09-24
      • 1970-01-01
      相关资源
      最近更新 更多