How to make a window transparent using Qt?

Asked

Viewed 857 times

3

I’m developing a college job in Qt. I would like to make the background of my application transparent. I did some research and the function I found was:

this->setWindowOpacity(0);

Meanwhile, the Qt applies opacity throughout the window. I would like the components I add, such as buttons or even drawings using the QPainter, keep their opacities normal, that is, do not want to apply opacity to the window components, only in the frame.

I’m using Debian 7.1 with the Gnome 3.4.2.

At the moment I’m having this result:

Qt_transparente

Note that the transparency applied to the button as well.

I removed some things from my application in order to facilitate the resolution of my problem. Follow the code:

Window. h

#ifndef JANELA_H
#define JANELA_H

#include <QMainWindow>
#include <QFrame>
#include <QPushButton>
#include <QWidget>
#include <QVBoxLayout>
#include <QPainter>
#include <QPen>

class Janela: public QMainWindow{
    Q_OBJECT
public:
    Janela();
    ~Janela();
    void paintEvent(QPaintEvent * event);

public:
    QFrame *frame;
    QPushButton *botao;
    int pos_x=280,pos_y=280;
};

#endif // JANELA_H

Window.cpp

#include <Janela.h>

Janela::Janela(){
    QFrame *frame = new QFrame();
    QVBoxLayout *layout = new QVBoxLayout();
    this->setWindowOpacity(0.80);
    setCentralWidget(frame);
    setWindowTitle(QString::fromUtf8("Exemplo de QFrame"));

    frame->setLineWidth(4);
    frame->setFrameStyle(QFrame::Box);
    frame->setFixedSize(300,300);
    frame->resize(300, 300);
    frame->setMinimumSize(300,300);
    frame->setMaximumSize(300,300);
    frame->setLayout(layout);

    botao = new QPushButton("Pressione-me");
    layout->addWidget(botao);

    show();
    frame->setFocus();

};

Janela::~Janela(){};

void Janela::paintEvent(QPaintEvent *event){
    QPainter painter(this);
    painter.setPen(Qt::green);
    painter.setBrush(Qt::green);
    painter.drawEllipse(10,10,10,10);
    painter.fillRect(pos_x,pos_y,10,10,Qt::red);
}

main.cpp

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

int main(int argc, char **argv){
    QApplication app(argc, argv);

    Janela *janela = new Janela();

    return app.exec();
}

1 answer

4


You can do this by activating the flag Qt::FramelessWindowHint in Janela leading.

Janela::Janela() : QMainWindow(NULL, Qt::FramelessWindowHint)

And then activate the attribute Qt::WA_TranslucentBackground to make the background invisible:

setAttribute(Qt::WA_TranslucentBackground);

The result is this:

Janela Transparent


To make the background translucent, and not just invisible, you add in Paintevent the background drawing you want. Example:

QPainter painter(this);

// Para usar a cor padrão de background do sistema.
QColor background_color = palette().color(QPalette::Background);
background_color.setAlpha(128); 
painter.setBrush(QBrush(background_color));

// Define uma borda.
painter.setPen(QColor("#888"));

// Desenha o novo background da janela, antes de qualquer outra coisa.
painter.drawRect(0, 0, width()-1, height()-1);

And the result is this:

Janela com Transparência

  • Perfect explanation. That’s exactly what I wanted. Very grateful! = D I noticed that just like in Java, Qt removes the close buttons. But this is not a problem. Since the window will be closed with a keyboard event.(http://answall.com/questions/9367/simulaca-de-controle-remoto-de-tv/9396#9396) There is how to reactivate them?

  • I think if you reactivate you lose transparency. But, you can also "simulate" the system buttons as well. It goes more work, but it doesn’t get bad. I did something similar to create a Visual Studio style window: http://i.imgur.com/Ao0sh9p.png

  • It was really good. Did you use icons on the buttons? I’m developing a Pacman: http://i.imgur.com/Bty9zaa.png made the window close when the ESC is pressed. I think that’s enough. Thanks for the help. This will serve me a lot (I’m a fan of transparent frames).

  • They’re real pictures, but they work like normal buttons. In your case, as it is a game, I think the ESC is even better, because most games come out in ESC. Just be sure to ask if the user is sure they want to leave.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.