PDA

View Full Version : How to Show GUI and work on background



roseicollis
22nd December 2014, 09:37
HI,

I'm working on Qt5 and I have that in main:

int main(int argc, char *argv[])
{
QApplication a(argc, argv);

MainWindow w;
w.showMaximized();
return a.exec();
}

And then in the constructor I call some functions that will create folders and copy and move files.


MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
[...] //Code that should work on background
}

What I want is to show the gui (where you have/see a picture with move with some text that changes) while the program works on the background but what it does is to do all the background job and once it has finished, then the GUI appears.

Thank you!

wysota
22nd December 2014, 09:52
Either use asynchronous operations if available, or do the long tasks using threads. QtConcurrent can help you in some aspects here.

roseicollis
22nd December 2014, 10:08
Hi,

If I have in the constructor Fn1, Fn2 and Fn3 which are call lineary (first goes Fn1, then Fn2 and so on, but never at the same time) then how and where should I make the threads?

QThread *mythread = new QThread;

and now? and where do you put it? I have never work before with threads -.-''

anda_skoa
22nd December 2014, 10:24
Since you have fixed sequence of functions, dervice from QThread and implement that sequence in its run() method.
Then create an instance of that class and run it.

You could also create a second, non-UI program and start that from the UI program using QProcess.

Cheers,
_

roseicollis
22nd December 2014, 10:57
Hi,


Since you have fixed sequence of functions, dervice from QThread and implement that sequence in its run() method.
Ok so you mean to do :

class MainWindow : public QMainWindow, QThread
{
Q_OBJECT
...
}

Then it says In member function 'virtual const QMetaObject* MainWindow::metaObject() const':/home/sg/Docs/.../moc_mainwindow.cpp:62: error: 'QObject' is an ambiguous base of 'MainWindow'


You could also create a second, non-UI program and start that from the UI program using QProcess.
Ok here I'm completly lost ^^'' can you explain that better please?

Thanks,

anda_skoa
22nd December 2014, 11:19
Ok so you mean to do :

class MainWindow : public QMainWindow, QThread
{
Q_OBJECT
...
}

Then it says In member function 'virtual const QMetaObject* MainWindow::metaObject() const':/home/sg/Docs/.../moc_mainwindow.cpp:62: error: 'QObject' is an ambiguous base of 'MainWindow'

You can only derive from one QObject based class.



Ok here I'm completly lost ^^'' can you explain that better please?

QProcess is a Qt class that allows you to run other programs/executables.
So one parallel processing approach is to have a task program that can be blocking/sequential and have the main program run it via QProcess.

Cheers,
_

roseicollis
22nd December 2014, 12:13
You can only derive from one QObject based class.
Then? How can I derive from both?



QProcess is a Qt class that allows you to run other programs/executables.
So one parallel processing approach is to have a task program that can be blocking/sequential and have the main program run it via QProcess.

Do you mean that I have to make 2 projects and then unify them with a process?? If yes can you put the code or the idea more or less of it? I can't understand how can the code be.
Sorry for all, I'm really newbie with Qt and developing with threads/process/etc. I've been looking for some written examples to try to understand it more but can't find any for that, so could you please write it?

Thank you!

Lesiok
22nd December 2014, 12:27
Then? How can I derive from both?

GUI can't be used outside main thread.

anda_skoa
22nd December 2014, 13:58
Then? How can I derive from both?

Didn't I just write that this is not possible?
How does asking how to do it make any sense?
Obviously there is no "how".



Do you mean that I have to make 2 projects and then unify them with a process??

That is an option.
You can also have a project that contains two exectuables.
The template for a multi target project is called "subdirs".



If yes can you put the code or the idea more or less of it? I can't understand how can the code be.

The idea is two have two programs:
1) a GUI that needs to be event driven to stay responive
2) a simple non-GUI worker program that can block until it is done

Then have the GUI program run the non-GUI program using QProcess.

Multi-Process is an alternative to multithreading with slightly different properties.

Depends a lot on what that parallel task is, how much it needs to share data with the UI and how often it needs to communicate.

Cheers,
_

roseicollis
22nd December 2014, 14:44
Didn't I just write that this is not possible?
How does asking how to do it make any sense?
Obviously there is no "how".
As you said: "Since you have fixed sequence of functions, dervice from QThread and implement that sequence in its run() method."
Though you meant I had to inherit from it too.

I'll search more about what you think of exes :) Thank you!

anda_skoa
22nd December 2014, 14:53
As you said: "Since you have fixed sequence of functions, dervice from QThread and implement that sequence in its run() method."
Though you meant I had to inherit from it too.

Yes, you thought correctly.
The way to reimplement a virtual method in C++ is to overwrite it, meaning you derive from the class and implement the method again in that new class.

Cheers,
_