Reputation: 3220
nearly searching for hours I became more confused about the Multithreading concept and I need help understanding and implementing it. After searching I have come to the following implementation
main.cpp
#include <QtGui/QApplication>
#include "mainwindow.h"
#include <mythread1.h>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
mythread abc;
abc.start();
abc.wait();
return a.exec();
}
In above code mythread.h
is my header file which I created for multithreading and below is my code of mythread1.h
mythread1.h
#ifndef MYTHREAD1_H
#define MYTHREAD1_H
#include <QtGui>
#include <windows.h>
class mythread : public QThread
{
public:
void run();
};
void mythread::run()
{
}
#endif // MYTHREAD_H
Now my questions are
QFile::copy("pathtomytargetfile","targetpath");
inside the run function inside in mythread1.cpp but that didn't work :O and it didn't copy that file but when i connected it with Push button in main thread then it got successfully copied, why this happened?Thank you
Upvotes: 1
Views: 352
Reputation: 3852
My answer might confuse you even more, but the following links are worth reading in my opinion:
QThread
class. He apologizes for suggesting that inheriting from QThread
is the way to go.QThread
will work as well, it just is not as nice design-wise).HTH
Upvotes: 1
Reputation: 13130
First of all: run method should be protected!
Secondly, what do you mean by "I have defined only one function run()". You defined method, not function. abc.start()
means that abc instance of mythread will start and use it's entry point method void run()
. You can't declare more than one method with same signature. And if you wonder how it calls your run()
instead of QThread::run()
then you need to read something about virtual methods in C++
Ad. 3: How can we tell why your file didn't copy? Check what errors QFile provides you. Change your code something like that:
QFile f("pathtomytargetfile");
if( ! f.copy("targetpath") )
{
qDebug() << f.errorString();
}
It will provide you some usefull info
Upvotes: 1