Implementing c++ Callback using Qt Signal and Slot - c++

I have created a library which has following interface and callback.
I wanted to Implent it using QT Signal and Slot.
Is is possible to replace IWifiCameraDiscovery with Signal and slot
class IWifiCameraDiscovery
{
public:
virtual int InitialiseWiFiDiscovery(IWifiCameraEnumerationCallback*) = 0;
virtual void UnInitialiseWiFiDiscovery() = 0;
virtual int EnumerateWiFiDevice() = 0;
};
class IWifiCameraEnumerationCallback
{
public:
virtual void onWiFiDeviceDiscovered( WiFiDeviceInfo* pDeviceInfo,unsigned short nNoOfDevice) = 0;
virtual void onDiscoveryTimeout() = 0;
};

Yes, in a quite straightforward way.
class IWifiCameraDiscovery
{
public:
virtual int InitialiseWiFiDiscovery() = 0;
virtual void UnInitialiseWiFiDiscovery() = 0;
virtual int EnumerateWiFiDevice() = 0;
signals:
void onWiFiDeviceDiscovered(WiFiDeviceInfo* pDeviceInfo, unsigned short nNoOfDevice);
void onDiscoveryTimeout();
};
In your implementation, instead of calling the callbacks directly:
callback.onWiFiDeviceDiscovered(pDevInfo, x);
You would emit a signal:
emit onWiFiDeviceDiscovered(pDevInfo, x);
And you would use QObject::connect to connect these signals to your actual receivers (slots) on the other end. Be aware that emitting a signal is more expensive than calling a virtual function.

try this simple example
#include <QApplication>
#include <QFont>
#include <QPushButton>
#include <QWidget>
class MyWidget : public QWidget
{
public:
MyWidget(QWidget *parent = 0);
};
MyWidget::MyWidget(QWidget *parent)
: QWidget(parent)
{
setFixedSize(200, 120);
QPushButton *quit = new QPushButton(tr("Quit"), this);
quit->setGeometry(62, 40, 75, 30);
quit->setFont(QFont("Times", 18, QFont::Bold));
connect(quit, SIGNAL(clicked()), qApp, SLOT(quit()));
}
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
MyWidget widget;
widget.show();
return app.exec();
}
you can use like this
class IWifiCameraDiscovery
{
public slots:
void UnInitialiseWiFiDiscovery();
signals:
void EnumerateWiFiDevice();
}

Related

How to access QMainWindow from another class

// mainwindow.h
#include "ui_MainWindow.h"
#include "parseTextFile.h"
class MainWindow:public QMainWindow
{
Q_OBJECT
public:
MainWindow(void);
private:
Ui_mainWindow ui;
parseFile *fileParse;
public slots:
int onOkButtonClick();
};
// mainwindow.cpp
MainWindow::MainWindow(void)
{
ui.setupUi(this);
connect(ui.OkButton,SIGNAL(clicked()),this,SLOT(onOkButtonClick()));
}
int MainWindow::onOkButtonClick()
{
fileParse = new parseFile(this);
fileParse->parseTextFile();
return 0;
}
int main(int argc,char* argv[])
{
QApplication app(argc,argv);
MainWindow *mainWindow = new MainWindow();
mainWindow->show();
return app.exec();
}
// parseTextFile.h
class parseFile
{
public:
parseFile(QWidget *parent =0);
~parseFile();
int parseTextFile( );
};
// parseTextFile.cpp
#include "parseTextFile.h"
#include <QMessageBox>
parseFile::parseFile(QWidget *parent)
{
}
parseFile::~parseFile()
{
}
int parseFile::parseTextFile( )
{
QMessageBox::information(this,"a","b");
return 0;
}
I can able to access parseTextFile method but i am getting error in QMessageBox. Is it the right way?
How to print QMessageBox in parseTextFile class?
anything needs to add in parseTextFile constructor?
Pass this to QMessageBox::information as a parent is not a valid argument because parseFile is not a QWidget derived class.
For simple, just pass nullptr instead of this :
QMessageBox::information(nullptr ,"a","b");
In this case, the message box belongs to no one. It's will be closed by the user or when the application exits.
PS: you should release the memory after using fileParse to avoid memory leaks.
There are several ways to like with the main window (static pointer, singleton, top widgets enumeration,etc.) but the simplest one, that I thinks you intend to do, is to make parseFile class as QObject derived class and QMainWindow its parent.
class parseFile: public QObject
{
Q_OBJECT
public:
parseFile(QWidget *parent =0);
~parseFile();
int parseTextFile();
};
parseFile::parseFile(QWidget *parent)
:QObject(parent)
{
}
int parseFile::parseTextFile()
{
QWidget * parentWidget = qobject_cast<QWidget *>( parent() ); //<-- QMainWindow instance
QMessageBox::information( parentWidget, "a", "b");
return 0;
}
int MainWindow::onOkButtonClick()
{
fileParse = new parseFile(this);
fileParse->parseTextFile( );
//fileParse should be released by:
//fileParse->deleteLater();
return 0;
}

Sending QPixmap pointer via signal-slots, empty pixmap data

