Results 1 to 11 of 11

Thread: How to Show GUI and work on background

  1. #1
    Join Date
    Dec 2014
    Posts
    82
    Thanks
    10
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default How to Show GUI and work on background

    HI,

    I'm working on Qt5 and I have that in main:
    Qt Code:
    1. int main(int argc, char *argv[])
    2. {
    3. QApplication a(argc, argv);
    4.  
    5. MainWindow w;
    6. w.showMaximized();
    7. return a.exec();
    8. }
    To copy to clipboard, switch view to plain text mode 
    And then in the constructor I call some functions that will create folders and copy and move files.

    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent) :
    2. QMainWindow(parent),
    3. ui(new Ui::MainWindow)
    4. {
    5. ui->setupUi(this);
    6. [...] //Code that should work on background
    7. }
    To copy to clipboard, switch view to plain text mode 
    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!

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to Show GUI and work on background

    Either use asynchronous operations if available, or do the long tasks using threads. QtConcurrent can help you in some aspects here.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Dec 2014
    Posts
    82
    Thanks
    10
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to Show GUI and work on background

    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 -.-''
    Last edited by roseicollis; 22nd December 2014 at 10:24.

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to Show GUI and work on background

    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,
    _

  5. #5
    Join Date
    Dec 2014
    Posts
    82
    Thanks
    10
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to Show GUI and work on background

    Hi,

    Quote Originally Posted by anda_skoa View Post
    Since you have fixed sequence of functions, dervice from QThread and implement that sequence in its run() method.
    Ok so you mean to do :
    Qt Code:
    1. class MainWindow : public QMainWindow, QThread
    2. {
    3. Q_OBJECT
    4. ...
    5. }
    To copy to clipboard, switch view to plain text mode 

    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,

  6. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to Show GUI and work on background

    Quote Originally Posted by roseicollis View Post
    Ok so you mean to do :
    Qt Code:
    1. class MainWindow : public QMainWindow, QThread
    2. {
    3. Q_OBJECT
    4. ...
    5. }
    To copy to clipboard, switch view to plain text mode 

    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.

    Quote Originally Posted by roseicollis View Post
    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,
    _

  7. #7
    Join Date
    Dec 2014
    Posts
    82
    Thanks
    10
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to Show GUI and work on background

    Quote Originally Posted by anda_skoa View Post
    You can only derive from one QObject based class.
    Then? How can I derive from both?


    Quote Originally Posted by anda_skoa View Post
    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!

  8. #8
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to Show GUI and work on background

    Quote Originally Posted by roseicollis View Post
    Then? How can I derive from both?
    GUI can't be used outside main thread.

  9. #9
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to Show GUI and work on background

    Quote Originally Posted by roseicollis View Post
    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".

    Quote Originally Posted by roseicollis View Post
    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".

    Quote Originally Posted by roseicollis View Post
    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,
    _

  10. #10
    Join Date
    Dec 2014
    Posts
    82
    Thanks
    10
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to Show GUI and work on background

    Quote Originally Posted by anda_skoa View Post
    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!

  11. #11
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to Show GUI and work on background

    Quote Originally Posted by roseicollis View Post
    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,
    _

Similar Threads

  1. How to show a frameless dialog - DOESN'T WORK
    By franco.amato in forum Newbie
    Replies: 4
    Last Post: 2nd June 2010, 17:21
  2. Replies: 2
    Last Post: 21st March 2010, 08:20
  3. Replies: 5
    Last Post: 16th December 2009, 11:33
  4. background-image on QLabel does NOT work
    By VireX in forum Newbie
    Replies: 2
    Last Post: 8th June 2007, 20:30
  5. aLabel->show() does not seem to work
    By nass in forum Qt Programming
    Replies: 2
    Last Post: 18th September 2006, 14:42

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.