QT中创建透明或者异性的Window

1.main.cpp中 打开窗体的那里添加

      w.setAttribute(Qt::WA_TranslucentBackground);


这样就不会有百边了


然后,假如是MainWindow

头文件里面添加:




protected:

    void paintEvent(QPaintEvent *event);


    void mousePressEvent(QMouseEvent *event);


    void mouseMoveEvent(QMouseEvent *event);


private:


    Ui::MainWindow *ui;


    QPoint dPos;


    QPoint windowPos;


    QPoint mousePos;



cpp文件中:


初始化中:

      QPixmap pix;

    pix.load(":/new/prefix1/image/login.png",0,Qt::AvoidDither|Qt::ThresholdDither|Qt::ThresholdAlphaDither);


    resize(pix.size());


    setMask(QBitmap(pix.mask()));


然后是下面的:

//需要引入#include "QtGui"
void MainWindow::paintEvent(QPaintEvent *event)
{
   QPainter painter(this);
   painter.drawPixmap(0,0,QPixmap(":/new/prefix1/image/login.png"));
//   painter.drawPixmap(0,0,width(),height(),QPixmap(":/new/prefix1/image/login.png"));
//   event->accept();
//   qDebug()<<"set back ground image";
}
 
  
 
  
 
  
 
  
 
  
 
  
 
  
 
  
 
  

 
  
 
  
   
 
  
 
  
 
  
//#region
//移动窗体
//QPoint dPos;
//QPoint windowPos;
//QPoint mousePos;
 
  
//w.setWindowOpacity(1);
//w.setWindowFlags(Qt::FramelessWindowHint);
//w.setAttribute(Qt::WA_TranslucentBackground);
 
  
void MainWindow::mousePressEvent(QMouseEvent *event){
    this->windowPos = this->pos();
    this->mousePos = event->globalPos();
    this->dPos = mousePos - windowPos;
}
void MainWindow::mouseMoveEvent(QMouseEvent *event){
    this->move(event->globalPos()-dPos);
}


这样做出来的异常窗体,你去任务栏里面点一下看看。。。


 很多网络教程不是使用setMask的,你去任务栏点击看看,不起作用!




你可能感兴趣的:(布局)