C++ 无框透明窗qt5

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18316710/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 21:53:36  来源:igfitidea点击:

Frameless and transparent window qt5

c++qt

提问by Caveman

I'm quite new to qt and c++ and I've encountered a problem that I can't seem to figure out. I'm wanting to open a frameless and transparent window when I click a button on the main ui. I've got the code working to open a new window when I press a button on the main ui but I can't seem to get the frameless and transparent part working.

我对 qt 和 c++ 很陌生,我遇到了一个我似乎无法弄清楚的问题。当我单击主 ui 上的按钮时,我想打开一个无框且透明的窗口。当我按下主 ui 上的按钮时,我的代码可以打开一个新窗口,但我似乎无法让无框和透明部分工作。

Here is the source codes for the small program that I wrote to learn this

下面是我写的小程序的源码来学习这个

main.cpp

主程序

#include "learnwindow.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    LearnWindow w;
    w.show();

    return a.exec();
}

Here is the LearnWindow.h

这是 LearnWindow.h

#ifndef LEARNWINDOW_H
#define LEARNWINDOW_H

#include <QMainWindow>
#include <transwindow.h>

namespace Ui {
class LearnWindow;
}

class LearnWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit LearnWindow(QWidget *parent = 0);
    ~LearnWindow();

private slots:
    void on_pushButton_clicked();

private:
    Ui::LearnWindow *ui;
    TransWindow *winTrans;

public slots:
    void openTrans();
};

#endif // LEARNWINDOW_H

Here is learnwindow.cpp

这是learnwindow.cpp

#include "learnwindow.h"
#include "ui_learnwindow.h"

LearnWindow::LearnWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::LearnWindow)
{
    ui->setupUi(this);
}

LearnWindow::~LearnWindow()
{
    delete ui;
}

void LearnWindow::openTrans()
{
    winTrans = new TransWindow (this);
    //winTrans->setWindowTitle("NewWin");
   // winTrans->setWindowFlags(Qt::FramelessWindowHint | Qt::X11BypassWindowManagerHint);
    //winTrans->setAttribute(Qt::WA_TranslucentBackground,true);
    //winTrans->setAutoFillBackground(false);
    //winTrans->setStyleSheet("background:transparent;");
    winTrans->show();
}

void LearnWindow::on_pushButton_clicked()
{
    openTrans();
}

Here is the transwindow.h

这是transwindow.h

#ifndef TRANSWINDOW_H
#define TRANSWINDOW_H

#include <QDialog>

namespace Ui {
class TransWindow;
}

class TransWindow : public QDialog
{
    Q_OBJECT

public:
    explicit TransWindow(QWidget *parent = 0);

    //setWindowFlags(windowFlags()| Qt::FramelessWindowHint);

    ~TransWindow();

private:
    Ui::TransWindow *ui;
};

#endif // TRANSWINDOW_H

And here is transwindow.cpp

这是 transwindow.cpp

#include "transwindow.h"
#include "ui_transwindow.h"

TransWindow::TransWindow(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::TransWindow)
{
    //setWindowTitle("NewWin");
    //setWindowFlags(Qt::FramelessWindowHint);
    //setAttribute(Qt::WA_TranslucentBackground,true);
    ui->setupUi(this);
}

TransWindow::~TransWindow()
{
    delete ui;
}

In the different source codes you'll see commented out lines which is the different things that I've tried. For the most part the problem is, if I un-comment out any of the lines that try to set the "Qt::FramlessWindowHint" the program runs normally but never opens a new window when I click the button on the main ui.

在不同的源代码中,您会看到注释掉的行,这是我尝试过的不同内容。在大多数情况下,问题是,如果我取消注释掉任何试图设置“Qt::FramlessWindowHint”的行,程序会正常运行,但当我单击主用户界面上的按钮时永远不会打开新窗口。

If I un-comment out any of the lines where I set the "Qt::WA_TranslucentBackground" the new window will open up when when the button is pressed in the main ui but the background of the new window is black, not transparent.

如果我取消注释掉我设置“Qt::WA_TranslucentBackground”的任何行,当在主用户界面中按下按钮但新窗口的背景是黑色而不是透明时,新窗口将打开。

Other info that may be pertinent: linux: ubunto 12.04 qt 5.0.2 (64-bit) qt creator 2.7.1

其他可能相关的信息: linux: ubunto 12.04 qt 5.0.2 (64-bit) qt creator 2.7.1

回答by duDE

Try this:

尝试这个:

setWindowFlags(Qt::Widget | Qt::FramelessWindowHint);
setParent(0); // Create TopLevel-Widget
setAttribute(Qt::WA_NoSystemBackground, true);
setAttribute(Qt::WA_TranslucentBackground, true);  
setAttribute(Qt::WA_PaintOnScreen); // not needed in Qt 5.2 and up