QT Programming-Remove the title bar and set the window transparent usage __ programming

Source: Internet
Author: User

Learning QT Programming, sometimes we want to make good-looking and relatively cool picture, then we often use some of the skills of QT.

Here I have a small example to show the QT techniques, this QT programming, as shown: (Remove the title bar and set the window after transparent)

QQ Mini version of the network chat software detailed in QT programming-my QQ (LAN available)-including source code

Code Implementation section:

. h Files

<span style= "FONT-SIZE:14PX;" > #ifndef mainwindow_h
#define MAINWINDOW_H

#include <QMainWindow>
#include <QLabel>
#include <QMouseEvent>
#include <QPalette>

namespace Ui {
    class MainWindow;
}

Class Mainwindow:public Qmainwindow
{
    q_object public

:
    explicit MainWindow (Qwidget *parent = 0); C13/>~mainwindow ();

Private:
    Ui::mainwindow *ui;

Private slots:
    void on_pushbutton_set_clicked ();

#endif//Mainwindow_h

</span>

Mainwindow.cpp

<span style= "FONT-SIZE:14PX;" > #include "mainwindow.h"
#include "ui_mainwindow.h"

Mainwindow::mainwindow (Qwidget *parent):
    Qmainwindow (parent),
    UI (new Ui::mainwindow)
{
    ui->setupui (this);
    This->setwindowtitle ("QQ");
    This->setwindowicon (Qicon (":/images/po.jpg"));
    This->setwindowflags (qt::framelesswindowhint);/Remove title bar
     this->setgeometry (Qrect (950, 55, 350, 250)); You can set the orientation and size of the window display

    //this->setwindowopacity (0.7);//Set transparent 1-all transparent
    this->setattribute (Qt::wa_ Translucentbackground, True);/Set transparent 2-the form title bar is opaque, the background is transparent
    this->resize (300,300);//display size
}

MainWindow: : ~mainwindow ()
{
    delete ui;
}

</span>


Main.cpp file

<span style= "FONT-SIZE:14PX;" > #include <QtGui/QApplication>
#include <QTextCodec>
#include "mainwindow.h"

int main ( int argc, char *argv[])
{
    qapplication A (argc, argv);
    Qtextcodec::setcodecforcstrings (Qtextcodec::codecforname ("GB2312"));
    Qtextcodec::setcodecforlocale (Qtextcodec::codecforname ("GB2312"));
    QTEXTCODEC::SETCODECFORTR (Qtextcodec::codecforname ("GB2312"));
    MainWindow W;
    W.show ();

    return a.exec ();
}
</span>


The transparency used in this example is that the form title bar is opaque and the background is transparent.

Here are several ways to set transparency effects:
1.this->setwindowopacity (0.7);/all transparent (refers to the form, title bar and all the controls above are transparent) inside the parameters can control transparency.

2. The window is overall transparent, but the controls on the form are opaque. By setting the background color of the form to achieve, set the background color to full through:

The code is as follows:

<span style= "FONT-SIZE:14PX;" > pal = Palette ();
 Pal.setcolor (Qpalette::background, Qcolor (0x00,0xff,0x00,0x00));
 SetPalette (PAL);</span>

3. The form title bar is opaque and the background is transparent. (used in this example)
This->setattribute (qt::wa_translucentbackground,true);

4. The overall opacity of the window, local transparency: Use clear mode drawing in the Paint event.

<span style= "FONT-SIZE:14PX;" >void MainWindow::p aintevent (qpaintevent*) 

{qpainter P (this); 

   P.setcompositionmode (qpainter::compositionmode_clear);
 P.fillrect (at), Qt::solidpattern; 
</span>

The drawing area is fully transparent, and if the drawing area has controls, it does not affect the transparency of the control.

5. Here's how to get rid of the title bar in this program

This->setwindowflags (Qt::framelesswindowhint)//Remove title bar

Reprint indicated: http://blog.csdn.net/liuyang1990i/article/details/8227342 wrote here, Pro, has the harvest?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.