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>
{
Q_OBJECT
public:
// Added
public slots:
void newMousePos(int x, int y);
private:
};
{
Q_OBJECT
public:
// Added
signals:
void selected(int x, int y);
private:
};
#endif /* 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 */
To copy to clipboard, switch view to plain text mode
communicate.cpp
//communicate.cpp
#include "communicate.h"
Communicate
::Communicate(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;
}
{
}
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;
}
//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;
}
To copy to clipboard, switch view to plain text mode
main.cpp
#include "communicate.h"
int main(int argc, char *argv[])
{
Communicate w;
w.setWindowTitle("Communication Test");
w.show();
return app.exec();
}
#include "communicate.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
Communicate w;
w.setWindowTitle("Communication Test");
w.show();
return app.exec();
}
To copy to clipboard, switch view to plain text mode
looks like we are wokring in parallel
Bookmarks