PyQT – Group cells in QTableWidget

Question:

I’m searching how to create headers like HTML-tables in QTableWidget, something like that:enter image description here

I can do it in QTextEdit (HTML-table with images in cells), but I need custom cells like that:

enter image description here

so QTextEdit is not suitable for this. Is it possible at all and if is, how can I create it? Maybe not a QTableWidget, I just need editable table with custom cells and custom header.

All I see is just insert one big header image and set all columns according to that image, but how to create one row with one cell in it?

enter image description here

Asked By: neurocker

||

Answers:

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

Parameters:
row – PySide.QtCore.int
column – PySide.QtCore.int
rowSpan – PySide.QtCore.int
columnSpan – PySide.QtCore.int

Sets the span of the table element at (row , column ) to the number of rows and columns specified by (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_())

enter image description here

Answered By: S. Nick
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.