@Pl45m4 Problem solved. Thanks to everyone who contributed. I am going to post below the final version of the source code that contains all of the changes that occurred as a result of this topic-thread. Perhaps this example will be useful to another first time user of Qt Signals/Slots.
main.cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
MainWindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
QT_BEGIN_NAMESPACE
namespace Ui {
class MainWindow;
}
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
MainWindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "myclassa.h"
#include "myclassb.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
connect(ui->myView, &MyClassB::mySignal, ui->myLabel, &MyClassA::setPoint);
}
MainWindow::~MainWindow()
{
delete ui;
}
myClassA.h
#ifndef MYCLASSA_H
#define MYCLASSA_H
#include <QLabel>
#include <QObject>
#include <QPoint>
#include <QWidget>
class MyClassA : public QLabel
{
Q_OBJECT
public:
MyClassA(QWidget *parent = nullptr);
~MyClassA();
public slots:
void setPoint (QPoint point);
private:
QPoint p;
};
#endif // MYCLASSA_H
MyClassA.cpp
#include <QDebug>
#include <QLabel>
#include <QPoint>
#include "myclassA.h"
MyClassA::MyClassA(QWidget *parent)
: QLabel(parent)
{
}
MyClassA::~MyClassA()
{
}
void MyClassA::setPoint (QPoint point)
{
qDebug("MyClassA::setPoint");
p = point;
}
MyClassB.h
#ifndef MYCLASSB_H
#define MYCLASSB_H
#include <QChartView>
#include <QMouseEvent>
#include <QObject>
#include <QPoint>
#include <QWidget>
class MyClassB : public QChartView
{
Q_OBJECT
public:
MyClassB(QWidget *parent = nullptr);
~MyClassB();
signals:
void mySignal(QPoint p);
protected:
void mousePressEvent(QMouseEvent *event) override;
};
#endif // MYCLASSB_H
MyClassB.cpp
#include <QChartView>
#include <QMouseEvent>
#include <QWidget>
#include "myclassB.h"
MyClassB::MyClassB(QWidget *parent)
: QChartView(parent)
{
}
MyClassB::~MyClassB()
{
}
void MyClassB::mousePressEvent(QMouseEvent* event)
{
qDebug("MyClassB::mousePressEvent");
QPoint pos = event->pos();
emit mySignal(pos);
QChartView::mousePressEvent(event); /* pass-on the event to the base class */
}
MainWindow.ui
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<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="MyClassB" name="myView">
<property name="geometry">
<rect>
<x>160</x>
<y>90</y>
<width>321</width>
<height>281</height>
</rect>
</property>
</widget>
<widget class="MyClassA" name="myLabel">
<property name="geometry">
<rect>
<x>570</x>
<y>190</y>
<width>121</width>
<height>20</height>
</rect>
</property>
<property name="text">
<string>TextLabel</string>
</property>
</widget>
</widget>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>26</height>
</rect>
</property>
</widget>
<widget class="QStatusBar" name="statusbar"/>
</widget>
<customwidgets>
<customwidget>
<class>MyClassB</class>
<extends>QGraphicsView</extends>
<header>myclassb.h</header>
</customwidget>
<customwidget>
<class>MyClassA</class>
<extends>QLabel</extends>
<header location="global">myclassa.h</header>
</customwidget>
</customwidgets>
<resources/>
<connections/>
</ui>