2

In the following code, I would like to use thicker rule separator between the column 2 and 3 for example. How can I achieve this ?

#! /usr/bin/env python2.7
# -*- coding: utf-8 -*-

import sys

from PySide import QtCore, QtGui

class MainWindow(QtGui.QWidget):
    def __init__(
        self,
        parent = None
    ):
        super(MainWindow, self).__init__(parent)

# General grid
        self.table = QtGui.QTableWidget(self)
        self.nbrow, self.nbcol = 9, 9
        self.table.setRowCount(self.nbrow)
        self.table.setColumnCount(self.nbcol)

# Each cell has dimension 50 pixels x 50 pixels
        for row in range(0, self.nbrow):
            self.table.setRowHeight(row, 50)

            for col in range(0, self.nbcol):
                self.table.setColumnWidth(col, 50)

# Each cell contains one single QTableWidgetItem
        for row in range(0, self.nbrow):
            for col in range(0, self.nbcol):
                item = QtGui.QTableWidgetItem()
                item.setTextAlignment(
                    QtCore.Qt.AlignHCenter | QtCore.Qt.AlignVCenter
                )

                self.table.setItem(row, col, item)

# Header formatting
        font = QtGui.QFont()
        font.setFamily(u"DejaVu Sans")
        font.setPointSize(12)
        self.table.horizontalHeader().setFont(font)
        self.table.verticalHeader().setFont(font)

# Font used
        font = QtGui.QFont()
        font.setFamily(u"DejaVu Sans")
        font.setPointSize(20)
        self.table.setFont(font)

# Global Size
        self.resize(60*9, 60*9 + 20)

# Layout of the table
        layout = QtGui.QGridLayout()
        layout.addWidget(self.table, 0, 0)
        self.setLayout(layout)

# Set the focus in the first cell
        self.table.setFocus()
        self.table.setCurrentCell(0, 0)


if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    fen = MainWindow()
    fen.show()
    sys.exit(app.exec_())
eyllanesc
  • 235,170
  • 19
  • 170
  • 241

2 Answers2

0

You can use stylesheets to change the style of the cell borders. See examples:

Community
  • 1
  • 1
Luke
  • 11,374
  • 2
  • 48
  • 61
  • How do you target that at particular columns? What does it look like in PyQt instead of pure Qt? – EL_DON May 05 '20 at 14:55
0

This is not a complete or ideal solution, but it's somewhat relevant and might be helpful to making progress. I found an example of changing a whole cell's border in Qt and translated it to PyQt.

First, create a delegate and override its paint method to do the customization you want.

class TableBorderDelegate(QtGui.QItemDelegate):
    """Delegate for customizing cell borders"""

    def paint(self, painter, option, index):
        """Overrides paint to put a red border around cells in column 3"""
        if index.column() == 3:  # index.row() is also a thing
            painter.setPen(QtGui.QColor(255, 0, 0))
            painter.drawRect(option.rect)
        QtGui.QItemDelegate.paint(self, painter, option, index)
    # End of class TableBorderDelegate

And then in your table

delegate = TableBorderDelegate()
self.table.setItemDelegate(delegate) 

Where self.table is an instance of QtGui.QTableWidget()

The Qt solution came from https://stackoverflow.com/a/7264248/6605826 . I found a pyqt example of delegate usage in https://github.com/baoboa/pyqt5/blob/master/examples/itemviews/spinboxdelegate.py An unrelated pyqt delegate

EL_DON
  • 1,416
  • 1
  • 19
  • 34