Search code examples
c++qtqlabelqmouseevent

Qt C++ QLabel Clickable mouse events doesn't work


I have a big doubt and problem. After try a lot of tutorials and examples, I cannot receive signal clicks in my QLabel. If I do the same, but in a QDialog (not in a QLabel), I can know the status of the mouse.

I paste my example code, before that, I present my steps to make the project:

I make a project, make a QMainWindow (named testWindow, maked graphically), after that, I add a QLabel (lblMouse), I mark the mouseTracking property as true. After execute, my QLabel lblMouse doesn't react to my mouse events.

Another doubt is: after execute my program, the text of my QLabel is not "Hello" as I assign in his constructor, it will be executed after ui is executed? I can change it from the constructor of the ui with ui->lblMouse->setText("Hello"); (Probably I have an error, I'm a C programmer and I'm trying to enter in the C++ world)

After that, I edit the code (testWindow.cpp and testWindow.h):

Here is my cpp code:

testWindow::testWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::testWindow)
{
    ui->setupUi(this);

//It will not be neccesary because I mark on the checkbox of mouseTracking
    ui->lblMouse->setMouseTracking(true);

    connect(ui->lblMouse, SIGNAL(Mouse_Pos()), this, SLOT(Mouse_current_pos()));
    connect(ui->lblMouse, SIGNAL(Mouse_Pressed()), this, SLOT(Mouse_Pressed()));
    connect(ui->lblMouse, SIGNAL(Mouse_Left()), this, SLOT(Mouse_left()));
}

testWindow::~testWindow()
{
    delete ui;
}

void testWindow::Mouse_current_pos()
{
    ui->lblMouse_Current_Pos->setText(QString("X = aa, Y = aa")/*.arg(ui->lblMouse->x())*/);
}

void testWindow::Mouse_Pressed()
{
    ui->lblMouse_Current_Pos->setText(QString("X = aa, Y = aa")/*.arg(ui->lblMouse->x())*/);

}

void testWindow::Mouse_left()
{
    ui->lblMouse_Current_Pos->setText(QString("X = aa, Y = aa")/*.arg(ui->lblMouse->x())*/);

}

lblMouse::lblMouse(QWidget *parent): QLabel(parent)
{
// strange for me, the initial text of the label is not Hello
    this->setText("Hello");
}

lblMouse::~lblMouse()
{

}

void lblMouse::mouseMoveEvent(QMouseEvent *ev)
{
    this->xpos = ev->x();
    this->ypos = ev->y();

    emit Mouse_Pos();
}

void lblMouse::mousePressEvent(QMouseEvent *)
{
    emit Mouse_Pressed();
//    ev->x();
}

void lblMouse::leaveEvent(QEvent *)
{
    emit Mouse_Left();
}

Here is my h file:

#ifndef TESTWINDOW_H
#define TESTWINDOW_H

#include <QMainWindow>
#include <QLabel>
#include <QMouseEvent>

#include <QEvent>
#include <QDebug>

namespace Ui {
class testWindow;
class lblMouse;
}


class lblMouse : public QLabel
{
    Q_OBJECT
public:
    explicit lblMouse(QWidget *parent = 0);
    ~lblMouse();
    int xpos,ypos;
    void leaveEvent(QEvent *);

protected:
    void mouseMoveEvent(QMouseEvent *ev);
    void mousePressEvent(QMouseEvent *ev);

signals:
    void Mouse_Pressed();
    void Mouse_Pos();
    void Mouse_Left();

};


class testWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit testWindow(QWidget *parent = 0);
    ~testWindow();

private:
    Ui::testWindow *ui;


private slots:
    void Mouse_current_pos();
    void Mouse_Pressed();
    void Mouse_left();
};

#endif // TESTWINDOW_H

Also, here is my .ui file (thanks @Thomas ):

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>testWindow</class>
 <widget class="QMainWindow" name="testWindow">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>800</width>
    <height>600</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="centralwidget">
   <widget class="QLabel" name="lblMouse">
    <property name="geometry">
     <rect>
      <x>20</x>
      <y>60</y>
      <width>331</width>
      <height>241</height>
     </rect>
    </property>
    <property name="mouseTracking">
     <bool>true</bool>
    </property>
    <property name="frameShape">
     <enum>QFrame::Box</enum>
    </property>
    <property name="text">
     <string>Mouse Area</string>
    </property>
    <property name="alignment">
     <set>Qt::AlignCenter</set>
    </property>
   </widget>
   <widget class="QGroupBox" name="groupBox">
    <property name="geometry">
     <rect>
      <x>400</x>
      <y>50</y>
      <width>201</width>
      <height>191</height>
     </rect>
    </property>
    <property name="title">
     <string>Mouse Events</string>
    </property>
    <layout class="QVBoxLayout" name="verticalLayout">
     <item>
      <widget class="QLabel" name="lblMouse_Current_Pos">
       <property name="frameShape">
        <enum>QFrame::Box</enum>
       </property>
       <property name="frameShadow">
        <enum>QFrame::Raised</enum>
       </property>
       <property name="text">
        <string>x=0, Y=0</string>
       </property>
      </widget>
     </item>
     <item>
      <widget class="QLabel" name="lblMouse_Current_Event">
       <property name="frameShape">
        <enum>QFrame::Panel</enum>
       </property>
       <property name="frameShadow">
        <enum>QFrame::Raised</enum>
       </property>
       <property name="text">
        <string>TextLabel</string>
       </property>
      </widget>
     </item>
    </layout>
   </widget>
  </widget>
  <widget class="QMenuBar" name="menubar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>800</width>
     <height>23</height>
    </rect>
   </property>
   <widget class="QMenu" name="menuMenu">
    <property name="title">
     <string>&amp;Menu</string>
    </property>
    <addaction name="actionImportar"/>
   </widget>
   <addaction name="menuMenu"/>
  </widget>
  <widget class="QStatusBar" name="statusbar"/>
  <action name="actionImportar">
   <property name="text">
    <string>&amp;Importar...</string>
   </property>
  </action>
 </widget>
 <resources/>
 <connections/>
</ui>

Edit:

I'm seing a runtime error in the console (is not a compilation error, I got this messages after run the program, but it continues running):

  • QObject::connect: No such signal QLabel::Mouse_Left() in ../testwindow.cpp:17
  • QObject::connect: (sender name: 'lblMouse')
  • QObject::connect: (receiver name: 'testWindow')

PD: I'm not sure if it is the problem. I have tested other ways to do this and I have not good results.

Thanks!


Solution

  • Your label in ui is QLabel class, but you have created lblMouse. So, in ui you must change code

    <widget class="QLabel" name="lblMouse">
    

    to

    <widget class="lblMouse" name="lblMouse">
    

    EDIT:

    To change this you can:

    1. Use any text editor;
    2. Go to Designer in Qt Creator chosing your ui, select your QLabel, call context menu and click "Promote to...". Then check that "Base class name" is correct (QLabel), write your class (lblMouse) in "Promoted class name" field, click "Add" button, then "Promote" button. That's all. Your label now your own label class.

    About setText() method.

    1. Go to designer;
    2. Choose your label;
    3. In right side in object propeties find QLabel area and click on a round arrow front of "text" property. That's all. Now, if you do setText() method in constructor - it will works.