Hide the border of the selected cell in qtablewidget in pyqt?
Asked Answered
A

3

10

Is there a way i can hide the border of the selected cell(or make the border color as white)in a qtablewidget.. By default a border with dotted line is shown.. Can u help me...

Antependium answered 13/1, 2010 at 9:47 Comment(0)
O
16

It looks like this dotted border around selected cell you're trying to hide is a focus rectangle. Any given cell can have focus and not be selected at the same time and vice-versa. If you want this border to not get painted use an item delegate. There you can remove State_HasFocus style from the item's state before painting it. Pls, see an example below on how to do this, it's c++, let me know if you have troubles converting it to python

// custom item delegate class
class NoFocusDelegate : public QStyledItemDelegate
{
protected:
    void paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const;
};

void NoFocusDelegate::paint(QPainter* painter, const QStyleOptionViewItem & option, const QModelIndex &index) const
{
    QStyleOptionViewItem itemOption(option);
    if (itemOption.state & QStyle::State_HasFocus)
        itemOption.state = itemOption.state ^ QStyle::State_HasFocus;
    QStyledItemDelegate::paint(painter, itemOption, index);
}
...
// set the item delegate to your table widget
ui->tableView->setItemDelegate(new NoFocusDelegate());

hope this helps, regards

Offshore answered 14/1, 2010 at 2:53 Comment(1)
Many thanks for replying.. I've achieved the same with setFocusPolicy(QtCore.Qt.NoFocus) since i've used qtablewidget.. I'm not familiar with delegates though.. I can't upvote this answer.. It says the reputation points must be greater than 15..Antependium
T
17

I prefer to do:

ui->tableWidget->setFocusPolicy(Qt::NoFocus);

You can also change the focus policy using the design tab.

Trolly answered 4/11, 2010 at 11:26 Comment(1)
.. but you lose keyboard navigation feature by doing soVladamir
O
16

It looks like this dotted border around selected cell you're trying to hide is a focus rectangle. Any given cell can have focus and not be selected at the same time and vice-versa. If you want this border to not get painted use an item delegate. There you can remove State_HasFocus style from the item's state before painting it. Pls, see an example below on how to do this, it's c++, let me know if you have troubles converting it to python

// custom item delegate class
class NoFocusDelegate : public QStyledItemDelegate
{
protected:
    void paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const;
};

void NoFocusDelegate::paint(QPainter* painter, const QStyleOptionViewItem & option, const QModelIndex &index) const
{
    QStyleOptionViewItem itemOption(option);
    if (itemOption.state & QStyle::State_HasFocus)
        itemOption.state = itemOption.state ^ QStyle::State_HasFocus;
    QStyledItemDelegate::paint(painter, itemOption, index);
}
...
// set the item delegate to your table widget
ui->tableView->setItemDelegate(new NoFocusDelegate());

hope this helps, regards

Offshore answered 14/1, 2010 at 2:53 Comment(1)
Many thanks for replying.. I've achieved the same with setFocusPolicy(QtCore.Qt.NoFocus) since i've used qtablewidget.. I'm not familiar with delegates though.. I can't upvote this answer.. It says the reputation points must be greater than 15..Antependium
G
3

Qt::NoFocus will remove the selected state of rows in QTableWidget.

The Python3/PySide2 version to the accepted answer:

class NoFocusDelegate(QtWidgets.QStyledItemDelegate):
    def paint(self, painter: PySide2.QtGui.QPainter, option: PySide2.QtWidgets.QStyleOptionViewItem, index: PySide2.QtCore.QModelIndex) -> None:
        itemOption = QtWidgets.QStyleOptionViewItem(option)
        if option.state & QtWidgets.QStyle.State_HasFocus:
            itemOption.state = itemOption.state ^ QtWidgets.QStyle.State_HasFocus
        super().paint(painter, itemOption, index)

table.setItemDelegate(NoFocusDelegate())

Worked perfectly for me.

Gav answered 15/12, 2021 at 18:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.