Qt compiler error: 'emit' was not declared in this scope - c++

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.

Related

Downloading txt file with qtnetwork

I am trying to download a txt file from a url in QT but i can't seem to make it work.
I am following this guide https://wiki.qt.io/Download_Data_from_URL. I implemented the filedownloader class exactly like it's made in the guide, but when i try to use it like specified in the guide I cannot make it work. I created a slot to be called when the download is finished, but if i try to call the downloader inside like the guide it says it is an undeclared identifier.
Does anyone know how to correctly implement this code?
this is the .cpp of my code
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "mainwindow.h"
#include <QStringList>
#include <QCoreApplication>
#include <QFile>
#include <QFileInfo>
#include <QList>
#include <QtNetwork/QNetworkReply>
#include <QStringList>
#include <QTimer>
#include <QUrl>
#include <QtNetwork/QNetworkAccessManager>
#include <QtNetwork/QNetworkRequest>
#include <filedownloader.h>
#include <iostream>
#include <QObject>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
QUrl emiurl( "url of my file");
// call to the downloader class.In the guide it's made differently, but it doesn't work
FileDownloader emiload(emiurl,this);
//this connect links the end of the download with the textwriter slot
QObject::connect(&emiload, SIGNAL (downloaded()), this, SLOT (textwriter()));
}
>MainWindow::~MainWindow()
{
delete ui;
}
//slot needed to create the txt file from the downloaded one
void MainWindow::textwriter()
{
QByteArray emibyte;
emibyte=emiload->downloadedData(); //this line gives me error
QFile emifile("emi.txt");
emifile.open(QIODevice::WriteOnly);
std::cout << emibyte.size() << std::endl;
QDataStream out(&emifile);
out << emibyte;
std::cout << emifile.size() << std::endl;
}
Now here's the .h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include "filedownloader.h"
#include <QMainWindow>
#include <QtNetwork/QNetworkAccessManager>
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;
private slots:
void textwriter();
};
#endif // MAINWINDOW_H
To make the undeclared identifier go away and successfully compile, you need to add FileDownloader to the class' declaration. This way, it will be known throughout the class.
I chose to go with the approach that's usual in Qt, to declare a pointer to FileDownloader.
#pragma once // <--- this is supported by virtually any compiler today
#include <QMainWindow>
#include <QtNetwork/QNetworkAccessManager>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class FileDownloader; // <-- forward declaration is enough, but you can also #include "filedownloader.h"
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget* parent = nullptr);
~MainWindow();
private:
Ui::MainWindow* ui = nullptr;
FileDownloader* emiload = nullptr; // <--- the important line!
private slots:
void textwriter();
};
And then instantiate and call emiload in the constructor:
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
// create an instance of FileDownloader with "new".
emiload = new FileDownloader(QUrl("url of my file"), this);
// using member pointer connection available since Qt5
connect(emiload, &FileDownloader::downloaded, this, &MainWindow::textwriter);
}

Trying to connect an emit signal, but nothing appears on the other side

