PDA

View Full Version : Subclassing QLabel



Ishtar_UK
18th May 2011, 19:00
Hello All

I am attempting to subclass QLabel in order to handle mouse click events. The x and y values of the mouse click will eventually be passed to QRubberBand() for further processing. Naturally, the QRubberBand will be in a separate class to the sub-classed QLabel.

My main project is actually using QtDesigner to build the GUI but to make this simpler I have rewritten the code in question without Designer and without the none relevant code from my main project.

The following code snippet compiles and works perfectly but how do I get the variables x and y from MyLabel() class into Communicate()?

HEADER FILE CALLED: communicate.h




// communicate.h

#ifndef COMMUNICATE_H
#define COMMUNICATE_H

#include <QApplication>
#include <QWidget>
#include <QApplication>
#include <QMouseEvent>
#include <QLabel>
#include <iostream>

class Communicate : public QWidget
{
Q_OBJECT
public:
Communicate(QWidget *parent = 0);

private:

};

class MyLabel : public QLabel
{
Q_OBJECT
public:
MyLabel(QWidget *parent = 0);
void mousePressEvent(QMouseEvent *event);

private:

};

#endif /* COMMUNICATE_H */



SOURCE FILE CALLED: communicate.cpp



//communicate.cpp

#include "communicate.h"

Communicate::Communicate(QWidget* parent)
: QWidget(parent)
{
int WIDTH = 400;
int HEIGHT = 400;

resize(WIDTH, HEIGHT);

MyLabel * label = new MyLabel(this);
label->setGeometry(20,20, 100, 50);
label->setText("CLICK AREA");
label->show();
}

MyLabel::MyLabel(QWidget* parent)
: QLabel(parent)
{
}

void MyLabel::mousePressEvent(QMouseEvent* event){
int x,y;

x = event->pos().x();
y = event->pos().y();

/* ***DEBUGGING***
Check x and y are correct after mouse click on sub-classed label */

std::cerr << "\nX: " << x;
std::cerr << "\nY: " << y;

return;
}


FINALLY: main.cpp



#include "communicate.h"

int main(int argc, char *argv[])
{
QApplication app(argc, argv);

Communicate w;
w.setWindowTitle("Communication Test");
w.show();

return app.exec();
}


As I said, I would like to get the variables x and y from MyLabel into the scope of the Communicate class. Any help would be well received.

Regards
Niola

Zlatomir
18th May 2011, 19:16
You can create a signal that emits the x and y values directly from the mousePressEvent overload, or store them as a member of the class MyLabel and access when you will need them, there are many ways you can achieve that.

Santosh Reddy
18th May 2011, 19:20
You should be able to use signal-slot cmmunication, have a singal in MyLabel which is emited inside mousePressEvent(), and connect it to a slot in Communicate class, then process x & y in the slot. (you need to have x & y as parametes for both the signal & slot)

This should be fairly strightforward, looks like you have some constraints, which are mentioned in the question.

Ishtar_UK
18th May 2011, 21:02
Hello Santosh Reddy & Zlatomir

Thank you for the responses thus far.

Unfortunately I am quite new to Qt Signals / Slots outside of what I normally do. (for example linking buttons to functions.) I have googled lots and read basic tutorials on Signals and Slots but am still unsure of how to implement this.

Would you be so good as to help with some code fitting my example?

Many Thanks
Nicola

Zlatomir
18th May 2011, 21:16
You can do this without signal and slot connections - just C++ code: store x and y as members is MyLabel and code a getter function (or maybe put them public - whatever is appropriate)

To use signals and slots you declare a member signal in your class (you don't implement the signal):


class MyLabel : public QLabel
{
Q_OBJECT
signals:
void mySignal(int x, int y);
//...
};

And then after you get the x and y you can emit the signal:


//...
x = event->pos().x();
y = event->pos().y();

emit mySignal(x, y);

And in your Communicate class code a slot (just like a member function, only declared with public slots: access specifier) that take two integers as parameter.

And in the Communicate constructor (after you create MyLabel object) connect the your signal with the slot:


//...
connect(label, SIGNAL(mySignal(int, int), this, SLOT(WhatEverSlotNameYouChose(int, int)));

Read more about signals and slots here (http://doc.qt.nokia.com/4.7/signalsandslots.html)

Santosh Reddy
18th May 2011, 21:20
I tried to modify your code and added the signal and slots required, have a look at it, also I recommed to have look at the signal-slot examples to help your self, in figuring out how to use them effectively.

communicate.h


#ifndef COMMUNICATE_H
#define COMMUNICATE_H

#include <QApplication>
#include <QWidget>
#include <QApplication>
#include <QMouseEvent>
#include <QLabel>
#include <iostream>

class Communicate : public QWidget
{
Q_OBJECT
public:
Communicate(QWidget *parent = 0);

// Added
public slots:
void newMousePos(int x, int y);

private:

};

class MyLabel : public QLabel
{
Q_OBJECT
public:
MyLabel(QWidget *parent = 0);
void mousePressEvent(QMouseEvent *event);

// Added
signals:
void selected(int x, int y);

private:

};

#endif /* COMMUNICATE_H */


communicate.cpp


//communicate.cpp

#include "communicate.h"

Communicate::Communicate(QWidget* parent)
: QWidget(parent)
{
int WIDTH = 400;
int HEIGHT = 400;

resize(WIDTH, HEIGHT);

MyLabel * label = new MyLabel(this);
label->setGeometry(20,20, 100, 50);
label->setText("CLICK AREA");
label->show();

// Added
connect(label, SIGNAL(selected(int , y), this, newMousePos(int, int));
}

// Added
void Communicate::newMousePos(int x, int y);
{
cout << x;
cout << y;
}

MyLabel::MyLabel(QWidget* parent)
: QLabel(parent)
{
}

void MyLabel::mousePressEvent(QMouseEvent* event){
int x,y;

x = event->pos().x();
y = event->pos().y();

/* ***DEBUGGING***
Check x and y are correct after mouse click on sub-classed label */

std::cerr << "\nX: " << x;
std::cerr << "\nY: " << y;

emit selected(x, y);
return;
}

main.cpp


#include "communicate.h"

int main(int argc, char *argv[])
{
QApplication app(argc, argv);

Communicate w;
w.setWindowTitle("Communication Test");
w.show();

return app.exec();
}


looks like we are wokring in parallel

Ishtar_UK
18th May 2011, 21:55
A big thank you to everyone...

I now have it working and have learned quite a bit too :D