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
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