相比此方案 ,已有更好的解决方案 https://blog.youkuaiyun.com/qq_33259248/article/details/128222959
项目中要实现类似于下图的一个窗体,里面的红色部分是按钮
直接上代码
按钮类:
#ifndef MyButton_H
#define MyButton_H
#include <QObject>
#include <QPointer>
#include <QPushButton>
class MyButton : public QPushButton
{
public:
explicit MyButton(QString flag = "", QWidget *parent = nullptr);
void setFriendWidget(QWidget *widget);
protected:
void paintEvent(QPaintEvent *event);
void mousePressEvent(QMouseEvent *event);
private:
QString flag_ ;
QPainterPath path_;
QPointer< QWidget >friendWidget_;
};
#endif // MyButton_H
#include "mybutton.h"
#include <QPainter>
#include <QBitmap>
#include <QDebug>
#include <QStyleOptionTab>
#include <QMouseEvent>
#include <QApplication>
MyButton::MyButton(QString flag, QWidget *parent):
QPushButton(parent)
{
flag_ = flag;
setWindowFlags(Qt::FramelessWindowHint);
setAttribute(Qt::WA_TranslucentBackground);//设置背景透明
QPixmap pixmap(":/view/view/doing1.png");
setFixedSize(pixmap.size());
}
void MyButton::setFriendWidget(QWidget *widget)
{
friendWidget_ = widget;
}
void MyButton::paintEvent(QPaintEvent *event)
{
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing);
QBrush brush(QColor(255, 0, 0), Qt::SolidPattern);
painter.setBrush(brush);
QPainterPath path;
path.moveTo(0, 0);
path.lineTo(98, 0);
path.lineTo(118, 19);
path.lineTo(98, 38);
path.lineTo(0, 38);
path.lineTo(20, 19);
path.closeSubpath();
QFont font;
font.setPixelSize(16);
path.addText(40, 20, font, flag_);
painter.setPen(Qt::NoPen);
painter.drawPath(path);
path_ = path;
}
void MyButton::mousePressEvent(QMouseEvent *event)
{
if (this->parent() && (event->buttons() & Qt::LeftButton))
{
qDebug() << flag_ << event->pos().rx() << event->pos().ry();
if (path_.contains(event->pos()))
{
qDebug() << flag_ << "按钮内部" ;
QPushButton::mousePressEvent(event);
}
else
{
qDebug() << flag_ << "按钮外部" << friendWidget_ ;
this->setAttribute(Qt::WA_TransparentForMouseEvents, true);
//将点击事件在当前部件的坐标转换为在父窗口坐标系中的坐标
QPoint point = this->mapToGlobal(event->pos());
if (friendWidget_ != nullptr)
{
//将当前部件在父窗口的坐标转为在当前部件自己的坐标系中的坐标
QPoint point1 = friendWidget_->mapFromGlobal(point);
QMouseEvent *mouseEvent = new QMouseEvent(event->type(), point1, event->button(), event->buttons(), event->modifiers());
QApplication::postEvent(friendWidget_, mouseEvent);
}
this->setAttribute(Qt::WA_TransparentForMouseEvents, false);
}
}
}
主界面
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QPainter>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
button1_ = new MyButton("button1", this);
button2_ = new MyButton("button2", this);
button3_ = new MyButton("button3", this);
button2_->setFriendWidget(button1_);
button3_->setFriendWidget(button2_);
connect(button1_, &MyButton::pressed, this, &MainWindow::slotButton1);
connect(button2_, &MyButton::pressed, this, &MainWindow::slotButton2);
connect(button3_, &MyButton::pressed, this, &MainWindow::slotButton3);
button1_->move(100, 100);
button2_->move(button1_->pos() + QPoint(-30 + button1_->width(), 0));
button3_->move(button2_->pos() + QPoint(-30 + button2_->width(), 0));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::slotButton1()
{
ui->lineEdit->setText("1");
}
void MainWindow::slotButton2()
{
ui->lineEdit->setText("2");
}
void MainWindow::slotButton3()
{
ui->lineEdit->setText("3");
}
解释:
- 按钮中通过重写paintEvent 绘制出按钮形状. 显示如图
- 但是此时按钮的鼠标点击事件触发范围,如下图所示,三个按钮的触发范围分别为 黑色框,蓝色框,绿色框,和我们想要的不符合.我们想要的点击范围为三个宽箭头形状
- 重写 mousePressEvent,判断鼠标点击点是否在 path范围内,如果在正常触发事件, 如果不在,则当前按钮忽略此事件,这样就不会在点击空白处时误触发(如上图1处) . 然后将此事件发给 friendWidget
- 按钮new的顺序是,1,2,3, 所以button2盖住了button1的一部分(上图2处), 此时点击2处实际触发的是button.我把button1 作为friendwidget给button2,如果button2鼠标点击事件不在path范围内, 则把点击事件发给button1,button1判断点是否在当前path范围内,如果不在则忽略, 如果在则说明点击到上图2处,触发button1的事件