Qt笔记——Event

Posted 大蓝鲸

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Qt笔记——Event相关的知识,希望对你有一定的参考价值。

#ifndef MYBUTTON_H
#define MYBUTTON_H

#include <QPushButton>

class MyButton : public QPushButton
{
    Q_OBJECT
public:
    explicit MyButton(QWidget *parent = nullptr);

protected:
    void mousePressEvent(QMouseEvent *e);
signals:

public slots:
};

#endif // MYBUTTON_H
#include "mybutton.h"
#include <QMouseEvent>
#include <QDebug>
MyButton::MyButton(QWidget *parent) : QPushButton(parent)
{

}

void MyButton::mousePressEvent(QMouseEvent *e)
{
    if(e->button() == Qt::LeftButton)
    {
        qDebug()<<"left mousebutton";

        e->ignore();
    }
    else
    {
        QPushButton::mousePressEvent(e);
    }
}
#ifndef MYLABEL_H
#define MYLABEL_H

#include <QLabel>

class mylabel : public QLabel
{
    Q_OBJECT
public:
    explicit mylabel(QWidget *parent = nullptr);
protected:
    void mousePressEvent(QMouseEvent *ev);
    void mouseReleaseEvent(QMouseEvent *ev);
    void mouseMoveEvent(QMouseEvent *ev);
    void enterEvent(QEvent *);
    void leaveEvent(QEvent *);
signals:

public slots:
};

#endif // MYLABEL_H
#include "mylabel.h"
#include <QMouseEvent>
#include <QEvent>
mylabel::mylabel(QWidget *parent) : QLabel(parent)
{
    this->setMouseTracking(true);
}

void mylabel::mousePressEvent(QMouseEvent *ev)
{
    int i = ev -> x();
    int j = ev -> y();
//    QString str = QString("abc %1 ^_^ %2").arg(123).arg("hello");
    if (ev->button() == Qt::LeftButton)
    {

    }else if(ev->button() == Qt::RightButton)
    {

    }else if(ev->button() == Qt::MiddleButton)
    {

    }
    QString text = QString("<center><h1>Mouse Press: (%1, %2)</h1></center>")
            .arg(i).arg(j);
    this->setText(text);
}

void mylabel::mouseReleaseEvent(QMouseEvent *ev)
{
    QString text = QString("<center><h1>Mouse Release: (%1, %2)</h1></center>")
            .arg(ev -> x()).arg(ev -> y());
    this->setText(text);
}

void mylabel::mouseMoveEvent(QMouseEvent *ev)
{
    QString text = QString("<center><h1>Mouse Move: (%1, %2)</h1></center>")
            .arg(ev -> x()).arg(ev -> y());
    //this->setText(text);
}

void mylabel::enterEvent(QEvent *e)
{
    QString text = QString("<center><h1>Mouse enter</h1></center>");
    this->setText(text);
}

void mylabel::leaveEvent(QEvent *e)
{
    QString text = QString("<center><h1>Mouse leave</h1></center>");
    this->setText(text);
}
#ifndef MYWIDGET_H
#define MYWIDGET_H

#include <QWidget>

namespace Ui {
class MyWidget;
}

class MyWidget : public QWidget
{
    Q_OBJECT

public:
    explicit MyWidget(QWidget *parent = 0);
    ~MyWidget();

protected:
    void keyPressEvent(QKeyEvent *event);
    void timerEvent(QTimerEvent *event);
    void closeEvent(QCloseEvent *event);

    bool eventFilter(QObject *watched, QEvent *event);
private:
    Ui::MyWidget *ui;
    int timerId;
    int timerId2;
};

#endif // MYWIDGET_H
#include "mywidget.h"
#include "ui_mywidget.h"
#include <QKeyEvent>
#include <QDebug>
#include <QMessageBox>
MyWidget::MyWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::MyWidget)
{
    ui->setupUi(this);
    timerId = this->startTimer(1000);
    timerId2 = this->startTimer(500);

    connect(ui->pushButton,&MyButton::clicked,
            [=]()
    {
        qDebug()<<"press now";
    });

    ui->label_2->installEventFilter(this);
}

void MyWidget::keyPressEvent(QKeyEvent *event)
{
    qDebug()<<(char)(event->key());
    if (event->key() == Qt::Key_A)
    {
        qDebug()<<"Qt::Key_A";
    }

}

void MyWidget::timerEvent(QTimerEvent *event)
{
    if(event->timerId() == this->timerId)
    {
        static int sec = 0;
        ui->label->setText(QString("<center><h1>Timer: (%1)</h1></center>")
                           .arg(sec++));
        if(5==sec)
        {
            this->killTimer(timerId);
        }
    }
    else if(event->timerId() == this->timerId2)
    {
        static int sec = 0;
        ui->label_2->setText(QString("<center><h1>Timer: (%1)</h1></center>")
                           .arg(sec++));


    }
}

void MyWidget::closeEvent(QCloseEvent *event)
{
    int ret = QMessageBox::question(this,"question","close it?");
    if (ret == QMessageBox::Yes)
    {
        event->accept();
    }
    else
    {
        event->ignore();
    }
}

bool MyWidget::eventFilter(QObject *watched, QEvent *event)
{
    if(watched == ui->label_2)
    {
        QMouseEvent *env = static_cast<QMouseEvent*>(event);
        if(event->type() == QEvent::MouseMove)
        {
            ui->label_2->setText(QString("Mouse move:(%1, %2)").arg(env->x()).arg(env->y()));
            return true;
        }
    }
    else
    {
        return QWidget::eventFilter(watched,event);
    }
}

MyWidget::~MyWidget()
{
    delete ui;
}
#include "mywidget.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MyWidget w;
    w.show();

    return a.exec();
}

 

以上是关于Qt笔记——Event的主要内容,如果未能解决你的问题,请参考以下文章

Qt笔记

js经常用到的代码片段

QT 实用代码片段

QT学习笔记6

Qt笔记(十七)之鼠标滚轮事件方向

[未解决问题记录]python asyncio+aiohttp出现Exception ignored:RuntimeError('Event loop is closed')(代码片段