So, I am learning my way around QThread and I understand it to a reasonable extent.
However, I am trying to implement a class derived from QThread to do a basic function, then emit a signal if a value has changed.
Let me post the Header and Source files:
dialog.h
#ifndef DIALOG_H
#define DIALOG_H
#include <QDialog>
#include "mythread.h"
#include <QTextEdit>
#include <QPushButton>
#include <QSpinBox>
class Dialog : public QDialog
{
Q_OBJECT
public:
Dialog(QWidget *parent = nullptr);
~Dialog();
void setGUI();
void Start_Thread();
void Stop_Thread(); //will be implemented later to stop all threads
public slots:
void Update_O1(int);
private:
MyThread *m_Thread1;
QPushButton *START;
QPushButton *STOP;
QSpinBox *L1Input;
QSpinBox *L2Input; //For when a second Thread is added
QSpinBox *L3Input; //For when a third Thread is added
QTextEdit *L1Out;
QTextEdit *L2Out; //For when a second Thread is added
QTextEdit *L3Out; //For when a third Thread is added
};
#endif // DIALOG_H
dialog.cpp
#include "dialog.h"
#include <QGridLayout>
Dialog::Dialog(QWidget *parent)
: QDialog(parent), m_Thread1(new MyThread), START(new QPushButton("Start")),
STOP(new QPushButton("Stop")), L1Input(new QSpinBox), L2Input(new QSpinBox),
L3Input(new QSpinBox),L1Out(new QTextEdit),L2Out(new QTextEdit), L3Out(new QTextEdit)
{
setGUI();
connect(START, &QPushButton::clicked, this, &Dialog::Start_Thread);
connect(STOP, &QPushButton::clicked, this, &Dialog::Stop_Thread);
connect(m_Thread1, SIGNAL(NumberChanged(int)), this, SLOT(Update_01(int)));
//I know this is the old Syntax, but I don't know how else to do this...yet.
//I got a similar function to work on a different project using the same format
}
Dialog::~Dialog()
{
}
void Dialog::setGUI()
{
setWindowTitle("Three Threads");
resize(300,300);
START->setMaximumWidth(100);
STOP->setMaximumWidth(100);
L1Input->setRange(1,100);
L2Input->setRange(1,100);
L3Input->setRange(1,100);
L1Out->setReadOnly(true);
L2Out->setReadOnly(true);
L3Out->setReadOnly(true);
QGridLayout *GLout = new QGridLayout;
GLout->addWidget(START,0,1);
GLout->addWidget(STOP,0,2);
GLout->addWidget(L1Input,1,0);
GLout->addWidget(L2Input,1,1);
GLout->addWidget(L3Input,1,2);
GLout->addWidget(L1Out,2,0);
GLout->addWidget(L2Out,2,1);
GLout->addWidget(L3Out,2,2);
setLayout(GLout);
}
void Dialog::Start_Thread()
{
qDebug() << "START works"; //this is just to let me know the Start_Thread function is called properly
m_Thread1->start();
}
void Dialog::Stop_Thread()
{
}
void Dialog::Update_O1(int x)
{
qDebug() << QString::number(x);
}
mythread.h
#ifndef MYTHREAD_H
#define MYTHREAD_H
#include <QThread>
#include <QObject>
class MyThread : public QThread
{
Q_OBJECT
public:
explicit MyThread(QObject *parent = nullptr);
void run();
void setSnum(int); //setter function
bool Stop = false; //will be used later to stop a thread
signals:
void NumberChanged(int);
private:
int Snum = 10; //temporary value. will later implement setter to use value from GUI
};
#endif // MYTHREAD_H
mythread.cpp
#include "mythread.h"
#include <QDebug>
#include <QtCore>
MyThread::MyThread(QObject *parent)
: QThread{parent}
{
}
void MyThread::run()
{
while(!(Snum == 1))
{
QMutex mutex;
mutex.lock();
if(this->Stop) break;
mutex.unlock();
if(Snum % 2 == 0)
{
Snum /= 2;
}else{
Snum *= 3;
Snum += 1;
}
emit this->NumberChanged(Snum);
}
}
void MyThread::setSnum(int startnum)
{
Snum = startnum;
}
Lastely, a very basic main.cpp file
#include "dialog.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Dialog w;
w.show();
return a.exec();
}
The problem I am facing though is that I don't seem to be getting a signal from the emit function in mythread.cpp's run function. I am not getting an output in the qDebug() section of QtCreator.
What am I doing wrong?
I am using Qt 6.3.0.
You have this connection:
connect(m_Thread1, SIGNAL(NumberChanged(int)), this, SLOT(Update_01(int)));
But your method is actually called like this:
void Dialog::Update_O1(int x)
Note Update_01 vs. Update_O1. They are different characters. One is zero and the other is capital 'o'.
So, you should have:
connect(m_Thread1, SIGNAL(NumberChanged(int)), this, SLOT(Update_O1(int)));
However, the best solution is to use the "new" signal-slot syntax.
connect(m_Thread1, &QThread::NumberChanged, this, &QDialog::Update_O1);
Actually, if the slot is short, you could even use a lambda:
connect(m_Thread1, &QThread::NumberChanged, this, [](int x) {qDebug() << QString::number(x);});
The major advantage of the new signal-slot syntax is that this sort of issue would be caught at compilation time rather than runtime. So, you would not potentially release your software with this bug.

Qt C++: Cannot create children for a parent that is in a different thread

Create a simple application in Qt (c ++) to encrypt a text. And I get the error: "Cannot create children for a parent that is in a different thread."
I used threads to update a text edit box in real time while entering text.
I saw other similar topics, but I did not find any solution that I could adapt to myself.
Can you please suggest me how I can solve it?
.h file
#ifndef GENERATORSHACODE_H
#define GENERATORSHACODE_H
#include <QMainWindow>
#include <thread>
QT_BEGIN_NAMESPACE
namespace Ui { class GeneratorShaCode; }
QT_END_NAMESPACE
class GeneratorShaCode : public QMainWindow
{
Q_OBJECT
public:
GeneratorShaCode(QWidget *parent = nullptr);
~GeneratorShaCode();
QString Sha512Generator(QString);
void updateOutputEditText();
std::thread GenerateCode;
private:
Ui::GeneratorShaCode *ui;
};
#endif // GENERATORSHACODE_H
.cpp file
#include "generatorshacode.h"
#include "ui_generatorshacode.h"
#include <windows.h>
#include "sha512.h"
#include <string>
#include <QtDebug>
GeneratorShaCode::GeneratorShaCode(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::GeneratorShaCode)
{
ui->setupUi(this);
GenerateCode = std::thread(&GeneratorShaCode::updateOutputEditText, this);
GenerateCode.detach();
}
GeneratorShaCode::~GeneratorShaCode()
{
delete ui;
}
QString GeneratorShaCode::Sha512Generator(QString Qstr)
{
return QString::fromStdString(sha512(Qstr.toStdString()));
}
void GeneratorShaCode::updateOutputEditText()
{
while(true)
{
ui->textEdit_Output->setText(Sha512Generator(ui->textEdit_Input->toPlainText()));
}
}
GUI thread is the main thread. You can't operate any ui controls directly within a sub thread. Usually you should use signals/slots between GUI thread and sub thread.

