码迷,mamicode.com
首页 > 其他好文 > 详细

Qt笔记——Event

时间:2018-04-06 11:01:33      阅读:218      评论:0      收藏:0      [点我收藏+]

标签:img   qwidget   star   track   har   lse   ==   install   new   

#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

标签:img   qwidget   star   track   har   lse   ==   install   new   

原文地址:https://www.cnblogs.com/dalanjing/p/8724910.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!