Qt中的鼠标跟踪

时间:2014-07-10 17:14:49

标签: c++ qt

我正在实现一个代码,当我按下线按钮时,我可以跟踪点以获得一条线。但是为了再次追踪一条线,我需要再次按下按钮。我希望在按下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();

   }

1 个答案:

答案 0 :(得分:0)

  

注意:由于某种原因,setMouseTracking (true);要求按下鼠标来捕获坐标。

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

当按下鼠标时,此“Thread”应该是一个布尔变量,可变为true。如果按下Esc设置false(当窗口处于非活动状态或失去焦点时,您可以创建另一个布尔变量来阻止事件,窗口重新获得焦点事件恢复工作)。

在“线程”中,您创建了一个无限循环,如:

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