0

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!

Diego
  • 35
  • 7
  • Are you sure it's your `lblMouse` label what is displayed? I have doubt about it. Please post your *.ui* file. – Tomas May 11 '16 at 05:44
  • Hello @Thomas, thanks for your repply. – Diego May 11 '16 at 06:55
  • Hello @Thomas. Thanks for your reply! (and sorry for my untidiness, I have bad edited my last answer). I have added the .ui to the main thread and information about a runtime error message. – Diego May 11 '16 at 07:15
  • Your instance of ui->lblMouse is not instance of lblMouse – Apin May 11 '16 at 07:18
  • You have `lblMouse` both as class name and as name of the QLabel instance, this might be confusing. Usually class names start with uppercase, so you can tell them apart better. Then change your ui file to instantiate a LblMouse instead of QLabel. – Karsten Koop May 11 '16 at 07:22

2 Answers2

1

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.
Shtol Krakov
  • 1,260
  • 9
  • 20
  • BTW, you can even do this in the Designer. It's called "use this as placeholder for user defined class" or similar. – Karsten Koop May 11 '16 at 07:24
  • Hello! Thanks for your reply! I cannot edit manually the .ui file from QtCreator (is only readable file, created by the Form creator of QtCreator), and I doesn't know how to change the class. Is very usefull your comment to stand what is happening – Diego May 12 '16 at 02:55
0

You just need to use your lblMouse class.

In .h file, add this in class testWindow

lblMouse *lblMouse_Current_Pos;

In .cpp file, add this in contructor:

lblMouse_Current_Pos = new lblMouse(this);      
lblMouse_Current_Pos->setMouseTracking(true);
connect(lblMouse_Current_Pos, SIGNAL(Mouse_Pos()), this, SLOT(Mouse_current_pos()));
connect(lblMouse_Current_Pos, SIGNAL(Mouse_Pressed()), this, SLOT(Mouse_Pressed()));
connect(lblMouse_Current_Pos, SIGNAL(Mouse_Left()), this, SLOT(Mouse_left()));
GAVD
  • 1,977
  • 3
  • 22
  • 40
  • Hello @GAVD , very thanks for your comment! Effectively, if I do what you said in the first lines, I got a new lblMouse and the constructor works (the initial text is "Hello"), but I have created one from the graphic editor and the properties are different, by that, appears one aditional lblMouse in my ui, and is not what I want to do. Now I stand that, first, will assign the object in my ui to a lblMouse (in my ui, is a QLabel), how can I do it? Thanks! – Diego May 12 '16 at 03:03
  • OK, you want to keep your label which you already created. Firstly, you create class lblMouse (with .h file and .cpp file). After that, you go to Design, right-click on your label and then, "Promote to", choose new class "lblMouse". – GAVD May 12 '16 at 03:11