Qt:设置QWidget背景色

    技术2022-07-11  146

    qt设置QWidget背景颜色的方法测试了三种,推荐第一种。

    一、使用QPalette

    Qpalette palette; palette.setColor(QPalette::Background, Qt::yellow); this->setAutoFillBackground(true); this->setPalette(palette);

    二、使用StyleSheet

    //测试如果子控件不能完全填充时只能填充子控件的背景色 this->setStyleSheet("background-color:black;");

    三、使用paintEvent

    void Widget::paintEvent(QPaintEvent *event) { Q_UNUSED(event); QPainter p(this); p.setPen(Qt::NoPen); p.setBrush(Qt::black); p.drawRect(rect()); }
    Processed: 0.013, SQL: 9