How to track mouse coordinates on a Qlabel?

Asked

Viewed 185 times

3

Hello, I’m with a screen project using Qt Creator and I’m stuck with the following problem: I want to track the mouse coordinates by moving the cursor over a Qlabel. I saw in some other topics some cases solved, but always had to create a Class manually for Qlabel, as I used the Qt editor Creator so it became more difficult to change the Qlabel class. Below is the method that would activate the 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 you click a button, the mouse tracking is activated and I can identify your x,y coordinates in the qLabel image. Below a method that may 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;
}

Would anyone know how to do that with the editor-generated fixed Qlabel, i.e., without creating a separate Label class? Thanks in advance.

1 answer

1


It is possible to recover the global cursor position on the screen using the static method QCursor::pos(), how 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 are exceeding the height or width of the label it means that the mouse is positioned outside the image.

By your question I also understood that you need to keep the information updated constantly after a certain event. As you mentioned it is not feasible to extend the class QLabel I believe the solution is to use a QTimer with a short interval (e.g. 100 ms, 50 ms) and retrieve the information always 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 function promote from the Qt Creator editor, it is still necessary to extend the class QLabel but this way Qt Creator can understand the change that has been made and adjust correctly. Start by creating a class that you inherit from QLabel.

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

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

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

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

Read more on Using Custom Widgets with Qt Designer.

  • First thank you for the answer, but now after creating the above class and calling the method 'Qmouseevent::pos()', I get 2 error messages, they are they: no matching Function for call to 'Myimagelabel::Myimagelabel(Qwidget*&)' myimage = new Myimagelabel(mainwindow); and the other is _cannot call Member Function 'Qpoint Qmouseevent::pos() const' without Object _ would know what is causing this error?

  • Make sure you have correctly declared the class constructor, it should receive a pointer to QWidget and call the parent class constructor by forwarding this pointer. You can also create the class by Qt Creator by going to File -> New File or Project -> C++ -> C++ Class. Use QLabel in the field Base class and QWidget in the field Type information.

  • Valew friend, I tried the first way with Qtimer and it worked, by the subclass method I tried but my compiler insists on error in the constructor, although I believe I have declared everything correctly. But thanks for your help.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.