Signals and Slots over multiple windows
-
Hello,
I have set up a view- model type application with multiple windows ; I have set up the appropriate signals, I don't know how to connect the signals to the slots over my class hierarchy which is in 3 levelsMainWindow has _*plotWindow, *_has actual plot code, using qCustomPlot
MainWindow has server has ListenWriteThread creates socket;
_*ListenWriteThread*_ creates _*dispData*_
The dispData emits signals whenever it detects peaks in the incoming data.
How do I connect it to the mainwindow/plotwindow for the actual graph plotting?
Would it work something like:
connect &peakData, setData() to this->Parent, plotData()?
where is the connection made exactly? In the mainwindow?I have only got the hang of button click to launch activity connections so far :(
My readready signal and corresponding slot are in the same class.@#ifndef DISPDATA_H
#define DISPDATA_H
#include <QObject>
#include <vector>
class dispData : public QObject
{
Q_OBJECT
public:
dispData(QObject* parent);
void setPeakDepth(int val) {peakDepth = val;}
void setPeakRecoil(int val){peakRecoil = val;}
void setCompressionRate(int val){compressionRate = val;}
void setStartIndex(int val){startIndex = val;}
void addData(int val, int index);
signals:
void setPeakDepth(int val) {peakDepth = val;}
void setPeakRecoil(int val){peakRecoil = val;}
void setCompressionRate(int val){compressionRate = val;}private:
int startIndex;
int value;
int peakDepth; //UPDATE VALUE EMITS SIGNAL
int peakRecoil;//UPDATE VALUE EMITS SIGNAL
int compressionCount; //UPDATE VALUE EMITS SIGNAL
int compressionRate;
std::vector<int> rawReads;
std::vector<int> rawIndex;};
#endif // DISPDATA_H
#ifndef LISTENWRITETHREAD_H
#define LISTENWRITETHREAD_H
#include <QThread>
#include <QTcpSocket>
#include "dispdata.h"
class dispData;class listenWriteThread : public QThread
{
Q_OBJECTpublic:
listenWriteThread(int socketDescriptor, const QString &fortune, QObject *parent);void run();
public slots:
void analyzeThis();
signals:
void error(QTcpSocket::SocketError socketError);private:
QTcpSocket* tcpSocket;
int socketDescriptor;
QString text;
dispData* rawData;};
#ifndef MAINWINDOW_H
#define MAINWINDOW_H#include <QMainWindow>
#include <QtWidgets>
#include "dynamicplots.h"
#include "cprServer.h"
namespace Ui {
class MainWindow;
}
class dynamicPlots;
class MainWindow : public QMainWindow
{
Q_OBJECTpublic:
explicit MainWindow(QWidget parent = 0);
~MainWindow();
void setPlotWindow(dynamicPlots dp);private slots:
void on_quitButton_clicked();void on_pbFiles_clicked(); void plotDepth(int val); void plotRecoil(int rec); void plotRate(int rate);
private:
void setUpListener();
dynamicPlots *dPlot; //SIGNAL HAS TO BE PROCESSED HERE
Ui::MainWindow *ui;
cprServer server;
};@ -
Supposing you are creating your MainWindow and dispData objects in your main(), you could connect like this:
@int main(){
/* (...) other code you may have */
QMainWindow * mainWnd = new QMainWindow;
dispData * dispObj = new dispData;connect(dispObj, SIGNAL( setPeakDepth(int) ), mainWnd, SLOT(dPlot->YOUR_SLOT_FUNCTION_HERE(int) )); connect(dispObj, SIGNAL( setPeakRecoil(int) ), mainWnd, SLOT(dPlot->YOUR_SLOT_FUNCTION_HERE(int) )); connect(dispObj, SIGNAL( setCompressionRate(int) ), mainWnd, SLOT(dPlot->YOUR_SLOT_FUNCTION_HERE(int) ));
/* (...) other code you may have */
}@
You didn't provide the dynamicPlots class slots so I just wrote your signal to slot linking code, but you have to provide the real slot method's names. Please note that the int in YOUR_SLOT_FUNCTION_HERE(int) is needed if you want to pass the signal int argument to the slot function, but you may omit it if you don't need it.Unfortunately, your code seems to have an issue: all signals cannot have any body! Signals are just like function prototypes. Slot functions are the ones that have body. So your declaration above:
@signals:
void setPeakDepth(int val) {peakDepth = val;}
void setPeakRecoil(int val){peakRecoil = val;}
void setCompressionRate(int val){compressionRate = val;}
@
is not valid and produces the MOC error: Not a signal declaration. Remove the signal body and instead put any code to execute for that signal inside the slots function's body.