In one of my project, I am trying to use the Qt signals and slots mechanism with a std::vector<bool> as parameter. My project is equivalent to the following minimal code :
class App
// app.h
#ifndef APP_H
#define APP_H
#include <QObject>
#include <QSharedPointer>
#include "emitter.h"
#include "receiver.h"
class App : public QObject
{
Q_OBJECT
public:
App(QObject *parent = 0);
};
#endif // APP_H
// app.cpp
#include "app.h"
App::App(QObject* parent): QObject(parent)
{
Emitter emitter;
Receiver receiver;
receiver.attachEmitter(emitter);
emitter.run();
}
class Emitter
//emitter.h
#ifndef EMITTER_H
#define EMITTER_H
#include <QObject>
#include <vector>
#include "helper.h"
class Helper;
class Emitter : public QObject
{
Q_OBJECT
public:
explicit Emitter(QObject *parent = 0);
void run();
signals:
void triggered(std::vector<bool> value);
};
#endif // EMITTER_H
// emitter.cpp
#include "emitter.h"
Emitter::Emitter(QObject *parent) : QObject(parent)
{
}
void Emitter::run()
{
emit triggered(Helper::value());
}
class Receiver
//receiver.h
#ifndef RECEIVER_H
#define RECEIVER_H
#include <QObject>
#include <QDebug>
#include <QSharedPointer>
#include "emitter.h"
class Emitter;
class Receiver : public QObject
{
Q_OBJECT
public:
explicit Receiver(QObject *parent = 0);
void attachEmitter(QSharedPointer<Emitter> emitter);
signals:
public slots:
};
//receiver.cpp
#include "receiver.h"
Receiver::Receiver(QObject *parent) : QObject(parent)
{
}
void Receiver::attachEmitter(QSharedPointer<Emitter> emitter)
{
connect(emitter.data(), &Emitter::triggered, [this](std::vector<bool> value) {
qDebug() << "Received value";
});
}
For some reason, the compiler doesn't like it at all and prints me this stack :
What do I have to do ? Thank you
Signal/slot values passend by value have to be registered with the Qt meta system and I don't think the std::vector is registered by default. Is there a reason you're not using QVectorinstead?
For futher information look at Q_DECLARE_METATYPE and qRegisterMetaType().
Related
I am new in C++ Qt and struggling with the correct use of forward declarations and #include.
What I want to do:
I have a Qt Gui (Class Ui::Gui) where we can set values.
I want to save these values in Gui Class variables.
As soon as a button (Generate Xml) is clicked, I want to pass the object
'ui' to the XmlGeneratorClass, So i can use the values to generate a Xml.
gui.h
#ifndef GUI_H
#define GUI_H
#include <QMainWindow>
#include <QDebug>
#include "xmlgeneratorqobject.h"
namespace Ui {
class Gui;
}
class Gui : public QMainWindow
{
Q_OBJECT
public:
explicit Gui(QWidget *parent = nullptr);
~Gui();
qint8 testvalue = 1;
signals:
void transmitToXmlGen(Ui::Gui*);
private slots:
void on_pushButtonGenerateXml_clicked();
private:
Ui::Gui *ui;
XmlGeneratorQObject *xmlgenerator = new XmlGeneratorQObject();
};
#endif // GUI_H
gui.cpp
#include "gui.h"
#include "ui_gui.h"
Gui::Gui(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::Gui)
{
ui->setupUi(this);
connect(this,SIGNAL(transmitToXmlGen(Ui::Gui*)),xmlgenerator,SLOT(receiveFromGui(Ui::Gui*)));
}
Gui::~Gui()
{
delete ui;
}
void Gui::on_pushButtonGenerateXml_clicked()
{
emit transmitToXmlGen(ui);
}
xmlgeneratorqobject.h
#ifndef XMLGENERATORQOBJECT_H
#define XMLGENERATORQOBJECT_H
#include <QObject>
#include <QDebug>
namespace Ui {
class XmlGeneratorQObject;
class Gui;
}
class XmlGeneratorQObject : public QObject {
Q_OBJECT
public:
explicit XmlGeneratorQObject(QObject * parent = nullptr);
private slots:
void receiveFromGui(Ui::Gui*);
};
#endif // XMLGENERATORQOBJECT_H
xmlgeneratorqobject.cpp
#include "xmlgeneratorqobject.h"
XmlGeneratorQObject::XmlGeneratorQObject(QObject *parent){}
void XmlGeneratorQObject::receiveFromGui(Ui::Gui* objectFromGui)
{
qDebug() << objectFromGui->testvalue; // ERROR member access into incomplete type 'Ui::Gui'
}
Expected result:
Access to public variables from passed gui-object should be possible
Actual result:
member access into incomplete type 'Ui::Gui'
Can you please help me learn forward declaration / include?
Is my approach in general okay?
Your xmlgeneratorqobject.cpp needs the line
#include "ui_gui.h"
This gives it the details of the ui widgets. This file is generated by the Qt build system.
I am trying to create a Simple GUI which creates multiple threads and perform some operation at the background while the GUI being responsive all the time. I am using QThreads of QT framework to achieve this but I am facing above said issue. Below is the code.
//Threading.h
This is my threading.h file.
#ifndef THREADING
#define THREADING
#include <QThread>
#include <QObject>
class Threading : public QThread
{
Q_OBJECT
private:
int num;
public:
explicit Threading(QObject * parent = 0);
void run();
void set_num(int);
int get_num();
Q_SIGNALS:
void someSignal(int);
};
//This is threading.cpp file
#include "threading.h"
#include <QtCore>
Threading::Threading(QObject *parent) : QThread(parent)
{
}
void Threading:: run()
{
emit someSignal(get_num());
}
void Threading :: set_num(int num)
{
QMutex mutex;
mutex.lock();
this->num = num;
mutex.unlock();
}
int Threading :: get_num()
{
return num;
}
//Mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QtCore>
#include "threading.h"
typedef unsigned char byte;
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
Threading *threadPointer;
};
//Mainwindow.cpp
In this file I am starting thread.
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "global.h"
#include <QtCore>
#include <QObject>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui-> setupUi(this);
threadPointer = new Threading(this);
connect(threadPointer,SIGNAL(someSignal(int)),this,SLOT(onSomeSignal()));
}
void MainWindow::on_clicked()
{
threadPointer->set_num(0);
threadPointer->start();
}
I saw some video online which has exactly similar code which is strangely but working fine and mine is not. Does it have to do with version ? Any help would be appreciated.
You can bypass the issue using Q_EMIT in place of emit, or just call the signal as a normal function (emit is optional and is there just for code readability):
void Threading:: run()
{
someSignal(get_num());
}
emit is an empty macro defined in qobjectdefs.h. You should investigate further, and try to understand why it is not defined (e.g. if QT_NO_KEYWORDS is defined somewhere and why).
You may also want to check if a
CONFIG += no_keywords
line exists in your pro file, as explained at the very end of this.
I know this question asked many times but still confused and cannot find a solution.
I have a MainWindow and a class.
In my class I have a signal which I emit in the method call.
Problem: Slot method is not firing.
Here is my code.
BWorker.h
#pragma once
#include <QObject>
class BWorker : public QObject
{
Q_OBJECT
public:
BWorker(QObject *parent);
~BWorker();
void doSomething();
signals:
void signalSomething();
};
BWorker.cpp
#include "BWorker.h"
BWorker::BWorker(QObject *parent)
: QObject(parent)
{
}
BWorker::~BWorker()
{
}
void BWorker::doSomething()
{
emit signalSomething();
}
QtGuiApplication1.h
#pragma once
#include <QtWidgets/QMainWindow>
#include "ui_QtGuiApplication1.h"
class QtGuiApplication1 : public QMainWindow
{
Q_OBJECT
public:
QtGuiApplication1(QWidget *parent = Q_NULLPTR);
public slots:
void workDone();
private:
Ui::QtGuiApplication1Class ui;
};
QtGuiApplication1.cpp
#include "QtGuiApplication1.h"
#include "BWorker.h"
QtGuiApplication1::QtGuiApplication1(QWidget *parent)
: QMainWindow(parent)
{
ui.setupUi(this);
BWorker bworker(this);
connect(&bworker, SIGNAL(bworker.signalSomething), this, SLOT(workDone()));
bworker.doSomething();
}
void QtGuiApplication1::workDone() {
}
Your problem is here:
SIGNAL(bworker.signalSomething)
This is not valid and you should see a message in terminal saying that there is no such signal. The correct syntax for your case would be:
SIGNAL(signalSomething())
Please post your code by copy-pasting it. Do not edit the code: The problem could be emerged from where you edited.
The code of mine worked fine.
mainwindow.h:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
public slots:
void workDone();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
mainwindow.cpp:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
#include "worker.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
Worker worker(this);
connect(&worker, SIGNAL(signalSomething()), this, SLOT(workDone()));
//connect(&worker, &Worker::signalSomething, this, &MainWindow::workDone);
worker.doSomething();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::workDone()
{
qDebug() << "Done.";
}
worker.h:
#ifndef WORKER_H
#define WORKER_H
#include <QObject>
class Worker : public QObject
{
Q_OBJECT
public:
explicit Worker(QObject *parent = 0);
void doSomething();
signals:
void signalSomething();
public slots:
};
#endif // WORKER_H
worker.cpp:
#include "worker.h"
Worker::Worker(QObject *parent) : QObject(parent)
{
}
void Worker::doSomething()
{
emit signalSomething();
}
Application Output:
Debugging starts
Done.
Debugging has finished
Printed out "Done." means the codes are working.
I want to get the currentIndex of combobox in the main Widget, then send this value to another class. I tried to make a function to return the currentIndex, however, I always get '0'. Could someone explain me why, or give me an example? Plus, I think it's because I create a new object in another class, but how can I get the actived Widget's pointer? Here are the codes:
widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
namespace Ui { class Widget; }
class Widget : public QWidget {
Q_OBJECT
public:
explicit Widget(QWidget *parent = 0);
~Widget();
int comboboxText();
private slots:
void on_pushButton_clicked();
private:
Ui::Widget *ui; };
#endif // WIDGET_H
widget.cpp
#include "widget.h"
#include "ui_widget.h"
#include <QDebug>
#include "database.h"
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
}
Widget::~Widget()
{
delete ui;
}
int Widget::comboboxText(){
return ui->comboBox->currentIndex();
}
void Widget::on_pushButton_clicked()
{
qDebug() << "from Widget: " << comboboxText();
Database database;
database.getIndex();
}
database.h
#ifndef DATABASE_H
#define DATABASE_H
#include <QObject>
#include <QDebug>
class Database : public QObject
{
Q_OBJECT
public:
explicit Database(QObject *parent = 0);
void getIndex();
signals:
public slots:
};
#endif // DATABASE_H
database.cpp
#include "database.h"
#include <widget.h>
Database::Database(QObject *parent) : QObject(parent)
{
}
void Database::getIndex(){
Widget a;
qDebug()<< "from database: " << a.comboboxText();
}
I have a task to create a chess game with support for network play.
For development I've been using Qt.
The problem is as follows:
I have class "MyServer":
Header file->
//Header file "MyServer.h"
#ifndef MYSERVER_H
#define MYSERVER_H
#include <QTcpServer>
#include <QTcpSocket>
#include <mythread.h>
#include <QDebug>
class MyServer : public QTcpServer
{
Q_OBJECT
public:
explicit MyServer(QObject *parent = 0);
void startServer();
QList<QString> *usersOnline;
QList<QTcpSocket*> *connections;
signals:
public slots:
protected:
void incomingConnection(int socketDescriptor);
private:
QTcpServer* server;
//QTcpSocket* socket;
//QByteArray* bytes;
//QString* str;
};
#endif // MYSERVER_H
cpp.file
#include "myserver.h"
MyServer::MyServer(QObject *parent) :
QTcpServer(parent)
{
}
void MyServer::startServer()
{
this->listen(QHostAddress::Any,1234);
usersOnline=new QList<QString>;
}
void MyServer::incomingConnection(int socketDescriptor)
{
MyThread* thread=new MyThread(socketDescriptor,this,this);
thread->run();
}
As you can see, this class with a new connection creates a new thread.
Class "MyThread".
Header file
#ifndef MYTHREAD_H
#define MYTHREAD_H
#include <QThread>
#include <QTcpSocket>
#include <QTcpServer>
#include <QDebug>
#include <QDataStream>
#include <QObject>
#include <myserver.h>
class MyServer;
class MyThread : public QThread
{
Q_OBJECT
public:
explicit MyThread(int ID,MyServer* s,QObject *parent = 0);
void run();
signals:
public slots:
void readyRead();
private:
QTcpSocket* socket;
int socketDescriptor;
};
#endif // MYTHREAD_H
cpp.file
#include "mythread.h"
MyThread::MyThread(int ID,MyServer* s,QObject *parent) :
QThread(parent)
{
//this->mainserver=parent;
//parent=new MyServer();
//qDebug()<<s->usersOnline;
this->socketDescriptor=ID;
}
void MyThread::run()
{
qDebug()<<"Starts thread";
socket=new QTcpSocket();
socket->setSocketDescriptor(this->socketDescriptor);
connect(socket,SIGNAL(readyRead()),this,SLOT(readyRead()),Qt::DirectConnection);
exec();
}
void MyThread::readyRead()
{
QDataStream in(this->socket);
quint32 n;
in>>n;
qDebug()<<n;
QByteArray bytes;
QDataStream out(&bytes,QIODevice::WriteOnly);;
QString str;
switch (n) {
case 1:
in>>str;
qDebug()<<str;
//usersOnline->append(str);
//qDebug()<<*(usersOnline);
//out(&bytes,QIODevice::WriteOnly);
//out<<(*usersOnline);
//for(int i=0;i<this->connections->length();i++)
//{
//connections->at(i)->is
//connections->at(i)->write(bytes);
//connections->at(i)->waitForBytesWritten(2000);
// }
break;
case 2:
in>>str;
qDebug()<<str;
break;
}
}
In "MyThread" constructor i pass pointer to "MyServer" class to use the fields "usersOnline","connections" and "MyServer" method.
Thus, i have in my architecture "circular dependency" with "MyServer" and "MyThread" class.
How to change architecture?
Thanks a lot.
Remove #include <myserver.h> from your thread header. You already have class MyServer; forward definition there, it will be enough.