Set QTableWidget cell's borders to 0px

2019-04-28 20:39发布

问题:

How can I set the cell borders in a QTableWidget to 0px? Preferably I can choose which sides of a cell's border to set to 0, but I can live with setting them all to 0 as well.

EDIT: Either setting the border to 0px or setting the color to white would be good as well.

回答1:

You can disable all the borders with QTableWidget::setShowGrid, and re-enable some of them with a style sheet (for instance: "QTableView::item { border-left: 1px solid black; }")

The latter are cell interior borders, so they might not be as well aligned as the grid.

If you want to change the borders individually for each cell, you need to write a delegate (like in that answer).



回答2:

check if QTableWidget's setShowGrid would work for you, smth like this:

tableWidget->setShowGrid(false);

hope this helps, regards



回答3:

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;
}


标签: qt qt4