I have problem with sharing pointer to Pixmap via signal-slot mechanism.
Inside slot function I have correct QPixmap with fulled data.
In reciever I have unaccessible QPixmap with empty data.
Any ideas?
class A
{
public:
A:A():pixmap(0){};
void fillPixmap()
{
// correct Pixmap and data isn't null
}
public signals:
void sendQPixmapToB(QPixmap*);
private:
QPixmap *pixmap;
}
class B
{
public:
B:B(){};
public slots:
void recievePixmap(QPixmap* pixmap)
{
// here in debugger pixmap is unaccesible and data is 0
}
}
void onButtonClicked()
{
a.fillPixmap();
}
int main()
{
A a;
B b;
.....
connect(a, SIGNAL(sendQPixmapToB(QPixmap*)),b,SLOT(recievePixmap(QPixmap*)));
return 0;
}
I can not tell you what is wrong with your code because as you indicate you are showing a pseudo-code, so I will show you the correct way to do it.
#include <QGuiApplication>
#include <QPixmap>
#include <QTimer>
#include <QDebug>
class A: public QObject{
Q_OBJECT
public:
using QObject::QObject;
void sendPixmap(){
fillPixmap();
emit sendQPixmapToB(pixmap);
}
signals:
void sendQPixmapToB(const QPixmap & pixmap);
private:
void fillPixmap(){
pixmap = QPixmap(64, 64);
pixmap.fill(Qt::red);
}
QPixmap pixmap;
};
class B: public QObject{
Q_OBJECT
public:
using QObject::QObject;
public slots:
void recievePixmap(const QPixmap & pixmap){
qDebug()<<pixmap;
}
};
int main(int argc, char *argv[])
{
QGuiApplication a(argc, argv);
A obja;
B objb;
QObject::connect(&obja, &A::sendQPixmapToB, &objb, &B::recievePixmap);
QTimer::singleShot(1000, &obja, &A::sendPixmap);
return a.exec();
}
#include "main.moc"ยท

Qt: slots are not being called from the second thread

I want to update UI from the second thread, I have created slots which are going to be called from other thread by signaling, but somehow it is not being called from the other thread. Below is the code:
WorkerThread.h
class WorkerThread: public QObject
{
Q_OBJECT
public:
WorkerThread();
~WorkerThread();
public slots:
void onStart();
signals:
void sendMessage(const QString& msg, const int& code);
};
WorkerThread.cpp
#include "workerwhread.h"
WorkerThread::WorkerThread(){}
void WorkerThread::onStart(){
emit sendMessage("start", 100);
}
Usage:
MyWidget.h
namespace Ui {
class MyWidget;
}
class MyWidget: public QWidget
{
Q_OBJECT
public:
explicit MyWidget(QWidget *parent = 0);
~MyWidget();
private slots:
void onGetMessage(const QString &msg, const int& code);
private:
Ui::MyWidget *ui;
QThread *thread = nullptr;
WorkerThread *wt = nullptr;
};
MyWidget.cpp
MyWidget::MyWidget(QWidget *parent) : QWidget(parent), ui(new Ui::MyWidget)
{
ui->setupUi(this);
wt = new WorkerThread;
thread = new QThread;
connect(thread, &QThread::finished, wt, &QObject::deleteLater);
connect(ui->btStart, &QPushButton::clicked, wt, &WorkerThread::onStart);
connect(wt, &WorkerThread::sendMessage, this, &MyWidget::onGetMessage);
wt->moveToThread(thread);
thread->start();
}
void MyWidget::onGetMessage(const QString &msg, const int& code)
{
qDebug() << "message" << msg; // this never being called ????
}
Note: When I pass the connection type Qt::DirectConnectoin, then it is working, but the problem is it is not the GUI thread.
connect(wt, &WorkerThread::sendMessage, this, &MyWidget::onGetMessage, Qt::DirectConnection);
Main
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
w.setWindowIcon(QIcon(":/icons/system.png"));
return a.exec();
}
After a lot of trying and checking the code line by line, I finally found the problem. The reason was with overriding the event() function of QWidget, the return value of event() function is bool, so if you return a true it is OK and working well without throwing any runtime or compile-time error. But it will prevent signal-slot events to happen.
So NOT return true, but return QWidget::event(event); then it will slove the problem.

QObject::connect: No such signal(classname)(signalname)(atribure)

I have a problem. All is great when I compile, but slots don't work. After application starts, it outputs QObject::connect: No such signal CMatrix::ReadyToDraw(ww). I tested it, slot DrawFunction() doesn't work. I can't debug because of segmentation fault.
header.h
class CMatrix:public QObject
{
Q_OBJECT
public:
int **m_pMatrix;
const short int m_size=4;
public:
CMatrix();
bool checkFields();
void setField();
signals:
void ReadyToDraw(CMatrix *ww);
public slots:
void MoveDown();
void MoveTop();
void MoveLeft();
void MoveRight();
};
class MainWindow : public QWidget
{
Q_OBJECT
public:
MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void DrawFunction(CMatrix *A);
public:
QPushButton* butTop;
QList<QLabel*> lblList;
};
main.cpp
#include "header.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
CMatrix *ww=new CMatrix;
QObject::connect(w.butTop,SIGNAL(clicked()),ww,SLOT(MoveTop()));
QObject::connect(ww,SIGNAL(ReadyToDraw(ww)),&w,SLOT(DrawFunction(ww)));
w.show();
return a.exec();
}
your signals signature is
ReadyToDraw(CMatrix *)
not
ReadyToDraw(ww)
as connect() needs the type, not the name of the variable.
so change your connect line to:
QObject::connect(ww,SIGNAL(ReadyToDraw(CMatrix *)),&w,SLOT(DrawFunction(CMatrix *)));

