17

I'm wondering if anyone knows of, perhaps a flag to disable the gray dotted border that appears when you single click on a QTableWidget's cell.

Thanks.

Jean-Luc
  • 3,563
  • 8
  • 40
  • 78

6 Answers6

17

C++: tableWidget->setFocusPolicy(Qt::NoFocus);

Python: tableWidget.setFocusPolicy(QtCore.Qt.NoFocus)

Be aware that you will lose the ability to process keyboard events, but mouse events will work fine.

Aleksandar
  • 3,541
  • 4
  • 34
  • 57
16

It seems like you want to remove the border when the cell gets the focus.

Try editing the Stylesheet as follows.

QTableWidget {
    outline: 0;
}

This worked for me perfectly.

Surajeet Bharati
  • 1,363
  • 1
  • 18
  • 36
8

The easiest way to do it for me without affecting widget's focus policy and using qss is to create the following custom delegate and install it for table:

*.h:

class FocusControlDelegate : public QStyledItemDelegate {
    public:
        FocusControlDelegate(QObject *parent = 0);
        virtual void initStyleOption(QStyleOptionViewItem *option, const QModelIndex &index) const;        
        void setFocusBorderEnabled(bool enabled);

    protected:
        bool f_focus_border_enabled;
    };

*.cpp:

FocusControlDelegate::FocusControlDelegate(QObject *parent) : QStyledItemDelegate(parent) {
    f_focus_border_enabled = false;
}

void FocusControlDelegate::setFocusBorderEnabled(bool enabled) {
    f_focus_border_enabled = enabled;
}

void FocusControlDelegate::initStyleOption(QStyleOptionViewItem *option, const QModelIndex &index) const {
    QStyledItemDelegate::initStyleOption(option, index);
    if(!f_focus_border_enabled && option->state & QStyle::State_HasFocus)
        option->state = option->state & ~QStyle::State_HasFocus;
}
Mikhail Zimka
  • 694
  • 1
  • 9
  • 20
8

That gray dotted border indicates that that widget has focus.

Setting the below at the widget level should do the trick.

setFocusPolicy( Qt::NoFocus )
Wes
  • 4,781
  • 7
  • 44
  • 53
5

qApp->setStyleSheet ( " QTableWidget::item:focus { border: 0px }" );

brus
  • 51
  • 1
  • 2
  • 1
    almost. but if we use alternate background color, it changes the selection background color to alternate background color. Add this fix `QTableWidget::item:focus { border: 0px; background-color: # }` – swdev May 23 '14 at 00:11
0

QPushButton { outline: none; }

phorgan1
  • 1,664
  • 18
  • 18