A while(1) C function to run in Qt
Hello Guy,
I trying to put a while(1) C function in my Qt project.
I want to run it in the background of a specific Gui.
I have been trying to use threads to do it .I tried declaring the thread in main() and my Gui cpp file(infuison.cpp) But there seem to be a problem.
this is what i have done.
QThread: Destroyed while thread is still running
ASSERT failure in QThread::setTerminationEnabled(): "Current thread was not started with QThread.", file thread\qthread_win.cpp, line 574
Invalid parameter passed to C runtime function.
Invalid parameter passed to C runtime function.
Code:
//threading.h
#ifndef THREADINGS_H
#define THREADINGS_H
#include <QObject>
#include <QThread>
{
Q_OBJECT
public:
explicit Threadings
(QObject *parent
= 0);
signals:
public slots:
void runAlgoMain();
};
#endif // THREADINGS_H
Code:
//threadings.cpp
#include "threadings.h"
extern "C++"
{
#include "test.h"
}
Threadings
::Threadings(QObject *parent
) :{
}
void Threadings
::DoSetup(QThread &cThread
) {
connect(&cThread,SIGNAL(started()),this,SLOT(runAlgoMain()));
}
void Threadings::runAlgoMain()
{
runAlgoMain();
}
Code:
#include infusion.cpp
#include "threadings.h"
Infusion
::Infusion(QWidget *parent
) : ui(new Ui::Infusion)
{
Threadings cThreadings;
cThreadings.DoSetup(cThread);
cThreadings.moveToThread(&cThread);
cThread.start();
}
Am i doing threading right ? Please help :)
Re: A while(1) C function to run in Qt
Quote:
Am i doing threading right ?
Clearly not.
What is the lifetime of the QThread object at line 7 of your last listing?
See How To Really, Truly Use QThreads; The Full Explanation
Re: A while(1) C function to run in Qt
So i have to make an instance of a new qthread and put my function into it?
But how am i going to move my Function into the thread ? eg Worker *worker=new Worker . How to pass when my Function is an .c not a c++?
Anyway can we use QTimer to make my c functions run in the background just like threads ?
I am unsure of the Puclic slot :finished ();, from the link you gave me .
Re: A while(1) C function to run in Qt
C++ compilers don't care if you call "C" or "C++" functions in your programs.
Re: A while(1) C function to run in Qt
Quote:
Originally Posted by
wysota
C++ compilers don't care if you call "C" or "C++" functions in your programs.
Oh wait, My "C" functions are in a .C file . So i am unable to do Worker *worker=new Worker at all .
Re: A while(1) C function to run in Qt
Wrap your plain functions in a class and the problem is solved. It really is not complicated. Here is a complete, threaded program that prints powers of two:
Code:
#include <QCoreApplication>
#include <QObject>
#include <QThread>
#include <QDebug>
// Plain function with C linkage declaration
extern "C" {
int multiplyByTwo(int i);
}
// Plain function impl
int multiplyByTwo(int i) {
return i * 2;
}
Q_OBJECT
public:
Worker();
~Worker();
public slots:
void process();
signals:
void finished();
private:
// add your variables here
};
Worker::Worker() { }
Worker::~Worker() { }
// Start processing data.
void Worker::process() {
int value = 1;
while (value < 1000000000) {
qDebug() << value;
value = multiplyByTwo(value);
}
emit finished();
}
int main(int argc, char **argv) {
Worker* worker = new Worker;
worker->moveToThread(thread);
QObject::connect(thread,
SIGNAL(started
()), worker,
SLOT(process
()));
QObject::connect(worker,
SIGNAL(finished
()), thread,
SLOT(quit
()));
QObject::connect(worker,
SIGNAL(finished
()), worker,
SLOT(deleteLater
()));
QObject::connect(thread,
SIGNAL(finished
()), thread,
SLOT(deleteLater
()));
thread->start();
QObject::connect(thread,
SIGNAL(destroyed
()),
qApp,
SLOT(quit
()));
return app.exec();
}
#include "main.moc"
Re: A while(1) C function to run in Qt
Thanks guys i have found another way to "Threading ", maybe wrongly executed .
I did it by declaring the "C" function into a function in the cpp file .
Then i used a Qtimer to connect the timer to the function and start it.
Re: A while(1) C function to run in Qt
Quote:
Originally Posted by
020394
Thanks guys i have found another way to "Threading ", maybe wrongly executed .
I did it by declaring the "C" function into a function in the cpp file .
Then i used a Qtimer to connect the timer to the function and start it.
There is no threading here, timers do not spawn new threads.
Re: A while(1) C function to run in Qt
Try to use the QtConcurrent::run or the QtConcurrent::map instead the QThread.
Re: A while(1) C function to run in Qt
So when can I use the QTimer ? I am trying to use the QTimer to run a function along with the Gui. Is it the right way to ?
Re: A while(1) C function to run in Qt
A timer is for running some code in a specific moment in time. It is handled by an event like (almost) everything else in Qt.
Re: A while(1) C function to run in Qt
Quote:
Originally Posted by
wysota
A timer is for running some code in a specific moment in time. It is handled by an event like (almost) everything else in Qt.
Isnt It the same as running threads ? For now i am doing a school project with different groups all doing different part . for me i am doing the Gui. I need to integrate the rest of the codes my friends do . EG (Algorithm , Checking of a keypad. etc). Am i able to use QTimer for them since i want them to run at a specific moment of time when i am at the Gui page
Re: A while(1) C function to run in Qt
Quote:
Originally Posted by
020394
Isnt It the same as running threads ?
No. If you are to eat lunch at 12 o'clock, it doesn't mean at 12 some other instance of yourself is spawned and eats a sandwich but rather you have to stop what you had been doing, take out a sandwich, unwrap it, eat it and then attend your other tasks.
Re: A while(1) C function to run in Qt
I see so threading is always running in the background . But i been using timer to check the current time(Updatiing the time on the Label) , Isnt that considered as threadings?
Re: A while(1) C function to run in Qt
Quote:
Originally Posted by
020394
I see so threading is always running in the background . But i been using timer to check the current time(Updatiing the time on the Label) , Isnt that considered as threadings?
No. Timers have nothing to do with threading.