Qt中的鼠标跟踪

Mouse tracking in Qt

本文关键字:跟踪 鼠标 Qt      更新时间:2023-10-16

我正在实现一个代码,其中当我按线按钮时,我可以跟踪点以获得一条线。但是为了再次跟踪一条线,我需要再次按下按钮。我想直到Esc键被按下相同的信号必须保持激活。我不想一次又一次地按按钮。代码片段如下:mainwindow.cpp

    connect(ui->lineButton, SIGNAL(clicked()),this, SLOT(drawLine()));
void MainWindow::drawLine(){
    ui->graphicsView->setScene(scene);
    line *item = new line;
    scene->addItem(item);
    //connect(item,SIGNAL(drawFinished()),SLOT(drawLine()));
    qDebug() << "Line Created";

}

line.cpp

    void line::mousePressEvent(QGraphicsSceneMouseEvent* e){
       if(e->button()==Qt::LeftButton) {
           if(mFirstClick){
               x1 = e->pos().x();
               y1 = e->pos().y();
               mFirstClick = false;
               mSecondClick = true;
           }
           else if(!mFirstClick && mSecondClick){
               x2 = e->pos().x();
               y2 = e->pos().y();
               mPaintFlag = true;
               mSecondClick = false;
               update();
           }
       }
       QGraphicsItem::mousePressEvent(e);
       update();
   }

注意:由于某些原因,setMouseTracking (true);需要按下鼠标来获取坐标。

当鼠标被按下时,它不会一直工作,你可以创建一个单独的线程一直运行。

这个"线程"应该是一个布尔变量,当鼠标被按下时该变量为true。如果按下Esc,则设置false(当窗口不活动或失去焦点时,您可以创建另一个布尔变量来阻止事件和窗口以重新获得焦点事件返回工作)。

在"Thread"中,创建一个无限循环,类似于:

class myCapture: QThread {
   bool capture;
   ...
}
while(1) {
   if(capture) {
   }
}

和使用QEvent,应检测鼠标的位置

使用示例:

trackmouse.h:

#ifndef TRACKMOUSE_H
#define TRACKMOUSE_H
#include <QThread>
#include <QPoint>
class trackMouse : public QThread
{
    Q_OBJECT
public:
    explicit trackMouse(QObject *parent = 0);
    void enable(const bool enable = true);
    void detectMove(const bool enable = true);
    void setDelay(const int value);
    void end();
protected:
    virtual void run();
private:
    int delay;
    bool track;
    bool running;
    bool lastPosActive;
signals:
    void mousePos(const QPoint pos);
};
#endif // TRACKMOUSE_H

trackmouse.cpp:

#include "trackmouse.h"
#include <QCursor>
#include <QDebug>
trackMouse::trackMouse(QObject *parent) :
    QThread(parent),
    delay(10),
    track(false),
    running(true),
    lastPosActive(false)
{
}
void trackMouse::end() {
    running = false;
    wait();
    terminate();
}
void trackMouse::enable(const bool enable) {
    track = enable;
}
void trackMouse::detectMove(const bool enable) {
    lastPosActive = enable;
}
void trackMouse::setDelay(const int value) {
    delay = value;
}
void trackMouse::run() {
    QPoint lastPos;
    QPoint currentPost;
    while(running) {
        QThread::msleep(delay);
        currentPost = QCursor::pos();
        if (track == true && (lastPosActive == false || lastPos != currentPost)) {
            lastPos = currentPost;
            emit mousePos(currentPost);
        }
    }
}

配置mainWindow.cpp:

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QMouseEvent>
#include <QKeyEvent>
#include <QCloseEvent>
#include <QDebug>
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    tt = new trackMouse(this);
    //tt->setDelay(1000); // 1 second
    tt->detectMove(true); //If true, will only "Track" when the mouse moves.
    QObject::connect(tt, SIGNAL(mousePos(QPoint)), this, SLOT(capture(QPoint)));
    tt->start();
}
void MainWindow::mousePressEvent(QMouseEvent *event)
{
    qDebug() << "enable mouse track";
    tt->enable();//enable track on click
}
void MainWindow::keyPressEvent(QKeyEvent *event)
{
    if(event->key() == Qt::Key_Escape) {
        qDebug() << "disable mouse track";
        tt->enable(false);
    }
}
void MainWindow::capture(const QPoint pos){
    qDebug() << "X:" << pos.x() << " | Y: " << pos.y();
}
void MainWindow::closeEvent(QCloseEvent *event) {
    tt->end();
}
MainWindow::~MainWindow()
{
    delete ui;
}

mainwindow.h:

#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include "trackmouse.h"
#include <QMainWindow>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
    Q_OBJECT
public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
protected:
    void mousePressEvent(QMouseEvent *event);
    void keyPressEvent(QKeyEvent *event);
    void closeEvent(QCloseEvent *event);
private:
    Ui::MainWindow *ui;
    trackMouse *tt;
private slots:
    void capture(const QPoint pos);
};
#endif // MAINWINDOW_H