先上效果图,

QMainWindow透明背景

实现方法就是设置WA_TranslucentBackground属性,并禁止窗口自动填充背景。

#include <QApplication>
#include <QMainWindow>
#include <QPainter>

class CMainWindow : public QMainWindow
{
public:
    CMainWindow(QWidget* parent = 0) : QMainWindow(parent)
    {
        setAutoFillBackground(false);
//        setWindowFlags(Qt::FramelessWindowHint);
        setAttribute(Qt::WA_TranslucentBackground, true);
    }

protected:
    void paintEvent(QPaintEvent*)
    {
        QPainter pt(this);
        QColor c(Qt::gray);
        c.setAlpha(100);
        pt.fillRect(rect(), c);
    }
};


int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    CMainWindow w;
    w.show();
    return a.exec();
}

 

 

需要注意不能使用QtCreator创建的UI文件来创建QMainWindow,会有一个黑色的背景无法祛除。

 

相关文章:

  • 2021-09-16
  • 2021-11-18
  • 2022-02-10
  • 2021-11-06
  • 2021-05-27
  • 2022-01-19
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-12-20
  • 2021-08-28
  • 2021-12-04
  • 2021-11-17
相关资源
相似解决方案