Realize irregular shape window display under Qt


foreword

This article realizes the demo of displaying two irregularly shaped windows under Qt, including the Qt window dialog box and the simple use of QPaintEvent and QMouseEvent. Here, the relevant content is displayed for everyone to learn. If there are any mistakes, everyone is welcome to criticize Correct me.

Project effect
Please add a picture description


提示:以下是本篇文章正文内容,下面案例可供参考

1. Adding resource files

First, prepare a picture with a transparent background, and then the window formed will be the shape of your picture. In the demo of this article, you can add the picture as a resource file. The steps of adding a resource file are described in detail in my previous article. Readers You can check it by yourself: (1) Two ways for Qt to implement custom controls - promotion method (if you don't add resource files, just pay attention to the input path of the picture)

2. Initialize the window

The window is initialized here, and the main function is setMask(). You can view the details of this function through F1:

void Dialog::initWidget()
{
    
    
    //保持顶部窗口
    this->setWindowFlags(this->windowFlags() | Qt::WindowStaysOnTopHint);

    QPixmap pix;
    pix.load(":/photo/flower.png",0,Qt::AvoidDither);   //如果没有将图片添加为资源文件,此处路径更改为完整路径
    resize(pix.size());
    setMask(QBitmap(pix.mask()));                 //设置透明
    setAttribute(Qt::WA_TranslucentBackground);   //去除毛边

    //实例化对象
    myLL = new MyLL();
}

3. Rewrite the paintEvent function to achieve window redrawing

void Dialog::paintEvent(QPaintEvent *event)
{
    
    
    //qDebug()<<"event:"<<event;
    QPainter painter(this);
    painter.drawPixmap(rect(),QPixmap(":/photo/flower.png"),QRect());
}

4. Rewrite QMouseEvent related functions to realize the movement and closing of irregular windows

void Dialog::mousePressEvent(QMouseEvent *event)
{
    
    
    if(event->button() == Qt::LeftButton)
    {
    
    
        startPoint = event->globalPos() - frameGeometry().topLeft();
        event->accept();
    }
    if(event->button() == Qt::RightButton)
    {
    
    
        myLL->show();
    }
    if(event->button() == Qt::MidButton)
    {
    
    
        //关闭全部窗口
        myLL->close();
        this->close();
    }
}

void Dialog::mouseMoveEvent(QMouseEvent *event)
{
    
    
    if(event->buttons() & Qt::LeftButton)
    {
    
    
        move(event->globalPos() - startPoint);
        event->accept();
    }
}

Five, demo complete code

1.MyPhoto.pro

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

CONFIG += c++11

DEFINES += QT_DEPRECATED_WARNINGS

#设置生成名称及图标
RC_ICONS = ll.ico
TARGET = ling

SOURCES += \
    main.cpp \
    dialog.cpp \
    myll.cpp

HEADERS += \
    dialog.h \
    myll.h

FORMS += \
    dialog.ui \
    myll.ui

# Default rules for deployment.
qnx: target.path = /tmp/$${
    
    TARGET}/bin
else: unix:!android: target.path = /opt/$${
    
    TARGET}/bin
!isEmpty(target.path): INSTALLS += target

RESOURCES += \
    photo.qrc

#全局忽略编译警告QMAKE_CXXFLAGS
QMAKE_CXXFLAGS += -Wno-unused-function    #未使用的函数
QMAKE_CXXFLAGS += -Wno-unused-parameter   #设置了但未使用的参数
QMAKE_CXXFLAGS += -Wno-comment            #注释使用不规范
QMAKE_CXXFLAGS += -Wno-sequence-point     #如出现i=i++这类代码,则报警告

2.main.cpp

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

int main(int argc, char *argv[])
{
    
    
    QApplication a(argc, argv);
    Dialog w;
    if(w.keepHappy())
    {
    
    
        w.show();
    }
    return a.exec();
}

3.dialog.h

#ifndef DIALOG_H
#define DIALOG_H

#include <QDialog>
#include <QMessageBox>
#include "myll.h"

QT_BEGIN_NAMESPACE
namespace Ui {
    
     class Dialog; }
QT_END_NAMESPACE

class Dialog : public QDialog
{
    
    
    Q_OBJECT

public:
    Dialog(QWidget *parent = nullptr);
    ~Dialog();

    void initWidget();
    bool keepHappy();

protected:
    void paintEvent(QPaintEvent *event);
    void mousePressEvent(QMouseEvent *event);
    void mouseMoveEvent(QMouseEvent *event);

private:
    Ui::Dialog *ui;

    QPoint startPoint;
    MyLL *myLL;
};
#endif // DIALOG_H

4.dialog.cpp

#include "dialog.h"
#include "ui_dialog.h"

Dialog::Dialog(QWidget *parent)
    : QDialog(parent)
    , ui(new Ui::Dialog)
{
    
    
    ui->setupUi(this);
    this->initWidget();
}

Dialog::~Dialog()
{
    
    
    delete myLL;
    delete ui;
}

void Dialog::initWidget()
{
    
    
    //保持顶部窗口
    this->setWindowFlags(this->windowFlags() | Qt::WindowStaysOnTopHint);

    QPixmap pix;
    pix.load(":/photo/flower.png",0,Qt::AvoidDither);   //如果没有将图片添加为资源文件,此处路径更改为完整路径
    resize(pix.size());
    setMask(QBitmap(pix.mask()));                 //设置透明
    setAttribute(Qt::WA_TranslucentBackground);   //去除毛边

    //实例化对象
    myLL = new MyLL();
}

//main函数调用
bool Dialog::keepHappy()
{
    
    
    const QMessageBox::StandardButton ret
        = QMessageBox::information(this,"Nice to meet you!!!Love Lingling","Be confident and happy every day! Send you a flower flower ~~~"
                                                          "\n每天都要自信且开心喔!送你一朵小花花~",QMessageBox::Yes | QMessageBox::No);
    if(ret == QMessageBox::Yes)
    {
    
    
        return true;
    }
    else
    {
    
    
        keepHappy();
    }
    return true;
}

void Dialog::paintEvent(QPaintEvent *event)
{
    
    
    //qDebug()<<"event:"<<event;
    QPainter painter(this);
    painter.drawPixmap(rect(),QPixmap(":/photo/flower.png"),QRect());
}

void Dialog::mousePressEvent(QMouseEvent *event)
{
    
    
    if(event->button() == Qt::LeftButton)
    {
    
    
        startPoint = event->globalPos() - frameGeometry().topLeft();
        event->accept();
    }
    if(event->button() == Qt::RightButton)
    {
    
    
        myLL->show();
    }
    if(event->button() == Qt::MidButton)
    {
    
    
        //关闭全部窗口
        myLL->close();
        this->close();
    }
}

void Dialog::mouseMoveEvent(QMouseEvent *event)
{
    
    
    if(event->buttons() & Qt::LeftButton)
    {
    
    
        move(event->globalPos() - startPoint);
        event->accept();
    }
}

5.myll.h

#ifndef MYLL_H
#define MYLL_H

#include <QWidget>
#include <QMouseEvent>
#include <QPainter>
#include <QPixmap>
#include <QBitmap>
#include <QDebug>

namespace Ui {
    
    
class MyLL;
}

class MyLL : public QWidget
{
    
    
    Q_OBJECT

public:
    explicit MyLL(QWidget *parent = nullptr);
    ~MyLL();

    void initWidget();

protected:
    void paintEvent(QPaintEvent *event);
    void mousePressEvent(QMouseEvent *event);
    void mouseMoveEvent(QMouseEvent *event);

private:
    Ui::MyLL *ui;
    QPoint startPoint;
};
#endif // MYLL_H

6.myll.cpp

#include "myll.h"
#include "ui_myll.h"

MyLL::MyLL(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::MyLL)
{
    
    
    ui->setupUi(this);
    this->initWidget();
}

MyLL::~MyLL()
{
    
    
    delete ui;
}

void MyLL::initWidget()
{
    
    
    this->setWindowFlags(this->windowFlags() | Qt::WindowStaysOnTopHint);

    QPixmap pix;
    pix.load(":/photo/ll.png",0,Qt::AvoidDither);
    resize(pix.size());
    setMask(QBitmap(pix.mask()));                 //设置透明
    //setAttribute(Qt::WA_TranslucentBackground);   //去除毛边
}

void MyLL::paintEvent(QPaintEvent *event)
{
    
    
    //qDebug()<<"event:"<<event;
    QPainter painter(this);
    painter.drawPixmap(rect(),QPixmap(":/photo/ll.png"),QRect());
}

void MyLL::mousePressEvent(QMouseEvent *event)
{
    
    
    if(event->button() == Qt::LeftButton)
    {
    
    
        startPoint = event->globalPos() - frameGeometry().topLeft();
        event->accept();
    }
    if(event->button() == Qt::RightButton)
    {
    
    
        this->close();
    }
}

void MyLL::mouseMoveEvent(QMouseEvent *event)
{
    
    
    if(event->buttons() & Qt::LeftButton)
    {
    
    
        move(event->globalPos() - startPoint);
        event->accept();
    }
}

7.dialog.ui (QDialog was selected as the Base class when creating a new project)
Please add a picture description

8.myll.ui (Widget was selected as the interface template when creating a new Qt designer interface class)
Please add a picture description

6. Download link

Demo Baidu network disk link: https://pan.baidu.com/s/1_v1lC1unytYTEituAJJ8iA
Extraction code: xxcj

Summarize

Here is a simple Qt development demo. Some of the knowledge points used are commented in the article. In addition, the pictures used need to be transparent, so that the irregular window display can be realized. The text displayed on the interface can be customized by the reader, so don’t worry about it The ones I wrote~ (PS: This demo is for me to make my partner happy, I sent a flower and her favorite Pikachu, hehehe)


hello:
Learn together and make progress together. If you still have related questions, you can leave a message in the comment area for discussion.

Reference blog: Qt study notes - irregular window (custom shape window

Guess you like

Origin blog.csdn.net/XCJandLL/article/details/129124952