• 事件


    鼠标事件

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    
    #include <QMainWindow>
    #include <QDebug>
    #include <QMouseEvent>
    #include <QEvent>
    #include <QMenu>
    #include <QLabel>
    
    
    QT_BEGIN_NAMESPACE
    namespace Ui { class MainWindow; }
    QT_END_NAMESPACE
    
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    
    public:
        MainWindow(QWidget *parent = nullptr);
        ~MainWindow();
    
    protected:
        //鼠标按下处理函数重写
        void mousePressEvent(QMouseEvent *event);
        //鼠标移动事件
        void mouseMoveEvent(QMouseEvent *event);
        //鼠标松开事件
        void mouseReleaseEvent(QMouseEvent *event);
        //鼠标双击事件
        void mouseDoubleClickEvent(QMouseEvent *event);
    
    private:
        Ui::MainWindow *ui;
        QLabel *statusLabel;
        QLabel *MousePosLabel;
    };
    #endif // MAINWINDOW_H
    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    
    MainWindow::MainWindow(QWidget *parent)
        : QMainWindow(parent)
        , ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    
        //设置主窗体的标题
        setWindowTitle(tr("鼠标事件"));
    
        //创建控件statusLabel
        statusLabel = new QLabel;
        statusLabel->setText(tr("当前位置:"));
        statusLabel->setFixedWidth(100);
    
        //创建控件MousePosLabel
        MousePosLabel = new QLabel;
        MousePosLabel->setText(tr(""));
        MousePosLabel->setFixedWidth(100);
    
        //在mainwindow状态栏中增加控件
        statusBar()->addPermanentWidget(statusLabel);
        statusBar()->addPermanentWidget(MousePosLabel);
    
        //设置窗体是否追踪鼠标,默认false
        this->setMouseTracking(true);
        resize(400,200);
    
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    
    void MainWindow::mousePressEvent(QMouseEvent *event)
    {
    
        QString str = "("+QString::number(event->x())+","+QString::number(event->y())+")";
        if(event->button() == Qt::LeftButton)
        {
            statusBar()->showMessage(tr("左键:")+str);
        }
        else if(event->button() == Qt::RightButton)
        {
            statusBar()->showMessage(tr("右键:")+str);
        }
        else if(event->button() == Qt::MidButton)
        {
            statusBar()->showMessage(tr("中键:")+str);
        }}
    
    void MainWindow::mouseMoveEvent(QMouseEvent *event)
    {
        MousePosLabel->setText("("+QString::number(event->x())+","+QString::number(event->y())+")");
    }
    
    void MainWindow::mouseReleaseEvent(QMouseEvent *event)
    {
        QString str = "(" + QString::number(event->x()) + "," + QString::number(event->y()) + ")";
        statusBar()->showMessage(tr("释放在:") + str,3000);
    }
    
    void MainWindow::mouseDoubleClickEvent(QMouseEvent *event)
    {
        qDebug()<<"双击";
    }

    按键事件

    #ifndef WIDGET_H
    #define WIDGET_H
    
    #include <QWidget>
    #include <QKeyEvent>
    #include <QPaintEvent>
    #include <QPainter>
    
    QT_BEGIN_NAMESPACE
    namespace Ui { class Widget; }
    QT_END_NAMESPACE
    
    class Widget : public QWidget
    {
        Q_OBJECT
    
    public:
        Widget(QWidget *parent = nullptr);
        ~Widget();
    
        void drawPix();
        //重写按键事件
        void keyPressEvent(QKeyEvent *event);
        //重写画图事件
        void paintEvent(QPaintEvent *event);
    
    private:
        QPixmap *pix;   //绘制图形
        QImage image;   //界面里的那个小图标
        int startX;     //图标的位置X
        int startY;     //图标的位置Y(左上角的位置)
        int width;      //界面的宽度
        int height;     //界面高度
        int step;       //网格大小(移动的步进)
    
    
    private:
        Ui::Widget *ui;
    };
    #endif // WIDGET_H
    #include "widget.h"
    #include "ui_widget.h"
    
    Widget::Widget(QWidget *parent)
        : QWidget(parent)
        , ui(new Ui::Widget)
    {
        ui->setupUi(this);
    
        setWindowTitle(tr("键盘事件"));
    
        setAutoFillBackground(true);
    
        QPalette palette = this->palette();
        palette.setColor(QPalette::Window,Qt::white);
        setPalette(palette);
    
        setMinimumSize(512,256);
        setMaximumSize(512,256);
    
        width = size().width();
        height = size().height();
    
        pix = new QPixmap(width,height);
        pix->fill(Qt::white);
    
        image.load("C:/Users/Administrator/Desktop/123.png");
    
        startX = 100;
        startY = 100;
    
        step = 20;
    
        drawPix();
    
        resize(512,256);
    }
    
    Widget::~Widget()
    {
        delete ui;
    }
    
    void Widget::drawPix()
    {
        //背景色底色填充为白色
        pix->fill(Qt::white);
        QPainter *painter = new QPainter;
        //创建QPen对象,使用画笔 DotLine ,绘制网格
        QPen pen(Qt::DotLine);
    
        //循环画出横线竖线
        for(int i = step; i<width ;i = i +step)
        {
            painter->begin(pix);
            painter->setPen(pen);
            painter->drawLine(QPoint(i,0),QPoint(i,height));
            painter->end();
        }
        for(int i = step; i<height ;i = i +step)
        {
            painter->begin(pix);
            painter->setPen(pen);
            painter->drawLine(QPoint(0,i),QPoint(width,i));
            painter->end();
        }
        //画出可移动的小图标
        painter->begin(pix);
        painter->drawImage(QPoint(startX,startY),image);
        painter->end();
    }
    
    void Widget::keyPressEvent(QKeyEvent *event)
    {
        //判断ctrl是否按下 当ctrl按下同时按上下左右为微调
        if(event->modifiers() == Qt::ControlModifier)
        {
            if(event->key() == Qt::Key_Left)
            {
                startX = (startX - 1 < 0)?startX:startX-1;
            }
            if(event->key() == Qt::Key_Right)
            {
                startX = (startX +1+image.width()>width)?startX:startX+1;
            }
            if(event->key() == Qt::Key_Up)
            {
                startY = (startY-1<0)?startY:startY-1;
            }
            if(event->key() == Qt::Key_Down)
            {
                startY = (startY+1+image.height()>height)?startY:startY+1;
            }
        }
        else    //没有ctrl按下时上下左右为正常移动
        {
            startX = startX - startX%step;
            startY = startY - startY%step;
    
            if(event->key() == Qt::Key_Left)
            {
                startX = (startX-step<0)?startX:startX-step;
            }
            if(event->key() == Qt::Key_Right)
            {
                startX = (startX + step + image.width()>width)?startX:startX+step;
            }
            if(event->key() == Qt::Key_Up)
            {
                startY = (startY-step<0)?startY:startY-step;
            }
            if(event->key() == Qt::Key_Down)
            {
                startY = (startY + step + image.height()>height)?startY:startY+step;
            }
            //home键复位 置为左上
            if(event->key() == Qt::Key_Home)
            {
                startX = 0;
                startY = 0;
            }
            //end键结束 置为右下
            if(event->key() == Qt::Key_End)
            {
                startX = width-image.width();
                startY = height-image.height();
            }
            //调整位置后 重新绘制小图标
            drawPix();
            update();
        }
    }
    
    void Widget::paintEvent(QPaintEvent *event)
    {
        QPainter painter;
        painter.begin(this);
        painter.drawPixmap(QPoint(0,0),*pix);
        painter.end();
    }
  • 相关阅读:
    数据查询表,列名对比
    Oracle ORA06550:错误
    设计模式——单例模式
    MS SQL 建表SQL的脚本
    WinForm 清空界面控件值的小技巧
    WinForm系列——控件的输入限制
    C++学习笔记——常量定义
    PLSQL 调试触发器
    static类为什么不能用sealed关键字修饰
    C++虚函数解析(转)
  • 原文地址:https://www.cnblogs.com/qifeng1024/p/12905547.html
Copyright © 2020-2023  润新知