Qt buton窗口中拖动,右键弹出菜单可调整大小
最近写了一个小项目案例,可点击button拖动,可右键弹出菜单调整button大小,可用于有需要的同学进行扩展
1、运行效果
初始效果
鼠标左键按下拖动
选中button右键
填写调整的大小数值
点击ok
2、实现如下:
#include <QMouseEvent> #include <QDebug> #include "MoveButton.h" MoveButton::MoveButton(QWidget *parent) : QPushButton(parent) { resizeWHDialog = new ResizeWHDialog(this); //this->setStyleSheet("background-color: rgb(218,165,32); border: 1px"); buttonAction1 = new QAction(QStringLiteral("宽高调整"),this); buttonMenu = new QMenu(this); buttonMenu->addAction(buttonAction1); connect(resizeWHDialog,&ResizeWHDialog::resizeWHSignal,[&](uint w,uint h){ qDebug()<<"resizeWHDialog accept,w="<<w<<",h="<<h; emit resizeBtnWHSignal(this,this->x(),this->y(),w,h); }); connect( buttonAction1, &QAction::triggered, [=]() { qDebug()<<"buttonAction1 is triggered"; resizeWHDialog->exec(); }); this->setContextMenuPolicy(Qt::CustomContextMenu); connect(this,&QPushButton::customContextMenuRequested,[&](const QPoint &pos){ qDebug()<<pos;//参数pos用来传递右键点击时的鼠标的坐标,这个坐标一般是相对于控件左上角而言的 buttonMenu->exec(QCursor::pos()); }); } void MoveButton::on_pushButton_customContextMenuRequested(const QPoint &pos) { qDebug()<<pos;//参数pos用来传递右键点击时的鼠标的坐标,这个坐标一般是相对于控件左上角而言的 buttonMenu->exec(QCursor::pos()); } void MoveButton::setWight(const int w)noexcept { this->setWight(w); } void MoveButton::setHeight(const int h)noexcept { this->setHeight(h); } void MoveButton::mousePressEvent(QMouseEvent *event) { if(event->button() == Qt::LeftButton) { _beginPos = event->pos(); } } void MoveButton::mouseMoveEvent(QMouseEvent *event) { _lastPos = event->pos(); int t_xDistance = _lastPos.x() - _beginPos.x(); int t_yDistance = _lastPos.y() - _beginPos.y(); if((this->x()+t_xDistance<=parentWidget()->width()-this->width()) && (this->y()+t_yDistance<=parentWidget()->height()-this->height()) && this->x()+t_xDistance>=0 && this->y()+t_yDistance>=0) { this->move(this->x()+t_xDistance,this->y()+t_yDistance); } } void MoveButton::keyPressEvent(QKeyEvent *event) { if(event->key() == Qt::Key_Left) { if(this->x()-4>=0) { this->move(this->x()-4,this->y()); } } else if(event->key() == Qt::Key_Right) { if(parentWidget()->width()-this->width()-this->x()-4>=0) { this->move(this->x()+4,this->y()); } } else if(event->key() == Qt::Key_Up) { if(this->y()-4>=0) { this->move(this->x(),this->y()-4); } } else if(event->key() == Qt::Key_Down) { if(parentWidget()->height()-this->height()-this->y()-4>=0) { this->move(this->x(),this->y()+4); } } }
#include <QDebug> #include "ResizeWH.h" #include "ui_resizewhdialog.h" ResizeWHDialog::ResizeWHDialog(QWidget *parent) : QDialog(parent), ui(new Ui::ResizeWHDialog) { ui->setupUi(this); } ResizeWHDialog::~ResizeWHDialog() { delete ui; } void ResizeWHDialog::on_buttonBox_accepted() { qDebug()<<"ResizeWHDialog accept,w="<<ui->lineEdit_h->text().toUInt()<<",h="<<ui->lineEdit_w->text().toUInt(); emit resizeWHSignal(ui->lineEdit_h->text().toUInt(),ui->lineEdit_w->text().toUInt()); }
#ifndef MOVEBUTTON_H #define MOVEBUTTON_H #include <QObject> #include <QPushButton> #include <QWidget> #include <QAction> #include <QMenu> #include "ResizeWH.h" class MoveButton : public QPushButton { Q_OBJECT public: explicit MoveButton(QWidget *parent = nullptr); void setWight(const int w=0)noexcept; void setHeight(const int h=0)noexcept; private slots: void on_pushButton_customContextMenuRequested(const QPoint &pos); signals: void resizeBtnWHSignal(QPushButton *obj,uint x = 0,uint y = 0, uint w = 0,uint h =0); protected: void mousePressEvent(QMouseEvent *event); void mouseMoveEvent(QMouseEvent *event); void keyPressEvent(QKeyEvent *event); private: QPoint _beginPos; QPoint _lastPos; QAction * buttonAction1; QMenu * buttonMenu; ResizeWHDialog *resizeWHDialog; }; #endif // MOVEBUTTON_H