I would like to know how to call the function Do_Download() from the SocketTest class outisde the main() function.
The first cTest.Do_Download() does work, but when I call the test() function, the csTest.Do_Download() does not work.
So it looks like I can only acces SocketTest from inside the main() function, and not from any other function.
Does somebody know how this can be solved?
Thanks!
main.cpp:
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
SocketTest cTest;
cTest.Do_Download();
return a.exec();
}
void test()
{
qDebug() << "test main functie";
SocketTest csTest;
csTest.Do_Download();
}
SocketTest.h:
#ifndef SOCKETTEST_H
#define SOCKETTEST_H
#include <QObject>
#include <QTcpSocket>
#include <QDebug>
#include <QHttp>
#include <QFile>
#include <QString>
class SocketTest : public QObject
{
Q_OBJECT
public:
explicit SocketTest(QObject *parent = 0);
void Do_Download();
signals:
public slots:
void stateChanged ( int state );
void responseHeaderReceived ( const QHttpResponseHeader & resp );
void requestFinished ( int id, bool error );
private:
QTcpSocket *socket;
QHttp *http;
QHttp *http2;
};
#endif // SOCKETTEST_H
If your DoDownload function is doing anything asynchronously (likely, when dealing with the Qt networking classes), the SocketTest you are creating in test() is being destroyed before it can act on any return value.
It works in main() because the event loop starts and the SocketTest instance hangs around.
Related
So I have a Qt class MyQtClass, and a QOject inherited class Sender. I want to access the Ui from the Senderclass (Which by the way only consists static members), so I set up the static Sender& instance(), static void emitSignal()functions and the QSignal Q_SIGNAL void mySignal()in the Sender class (See the code below). In the Qt-class-header MyQtClass.h I've set up the QSlot Q_SLOT void mySlot(). I connect those two slots in the main.cpp
(const bool connected = QObject::connect(&Sender::instance(), &Sender::mySignal, &w, &MyQtClass::mySlot);)
Via qDebug();I found out that the connect is successful, and once I invoke the method emitSignal()the Signal is being emitted. The Slot though is never being called.
I gues that my mistake is in the main at the connection.
Code:
Sender.h:
#pragma once
#include <QtWidgets/QMainWindow>
#include <qdebug.h>
class Sender : public QObject
{
Q_OBJECT
using QObject::QObject;
public:
/*----------------------------*/
/*---Lots of static Members---*/
/*----------------------------*/
static Sender& instance(){
static Sender m_instance;
return m_instance;
}
static void emitSignal() {
emit instance().mySignal();
}
Q_SIGNAL void mySignal() {
qDebug() << "Signal emitted!";
}
};
MyQtClass.h
#pragma once
#include <qdebug.h>
#include <QtWidgets/QMainWindow>
#include "ui_MyQtClass.h"
class MyQtClass : public QMainWindow
{
Q_OBJECT
public:
MyQtClass(QWidget *parent = Q_NULLPTR);
Q_SLOT void mySlot() {
qDebug() << "Slot invoked";
}
private:
Ui::MyQtClassClass ui;
};
MyQtClass.cpp
#include "MyQtClass.h"
#include "Sender.h";
#include <qdebug.h>
MyQtClass::MyQtClass(QWidget *parent)
: QMainWindow(parent)
{
ui.setupUi(this);
qDebug() << "Ui has been setup!";
}
main.cpp
#include "MyQtClass.h"
#include "Sender.h"
#include <qdebug.h>
#include <QtWidgets/QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MyQtClass w;
w.show();
const bool connected = QObject::connect(&Sender::instance(), &Sender::mySignal, &w, &MyQtClass::mySlot);
qDebug() << "Connection established: " << connected;
Sender::emitSignal();
return a.exec();
}
Thank you very much for your time!
I would check your build system. You should not be able to compile the Q_SIGNAL with a function body without getting a redefinition error. If you can build it you are likely not involving any of the Qt machinery that you want to use. I do not have any experience with your Visual Studio/mingw environment but I was able to get the following to build on an Linux platform.
main.cpp
#include <QDebug>
#include <QtWidgets/QApplication>
#include <QtWidgets/QMainWindow>
#include "Sender.h"
#include "MyQtClass.h"
//#include "ui_MyQtClass.h"
MyQtClass::MyQtClass(QWidget *parent)
: QMainWindow(parent)
{
qDebug() << "Ui has been setup!";
}
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MyQtClass w;
w.show();
const bool connected = QObject::connect(&Sender::instance(), &Sender::mySignal, &w, &MyQtClass::mySlot);
qDebug() << "Connection established: " << connected;
Sender::emitSignal();
return a.exec();
}
Sender.h
#pragma once
#include <QtWidgets/QMainWindow>
class Sender : public QObject
{
Q_OBJECT
using QObject::QObject;
public:
/*----------------------------*/
/*---Lots of static Members---*/
/*----------------------------*/
static Sender& instance(){
static Sender m_instance;
return m_instance;
}
static void emitSignal() {
emit instance().mySignal();
}
Q_SIGNAL void mySignal();
};
MyQtClass.h
#pragma once
#include <QtWidgets/QMainWindow>
#include <QDebug>
class MyQtClass : public QMainWindow
{
Q_OBJECT
public:
MyQtClass(QWidget *parent = Q_NULLPTR);
Q_SLOT void mySlot() {
qDebug() << "Slot invoked";
}
private:
//Ui::MyQtClassClass ui;
};
I then built it manually invoking the Meta-Object Compiler
moc Sender.h > moc_Sender.cpp
moc MyQtClass.h > moc_MyQtClass.cpp
g++ main.cpp moc_Sender.cpp moc_MyQtClass.cpp -I /usr/include/x86_64-linux-gnu/qt5/QtCore/ -I /usr/include/x86_64-linux-gnu/qt5 -fPIC -std=c++11 -I /usr/include/x86_64-linux-gnu/qt5/QtWidgets/ -lQt5Core -lQt5Widgets
which produced the expected output
user#mintvm ~ $ ./a.out
Ui has been setup!
Connection established: true
Slot invoked
Have you tried to use the qmake build system so that you do not need to worry about all of the details related to the compilation of the meta object system?
This happens when i enter the code to the slots of my buttons
when i debug the app normally ,all functions etc everything is ok.
but when i try use my code in UI something brokes down.
I'm making the Parking System for my uni classes ,
i got one parent class vehicle and 4 child classes which inherits by public.
here's the code from the UI source file :
#include "datain.h"
#include "ui_datain.h"
#include <QMessageBox>
#include <car.h>
#include <bike.h>
#include <motorbike.h>
#include <tir.h>
#include <vehicle.h>
#include <iostream>
using namespace std;
dataIn::dataIn(QWidget *parent) :
QDialog(parent),
ui(new Ui::dataIn)
{
ui->setupUi(this);
}
dataIn::~dataIn()
{
delete ui;
}
dataIn siema;
void dataIn::on_lineEdit_textEdited(const QString &arg1)
{
siema.rejestracja=arg1;
}
void dataIn::on_lineEdit_2_textEdited(const QString &arg1)
{
siema.marka=arg1;
}
void dataIn::on_car_clicked()
{
Vehicle *wsk;
string reje,mareczka;
reje=siema.rejestracja.toStdString();
mareczka=siema.marka.toStdString();
Car obj(reje,mareczka);
wsk=&obj;
wsk->add();
}
void dataIn::on_motorcycle_clicked()
{
Vehicle *wsk;
string reje,mareczka;
reje=siema.rejestracja.toStdString();
mareczka=siema.marka.toStdString();
Motorbike obj(reje,mareczka);
wsk=&obj;
wsk->add();
}
void dataIn::on_bike_clicked()
{
Vehicle *wsk;
string reje,mareczka;
reje=siema.rejestracja.toStdString();
mareczka=siema.marka.toStdString();
Bike obj(reje,mareczka);
wsk=&obj;
wsk->add();
}
void dataIn::on_tir_clicked()
{
Vehicle *wsk;
string reje,mareczka;
reje=siema.rejestracja.toStdString();
mareczka=siema.marka.toStdString();
Tir obj(reje,mareczka);
wsk=&obj;
wsk->add();
}
Here's the main:
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
dataIn code:
#ifndef DATAIN_H
#define DATAIN_H
#include <QString>
#include <iostream>
#include <QDialog>
using namespace std;
namespace Ui {
class dataIn;
}
class dataIn : public QDialog
{
Q_OBJECT
QString rejestracja;
QString marka;
public:
explicit dataIn(QWidget *parent = 0);
~dataIn();
private slots:
void on_lineEdit_textEdited(const QString &arg1);
void on_lineEdit_2_textEdited(const QString &arg1);
void on_car_clicked();
void on_motorcycle_clicked();
void on_bike_clicked();
void on_tir_clicked();
private:
Ui::dataIn *ui;
};
#endif // DATAIN_H
You may not declare
dataIn siema;
and you should not refer to it - remove all siema.... references from your dataIn functions (replace siema. with nothing or with this->).
When you need your dialog for the first time, create it in (and as child of) MainWindow:
void MainWindow::whateverEvent() {
dataIn *siema = new dataIn(this);
siema->show();
}
If you need to reference it again or want do prevent it from being created twice, store the pointer in a member of your MainWindow.
I wanted to create a Qt widget which communicates with other classes on different threads via the signal / slot system. The recieving Objects are created in a Function wich is run via std::async.
The problem is: If the widget emits a signal the slot on the other thread is not called.
My Example:
I created the Class MainWindow which derives from QMainWindow and will live on the main thread. The class Reciever is created in a function which is called via std::async, and has a thread which should print something to the console.
I tested if the signal is emitted by connecting it to another slot on the same thread which works fine.
MainWindow.hpp
#pragma once
#include <QMainWindow>
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
signals:
void send();
private slots:
void buttonClicked();
void recieve();
};
MainWindow.cpp
#include "MainWindow.hpp"
#include <iostream>
#include <QPushButton>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent)
{
QPushButton* start = new QPushButton("Start");
setCentralWidget(start);
start->show();
connect(start, SIGNAL(clicked(bool)), this, SLOT(buttonClicked()));
connect(this, SIGNAL(send()), this, SLOT(recieve()));
}
void MainWindow::buttonClicked()
{
std::cout << "MainWindow::buttonClicked()\n";
emit send();
}
void MainWindow::recieve()
{
std::cout << "MainWindow::recieve()\n";
}
Reciever.hpp
#include <QObject>
class Reciever : public QObject
{
Q_OBJECT
public:
Reciever(QObject *parent = 0);
public slots:
void recieve();
};
Reciever.cpp
#include "Reciever.hpp"
#include <iostream>
Reciever::Reciever(QObject *parent) : QObject(parent)
{
std::cout << "Reciever()" << std::endl;
}
void Reciever::recieve()
{
std::cout << "Reciever::recieve()" << std::endl;
}
main.cpp
#include "MainWindow.hpp"
#include "Reciever.hpp"
#include <QApplication>
#include <future>
void StartAndConnect(MainWindow &widget)
{
Reciever* rec = new Reciever();
QObject::connect(&widget, SIGNAL(send()), rec, SLOT(recieve()));
}
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
MainWindow myWidget;
myWidget.show();
auto future = std::async(std::launch::async, [&myWidget](){
StartAndConnect(myWidget);
});
app.exec();
future.wait();
}
After some research my strongest guess was, that the thread launched by std::async does not has a Qt event-loop and thus will not come to a point where the posted event (emit) is processed. I changed the main to use QtConcurrent::run but it also did not work.
EDIT
Here my try with QtConcurrent:
main2.cpp
#include "MainWindow.hpp"
#include "Reciever.hpp"
#include <QApplication>
#include <future>
#include <QtConcurrent>
void StartAndConnect(MainWindow &widget)
{
Reciever* rec = new Reciever();
QObject::connect(&widget, SIGNAL(send()), rec, SLOT(recieve()));
}
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
MainWindow myWidget;
myWidget.show();
auto future = QtConcurrent::run( [&myWidget](){
StartAndConnect(myWidget);
});
app.exec();
future.waitForFinished();
}
You need a running event loop in your thread, if you want to process cross-thread slot calls.
auto future = QtConcurrent::run( [&myWidget](){
StartAndConnect(myWidget);
QEventLoop e;
e.exec();
});
But I recomend to use QThread, because in your case it is obvious. Qt has a very good documentation, that describes your case.
I have this two class in c++
GUI.cpp
#include "AL_GUI.h"
#include <QtGui/QApplication>
#include "mainwindow.h"
GUI::GUI() {
}
void GUI::startGUI(){
int c=1;
char *array[10];
char** v = &array[0];
QApplication qa(c,v);
w.show();
qa.exec();
}
void GUI::notifyAlert(){
}
GUI::~GUI() {
// TODO Auto-generated destructor stub
}
GUI.h
#include <QtGui/QApplication>
#include "mainwindow.h"
#include "mainwindow.h"
#ifndef GUI_H_
#define GUI_H_
class GUI {
public:
GUI();
virtual ~GUI();
void startGUI();
void notifyAlert();
private:
MainWindow w;
};
#endif
But when i run this program i have the error:
QWidget: Must construct a QApplication before a QPaintDevice
How can I declare MainWindow w in gui.h in such a way that I don't receive this error
You can't (well, you can, but you shouldn't). The MainWindon declaration is right where it should be. The problem is that you attempt to create a GUI object before you create the QApplication.
Why not create the QApplication where you create the GUI object, just before it?
I would have made w a pointer used a forward declaration for MainWindow and removed all the includes (including the 2 includes for mainwindow.h) from GUI.h. Then like the answer from Sebastian says construct the QApplication first.
AL_GUI.h
#ifndef GUI_H_
#define GUI_H_
class MainWindow;
class GUI {
public:
GUI();
virtual ~GUI();
void startGUI();
void notifyAlert();
private:
MainWindow* w;
};
gui.cpp
#include "AL_GUI.h"
#include <QtGui/QApplication>
#include "mainwindow.h"
GUI::GUI() : w(NULL)
{
}
void GUI::startGUI(){
int c=1;
char *array[10];
char** v = &array[0];
QApplication qa(c,v);
w = new MainWindow;
w->show();
qa.exec();
}
void GUI::notifyAlert(){
}
GUI::~GUI() {
delete w;
}
I'm trying to create a simple game that uses a timer but I can't seem to get it working. It throws this error: "no matching function for call to 'QObject::connect(QTimer*&, const char*, Time*&, const char*)'" now matter what I do do I can't fix it please help. I have only just started coding the game when I ran into this error. Here are the files exluding the unimportant(at the moment) qml file.
Main.cpp:
#include <QtGui/QApplication>
#include "qmlapplicationviewer.h"
#include "time.h"
#include <QObject>
#include <QTimer>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QmlApplicationViewer viewer;
viewer.setOrientation(QmlApplicationViewer::ScreenOrientationLockLandscape);
viewer.setMainQmlFile(QLatin1String("qml/RaakGame/main.qml"));
viewer.showExpanded();
Time *timmer = new Time;
QTimer *timer = new QTimer(0);
QObject::connect(timer, SIGNAL(timeout()), timmer, SLOT(ShowTime()));
timer->start(1000);
return app.exec();
}
time.h:
#ifndef TIME_H
#define TIME_H
class Time
{
public:
Time();
private slots:
void ShowTime();
signals:
int setTime();
};
time.cpp:
#include "time.h"
int theTime = 60;
Time::Time()
{
ShowTime();
}
void Time::ShowTime()
{
theTime--;
}
int Time::setTime()
{
return theTime;
}
#endif // TIME_H
Your implementation of Time does not declare it to be a QObject, so you can't not connect slots or signals from it. You need to inherit from QObject (or probably QWidget if you want to draw on the screen) and then include the statement Q_OBJECT which instantiates a few needed things.
class Time : public QWidget
{
Q_OBJECT
public:
Time();
private slots:
void ShowTime();
signals:
int setTime();
};
I notice that your classes do not contain the Q_OBJECT macro defined. This may help your efforts.
class Time
{
Q_OBJECT
public Time()
.
.
.
}