QT C++ prevent Runtime Error when close app

I'm writing a QT application and everything is fine with building and working but when i close the app with a thread running, the app closes and shows a message:
[Runtime Error....].
The application output says:
QThread: Destroyed while thread is still running
The program has unexpectedly finished.
The process was ended forcefully.
example crashed.
how to solve that ?
Example:
main.cpp
#include "mainwindow.h"
#include <QApplication>
#include <QString>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
example = new Thread(this);
connect(example, SIGNAL(print_line(QString)), this, SLOT(print_line2(QString)), Qt::BlockingQueuedConnection);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::print_line2(QString in)
{
ui->textBrowser->append(in);
}
void MainWindow::on_pushButton_clicked()
{
example->start();
}
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <Thread.h>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
Thread *example;
public slots:
void print_line2(QString in);
private slots:
void on_pushButton_clicked();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
Thread.cpp
#include "Thread.h"
#include <windows.h>
using namespace std;
Thread::Thread(QObject *parent):
QThread(parent)
{
}
Thread::~QThread()
{
delete ui;
}
void Thread::print(QString in)
{
print_line(in);
}
void Thread::run()
{
int count = 0;
for(;;) {
Sleep(100);
count += 1;
print(QString::number(count));
}
}
Thread.h
#ifndef READINFO_H
#define READINFO_H
#include <QThread>
class Thread : public QThread
{
Q_OBJECT
public:
explicit Thread(QObject *parent =0);
void print(QString in);
void run();
signals:
void print_line(QString x);
};
#endif
Ready example
The problem is that the thread is still in the running state when your application shuts down.
The code inside Thread::Run() contains an infinite loop -- there is no way for it to terminate by itself. You need to write your thread so that it will exit, or at the very least, you need to forcefully terminate the thread before your app closes.
To forcefully terminate, you could add two lines of code to the MainWindow destructor like so:
MainWindow::~MainWindow()
{
example->terminate();
example->wait();
(...)
}

QT signal-slot not received

I am trying to connect a signal from a second QMainWindow to the mainwindow. It doesn't say anything about a problem connection when the program is launched, but It doesn't work. I am not very familiar with C++ and Qt so maybe is something simple.
My code consists on a Mainwindow used as a SCADA with Start, stop, On, off buttons. In the second qmainwindow I created a terminal where you can type, start,stop... There, I would like to emit a signal to my MainWindow which is in charge of controlling the multiple threads and windows. The problem is that I cannot connect to my slot. I present here a simple overview of this two pieces of code.
Terminal. h
#ifndef TERMINAL__H
#define TERMINAL__H
#include <QMainWindow>
#include <QTextEdit>
#include <QLineEdit>
#include <QObject>
namespace Ui {
class Terminal_;
}
class Terminal_ : public QMainWindow
{
Q_OBJECT
public:
explicit Terminal_(QWidget *parent = 0);
~Terminal_();
signals:
void turnonPLC_terminal();
public slots:
void newline();
private:
Ui::Terminal_ *ui;
QTextEdit* mTerminal;
QLineEdit* mInput;
};
#endif // TERMINAL__H
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include "terminal_.h"
#include "terminal_help.h"
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
Terminal_ *terminal;
public slots:
void turnon_terminal();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
Mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "terminal_.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
terminal = new Terminal_(this);
connect(terminal, SIGNAL(turnonPLC_terminal()), this, SLOT(turnon_terminal()));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::turnon_terminal(){
turnonPLC=1;
}
terminal_.cpp
#include "terminal_.h"
#include "ui_terminal_.h"
#include <QDockWidget>
#include <QWidget>
#include <QLineEdit>
QString on=("on");
Terminal_::Terminal_(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::Terminal_)
{
ui->setupUi(this);
mTerminal = new QTextEdit();
setCentralWidget(mTerminal);
mInput = new QLineEdit();
QDockWidget* qdw = new QDockWidget;
qdw->setWidget(mInput);
addDockWidget(Qt::BottomDockWidgetArea, qdw);
connect (mInput, SIGNAL(returnPressed()),
this, SLOT(newline()));
}
Terminal_::~Terminal_()
{
delete ui;
}
void Terminal_::newline(){
QString command = mInput->text();
if (command==on){
emit turnonPLC_terminal();
}
}
Thanks
The signal-slots part in the code works perfectly. (compiled and tested with some small modifications)
After entering "on" (not On as written in question)
Terminal_::newline() slot called, turnonPLC_terminal() is fired and finally
void MainWindow::turnon_terminal() is called.
However, there are some small details the header file is called terminal_.h, not Terminal.h turnonPLC is not defined. terminal is created by not displayed (no show-call).
I guess, there are simply some many small logic errors. Try to use debugger or trace the chain of expected calls with qDebug.