• 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
    作者:灼光
    本博客文章大多为原创,转载请在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
  • 相关阅读:
    listctrl中的cell如何支持被复制
    Can not issue data manipulation statements with executeQuery()的解决方案
    Jenkins工具学习(一)
    如何对图片进行测试
    WebDriver常用的api
    Cannot get a STRING value from a NUMERIC cell问题的解决办法
    Eclipse中安装springmvc插件
    Tomcat 的端口被占用的解决办法
    pyCharm最新激活码(2018激活码)
    Error: Error occured while starting App. Original error: Activity used to start app doesn't exist or cannot be launched! Make sure it exists and is a launchable activity
  • 原文地址:https://www.cnblogs.com/laiyingpeng/p/14306439.html
Copyright © 2020-2023  润新知