On Qt, how to change the icon of an action in the toolbar at runtime?

In a program which calculates abritary precision numbers.
I have an action on the taskbar.
QAction* button_stop_continue.
I've set the icon green icon in the beginning of the program, when calculations are being executed it should turn red and so on.
I've already tried something like this:
connect(this, SIGNAL(startedComputing()), this, SLOT(turnIconRed()));
connect(this, SIGNAL(finishedComputing()), this, SLOT(turnIconGreen()));
the function turnIconRed is looking similar to this:
void turnIconRed()
{
button_stop_continue->setIcon(QIcon("images/red-light.png"));
}
I've come up with some incredibly-ugly algorithms :S. Isn't there a straight-forward way to deal with that on Qt? Any ideas?
Thanks.
I would subclass QAction and add some logic for the states in which it can be. It is never a good idea to hardcode the color of something into the name of a method. By subclassing QAction, the look and feel of it is encapsulated.
This could be something like this:
Header file:
class StateAction : public QAction
{
Q_OBJECT
public:
explicit StateAction(QObject *parent = 0);
public slots:
void start();
void stop();
void pause();
};
Implementation file:
StateAction::StateAction(QObject *parent) :
QAction(parent)
{
this->stop();
}
void StateAction::start()
{
this->setIcon(QIcon(":/states/start.png"));
}
void StateAction::stop()
{
this->setIcon(QIcon(":/states/stop.png"));
}
void StateAction::pause()
{
this->setIcon(QIcon(":/states/pause.png"));
}
Now, in your MainWindow you can use that custom QAction simply by connecting its slots to the right signals:
Header file:
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
signals:
void startedComputing();
void finishedComputing();
void pausedComputing();
private:
void createActions();
void createToolbars();
void createConnections();
StateAction *m_stateAction;
};
Implementation file:
...
void MainWindow::createConnections()
{
connect(this, SIGNAL(startedComputing()), m_stateAction, SLOT(start()));
connect(this, SIGNAL(finishedComputing()), m_stateAction, SLOT(stop()));
connect(this, SIGNAL(pausedComputing()), m_stateAction, SLOT(pause()));
}
I've found a solution using QToolButton here it is:
Header : FenPrincipale.h
#ifndef FENPRINCIPALE_H
#define FENPRINCIPALE_H
#include <QWidget>
#include <QVBoxLayout>
#include <QToolButton>
#include <QToolBar>
#include <QAction>
#include <QTextEdit>
class FenPrincipale : public QWidget {
Q_OBJECT
public:
FenPrincipale();
private slots:
void goComputing();
void stopComputing();
private:
QAction* actionDemarrer; // Start computing
QAction* actionArreter; // Stop computing
QToolBar* toolBarActions;
QToolButton* boutonAction; // this button holds the appropriate action
QVBoxLayout* layoutPrincipale;
QTextEdit* resultat; // show result
};
...
Implementation : FenPrincipale.cpp
#include "FenPrincipale.h"
FenPrincipale::FenPrincipale() : QWidget()
{
this->setFixedSize(400, 200);
// create actions
actionDemarrer = new QAction(QIcon("bouton-vert.png"), "demarrer", this);
actionArreter = new QAction(QIcon("bouton-rouge.png"), "arreter", this);
boutonAction = new QToolButton;
boutonAction->setDefaultAction(actionDemarrer);
// create toolbar
toolBarActions = new QToolBar(this);
toolBarActions->addWidget(boutonAction);
// create result widget
resultat = new QTextEdit(this);
// create layout
layoutPrincipale = new QVBoxLayout(this);
layoutPrincipale->addWidget(toolBarActions);
layoutPrincipale->addWidget(resultat);
this->setLayout(layoutPrincipale);
// make connections
QObject::connect(actionDemarrer, SIGNAL(triggered()), this, SLOT(goComputing()));
QObject::connect(actionArreter, SIGNAL(triggered()), this, SLOT(stopComputing()));
}
void FenPrincipale::goComputing()
{
resultat->setText("Computing...");
boutonAction->setDefaultAction(actionArreter);
}
void FenPrincipale::stopComputing()
{
resultat->setText("Partial result : {?}");
boutonAction->setDefaultAction(actionDemarrer);
}
...
Main
#include <QApplication>
#include "FenPrincipale.h"
int main(int argc, char* argv[])
{
QApplication app(argc, argv);
FenPrincipale fenetre;
fenetre.show();
return app.exec();
}