Document directory
- Add it to your resource package and set it as the background of the form.
-
Many of my friends asked me how to do the transparent effect. Why do my borderless forms cannot be moved? I am tired of answering them one by one. I just wrote them and shared them.
int main(int argc, char *argv[]){ QApplication::setStyle("cleanlooks"); QApplication a(argc, argv); login w; w.setWindowTitle("ClientLogin"); w.setWindowOpacity(1); w.setWindowFlags(Qt::FramelessWindowHint); w.setAttribute(Qt::WA_TranslucentBackground); w.show(); w.move(200,100); return a.exec();}
The key statement is:
w.setWindowOpacity(1); w.setWindowFlags(Qt::FramelessWindowHint); w.setAttribute(Qt::WA_TranslucentBackground);
If you do not know what these statements mean, Press F1 or directly view the help documentation ......
The settings for the form without borders should be written in the main, so that all derived subwindows, qdialog, and qwidget can be inherited, well planned and managed to facilitate unified beautification design.
Take a chat window in the project as an example. First, use psto create a background image of the Form. Note that it is stored in PNG format, which is the key to transparency. No ps. You can find some PNG resource images. My PNG transparent background image is:
Add it to your resource package and set it as the background of the form. It's my project, and the scenario settings are actually the background image of the new project ~~
You can preview the transparent borderless form, but there is another important problem that the window cannot be moved.
This is also caused by no borders ...... I will not elaborate on the specific reasons. It is very clear to search for them. I will only talk about the solution.
In each subwindow, add:
void yourwindow::mousePressEvent(QMouseEvent *event){ this->windowPos = this->pos(); this->mousePos = event->globalPos(); this->dPos = mousePos - windowPos;}void yourwindow::mouseMoveEvent(QMouseEvent *event){ this->move(event->globalPos() - this->dPos);}
In this way, the success is achieved. Run it to see the effect. The green forest is a desktop, which can be ignored. Welcome