c++ – How to track mouse coordinates in a QLabel?

Question:

Hi, I have a project with screens using Qt Creator and I am stuck with the following problem: I want to track the mouse coordinates when moving the cursor over a QLabel. I saw in some other topics some solved cases, but they always had to create a Class manually for the QLabel, as I used the Qt creator editor, so it was more difficult to change the QLabel class. Below is the method that would enable mouse tracking on the screen.

void mainwindow::on_ButtonAddFileira_clicked(){

    this->pTimer.disconnect();
    QImage image;

    image.load("C:/Users/Syn/Desktop/TCC/Fotos Bolsão FEI/02 Bolsao_Cheio_Foreground.jpg");

    //image = MatToQImage(this->ImagemBase.capturaImgBuffer());
    ui->labelScreen->setPixmap(QPixmap::fromImage(image));
    ui->labelScreen->setScaledContents(true);

    ui->labelScreen->setMouseTracking(true);

}

The idea is that when I click on a button, mouse tracking is activated and I can identify its x,y coordinates in the qLabel image. Below is a method that might help, but I'm not sure how to use it:

bool mainwindow::eventFilter(QObject *obj, QEvent *event){

if (qobject_cast<QLabel*>(obj)== ui->labelScreen && event->type() == QEvent::MouseMove)
  {
    QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event);
    qDebug() << QString("Mouse move (%1,%2)").arg(mouseEvent->pos().x()).arg(mouseEvent->pos().y());

    ui->label_X->setText(QString().arg(mouseEvent->pos().x()));
    ui->label_Y->setText(QString().arg(mouseEvent->pos().y()));

  }

  return false;
}

Does anyone know how I can do this with the fixed QLabel generated by the editor, ie without creating a separate class for the Label? Thank you very much in advance.

Answer:

It is possible to retrieve the global position of the cursor on the screen using the static method QCursor::pos() , as you want to get the position inside the label you can use the method QWidget::mapFromGlobal() .

QPoint cursorPos = ui->label->mapFromGlobal( QCursor::pos() );

If the x or y coordinates are negative or exceed the height or width of the label, it means that the mouse is positioned outside the image.

From your question I also understand that you need to keep the information constantly updated after a certain event. As you mentioned that it is not feasible to extend the QLabel class, I believe that the solution is to use a QTimer with a short interval (eg 100 ms, 50 ms) and always retrieve the information after this interval.

void mainwindow::on_ButtonAddFileira_clicked(){
    // carrega imagem
    QTimer *cursorTimer = new QTimer;
    connect(cursorTimer, SIGNAL(timeout()), SLOT(cursor_timer_timeout()));
    cursorTimer->start(100);
}

Note: The code above is an example, remember to release the timer when it is no longer needed.

Another possible solution is to use the Qt Creator editor promote function, it is still necessary to extend the QLabel class but this way Qt Creator can understand the change that was made and adjust accordingly. Start by creating a class that inherits from QLabel .

class MyImageLabel : public QLabel
{
    // ...
    void mouseMoveEvent(QMouseEvent *e);
};

Now just open your window in the editor, right-click the label and click Promote to... , fill the Promoted class name field with your class name and click Add , select it from the list above and click Promote . Now just override the QLabel::mouseMoveEvent() in your new class, something like:

void MyImageLabel::mouseMoveEvent(QMouseEvent *e)
{
    qDebug() << e->pos();
    return QLabel::mouseMoveEvent(e);
}

This way the mouse position can be retrieved in real time using the QMouseEvent::pos() .

Read more in Using Custom Widgets with Qt Designer .

Scroll to Top
AllEscort