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:
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();
}
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?
– Avelino
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
– Lucas Lima
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).
– Avelino
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.
– Lucas Lima