zoukankan      html  css  js  c++  java
  • Qt坐标系以及自定义可移动控件

    1、效果

    黑色这个控件是自定义的,可以在界面拖动

    2、新建控件的Qt类,父类是QWidget,带ui的,Control.h

    #ifndef CONTROL_H
    #define CONTROL_H
    #include <QWidget>
    
    namespace Ui {
    class Control;
    }
    
    class Control : public QWidget
    {
        Q_OBJECT
    
    public:
        explicit Control(QWidget *parent = 0);
        ~Control();
        int A1X,A1Y;
        int A2X,A2Y;
        int pressX,pressY;
        int curentX,curentY;
    protected:
        void paintEvent(QPaintEvent *event);
        void mouseMoveEvent(QMouseEvent *event);
        void mousePressEvent(QMouseEvent *event);
    
    private:
        Ui::Control *ui;
    };
    
    #endif // CONTROL_H

    3、Control.cpp

    #include "control.h"
    #include "ui_control.h"
    #include <QDebug>
    #include <QMouseEvent>
    Control::Control(QWidget *parent) :
        QWidget(parent),
        ui(new Ui::Control)
    {
        ui->setupUi(this);
        A2X = this->pos().x();
        A2Y = this->pos().y();
    }
    
    Control::~Control()
    {
        delete ui;
    }
    
    void Control::paintEvent(QPaintEvent *event)
    {
    #if 1
        QPalette palette(this->palette());
        palette.setColor(QPalette::Background, Qt::black);
        this->setAutoFillBackground(true);
        this->setPalette(palette);
    #endif
        if(A2X==0 && A2Y==0)
        {
            return;
        }
        this->move(A2X,A2Y);
    }
    
    void Control::mouseMoveEvent(QMouseEvent *event)
    {
        if (event->buttons() == Qt::LeftButton) // 这个if判断只是为了保证是按压鼠标左键移动产生窗体拖动效果
        {
    #if 0
            curentX = event->x();//鼠标点相对当前widget的位置
            curentY = event->y();
    #endif
            curentX = event->globalX();//鼠标点相对整个显示器的位置
            curentY = event->globalY();
            A2X = A1X+curentX-pressX;
            A2Y = A1Y+curentY-pressY;
    #if 0
            qDebug()<< "mouseMoveEvent:" << curentX << curentY;
            qDebug()<< "A2XY:" << A2X << A2Y;
    #endif
            update();
        }
    }
    
    void Control::mousePressEvent(QMouseEvent *event)
    {
        pressX = event->globalX();//鼠标点相对整个显示器的位置
        pressY = event->globalY();
        A1X = this->x();//widget左上角相对程序的位置
        A1Y = this->y();
    #if 0
        int a = event->x();//鼠标点相对当前widget的位置
        int b = event->y();
        int c = this->geometry().x();//widget左上角相对程序的位置
        int d = this->geometry().y();
        qDebug()<< "mousePressEvent:" << pressX << pressY;
        qDebug()<< "A1XY:" << A1X << A1Y;
        qDebug()<< "A1xy:" << A1X << A1Y;
        qDebug()<< "ab:" << a << b;
        qDebug()<< "cd:" << c << d;
    #endif
    }

    4、在main.ui里拖入widget控件,提升为Control,运行,ok

  • 相关阅读:
    深度学习面试问题
    重新学习pytorch的库函数等..
    新电脑的操作系统win10的所有设置问题汇总
    二叉搜索树,和红黑树,
    Most common words
    Word histogram
    Random numbers
    Word frequency analysis
    DSU
    Sequences of sequences
  • 原文地址:https://www.cnblogs.com/judes/p/11016771.html
Copyright © 2011-2022 走看看