QTableWidget等控件去除选中虚框

重载QProxyStyle的paint函数,过滤焦点

// 解决QTableWidget选中单元格有虚框的问题
class MyProxyStyle : public QProxyStyle
{
public:
    virtual void drawPrimitive(PrimitiveElement element, const QStyleOption * option,
        QPainter * painter, const QWidget * widget = 0) const
    {
        if (PE_FrameFocusRect == element)
        {
            //这里不做任何操作,Qt默认是绘制矩形虚线框
        }
        else
        {
            QProxyStyle::drawPrimitive(element, option, painter, widget);
        }
    }
};

ui->tableWidget->setStyle(new MyProxyStyle);
ui->tableWidget->setStyleSheet("QTableWidget{outline:none;}");

 

posted @ 2023-06-21 10:57  徐沛东  阅读(414)  评论(0编辑  收藏  举报