Results 1 to 14 of 14

Thread: I`m doing it wrong....

  1. #1
    Join Date
    Jan 2012
    Location
    Argentina
    Posts
    167
    Thanks
    33
    Thanked 10 Times in 10 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default I`m doing it wrong....

    Hi guys im having kind of a headache understanding threads in Qt.
    I`ve already read some post about QThread and the You are doing it blog entry but somehow I cant understand the full concep...
    My idea is to show a loading widget while i load my application data. So far I ve the following code:

    Qt Code:
    1. #include <QtGui/QApplication>
    2. #include <QObject>
    3. #include "waitthread.h"
    4. #include "loadthread.h"
    5.  
    6. int main(int argc, char *argv[])
    7. {
    8. QApplication a(argc, argv);
    9. QApplication::setStyle (new IconSize ());
    10.  
    11. LoadThread * load_thread = new LoadThread ();
    12. WaitThread * wait_thread = new WaitThread ();
    13.  
    14. QObject::connect (load_thread, SIGNAL(load_finished()), wait_thread, SLOT(loading_finished()));
    15.  
    16. load_thread->start ();
    17. wait_thread->start ();
    18.  
    19. return a.exec ();
    20. }
    To copy to clipboard, switch view to plain text mode 

    the loadthread.cpp:

    Qt Code:
    1. LoadThread::LoadThread()
    2. {
    3. mainWin = new MainWindow ();
    4. }
    5.  
    6. void LoadThread::run (){
    7. //--------------------do some stuff
    8. emit load_finished ();
    9. emit load_finished ();
    10. show_mainWin ();
    11. }
    12.  
    13. void LoadThread::show_mainWin (){
    14. mainWin->show ();
    15. }
    To copy to clipboard, switch view to plain text mode 

    And the waitThread.cpp

    Qt Code:
    1. #include "waitthread.h"
    2.  
    3. WaitThread::WaitThread()
    4. {
    5. loadwidg = new LoadingWidget ();
    6. loadwidg->show ();
    7. }
    8.  
    9. void WaitThread::run (){
    10. }
    11.  
    12. void WaitThread::loading_finished (){
    13. loadwidg->close ();
    14. delete loadwidg;
    15. this->terminate ();
    16. }
    To copy to clipboard, switch view to plain text mode 


    I think that the signal is received because the loading widget is closed...but mainWin only pops up for a split sec, like the object Qthread is destroyed...I know that im designing this the wrong way...hope someone can give a hand to fully understand this...

  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: I`m doing it wrong....

    Threads can't operate on widgets. Period.

    Read this: [wiki]Keeping the GUI Responsive[/wiki]
    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
    Jan 2012
    Location
    Argentina
    Posts
    167
    Thanks
    33
    Thanked 10 Times in 10 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: I`m doing it wrong....

    Ok I read the article. But still cannot crack it. Somehow I think my best course of action to achieve what I want (show a widget smoothly while mainWindow and its components are loaded) is to use the QTimer::singleShot.
    I´ve changed my whole code but i cannot get the whole idea of the timer I guess
    So far I have this:

    Qt Code:
    1. #include <QtGui/QApplication>
    2. #include <QObject>
    3. #include <QDesktopWidget>
    4. #include "loadthread.h"
    5.  
    6. int main(int argc, char *argv[])
    7. {
    8. QApplication a(argc, argv);
    9. QApplication::setStyle (new IconSize ());
    10.  
    11. LoadingWidget *loadwidg = new LoadingWidget ();
    12. loadwidg->show ();
    13. QApplication::setStyle (new IconSize ());
    14. MainWindow mainWin;
    15. LoadThread * load_thread = new LoadThread (&mainWin);
    16. QObject::connect (load_thread, SIGNAL(load_finished()), loadwidg, SLOT(load_finished()));
    17. QTimer::singleShot (0,load_thread, SLOT(load()));
    18. mainWin.show ();
    19. return a.exec ();
    20. }
    To copy to clipboard, switch view to plain text mode 

    And the load class does this
    Qt Code:
    1. #include "loadthread.h"
    2.  
    3. LoadThread::LoadThread(MainWindow * mainWin)
    4. {
    5. this->mainWin = mainWin;
    6. }
    7.  
    8. void LoadThread::load (){
    9.  
    10. mainWin->setWindowTitle("Los Taninos Vinoteca");
    11. mainWin->setWindowFlags (Qt::WindowTitleHint | Qt::CustomizeWindowHint | Qt::WindowMinimizeButtonHint);
    12. //centrar imagen
    13. mainWin->setGeometry( QStyle::alignedRect(Qt::LeftToRight, Qt::AlignCenter,mainWin->size(),qApp->desktop()->availableGeometry()));
    14. //some not interesting code
    15. if (handler >= 0 && aux ==0 && capacidad >0){
    16. mainWin->setHandler (handler);
    17. }else{
    18. if (capacidad == 0){
    19. QMessageBox::critical (0, QString ("NO MORE REGISTERS TO STORE DATA"), QString ("No quedan más registros para guardar información fiscal\n\nNo se podrá realizar ninguna operación\nrelacionada con el controlador."), QMessageBox::Ok);
    20. }else QMessageBox::information (0, QString ("NO FISCAL PRINTER"),QString ("No se ha encontrado el controlador fiscal\nconectado, no se podrá facturar"));
    21. mainWin->setHandler (0);
    22. }
    23. emit load_finished ();
    24. }
    To copy to clipboard, switch view to plain text mode 

    Doing this the loading widget still not running at 40ms
    Last edited by KillGabio; 12th February 2012 at 21:04.

  4. #4
    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: I`m doing it wrong....

    What exactly would you expect this code to do?
    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.


  5. #5
    Join Date
    Jan 2012
    Location
    Argentina
    Posts
    167
    Thanks
    33
    Thanked 10 Times in 10 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: I`m doing it wrong....

    To show the widget (consisting of a label and Qmovie attached to another label) and do loading operations without afecting the .gif movie smooth visualization. My problem is that the operations of loading slow the presentation of the gif and I dont want that

  6. #6
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: I`m doing it wrong....

    My idea is to show a loading widget while i load my application data. So far I ve the following code:
    QSplashScreen seems a perfect place to start.

  7. #7
    Join Date
    Jan 2012
    Location
    Argentina
    Posts
    167
    Thanks
    33
    Thanked 10 Times in 10 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: I`m doing it wrong....

    I`ve read the documentatios of splashscreen, but I think it isnt possible to display .gif , is it? As it is not editable i cannot add a for example two labels as I have, and the fact that by clicking it, it disappears I dont like it, lol...

  8. #8
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: I`m doing it wrong....

    Let's see:
    • QSplashScreen displays a pixmap, which could be from a GIF. An animated GIF won't work as-is, but you could subclass QSplashScreen, and use a QMovie to provide frame-by-frame pixmaps. Your main problem would be calling qApp->processEvents() often enough.
    • QSplashScreen displays a message that can be changed as the load process continues
    • QSplashScreen disappears when the mouse is clicked (by default and user expectation) but you could override the mousePressEvent() and ignore that.


    Worst case, you could look at how QSpashScreen is built and mimic it.

    It's only a suggestion.

  9. #9
    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: I`m doing it wrong....

    If you really want your program to waste processing power on showing an animation then display the animation in the main thread and do the loading in another thread but focus on loading data and not manipulating widgets. Widgets can only be accessed from the main thread. Other than that, you can use the "solving the problem step by step" approach from the article I gave you a link to but it won't be as easy as writing play() and load().
    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.


  10. #10
    Join Date
    Jan 2012
    Location
    Argentina
    Posts
    167
    Thanks
    33
    Thanked 10 Times in 10 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: I`m doing it wrong....

    This is the class i`ve implemented so far....the problem is that as it is a Widget tha gif is shown sooo slow i can go to the bath come back and it hasn`t move. When the mainWindow is displayed I can see that the gif of the widget moves like super fast...like when a movie is freezed and when the focus returns it starts to move like x10 speed..Maybe the delay is produced by my code.. I dont know

    Qt Code:
    1. LoadingWidget::LoadingWidget(QWidget *parent) :
    2. QWidget(parent),
    3. ui(new Ui::LoadingWidget)
    4. {
    5. ui->setupUi(this);
    6. this->setWindowTitle ("Loading Gestion Vinoteca");
    7. var_timer = 0;
    8. load_gif = new QMovie (":/ImagesMilk/Iconos/zombiewalk.gif");
    9. ui->label_gif->setMovie (load_gif);
    10. timer = new QTimer (this);
    11. this->setWindowFlags (Qt::Window |Qt::CustomizeWindowHint);
    12. connect (timer, SIGNAL(timeout()), this, SLOT(update()));
    13. timer->start (1000);
    14. load_gif->start ();
    15. }
    16.  
    17. LoadingWidget::~LoadingWidget()
    18. {
    19. delete timer;
    20. delete load_gif;
    21. delete ui;
    22. }
    23.  
    24. void LoadingWidget::update (){
    25. var_timer = (var_timer+1) %4;
    26. switch (var_timer){
    27. case 0:
    28. ui->label_text_loading->setText ("Loading");
    29. break;
    30. case 1:
    31. ui->label_text_loading->setText ("Loading.");
    32. break;
    33. case 2:
    34. ui->label_text_loading->setText ("Loading..");
    35. break;
    36. case 3:
    37. ui->label_text_loading->setText ("Loading...");
    38. break;
    39. }
    40. }
    41.  
    42. void LoadingWidget::load_finished (){
    43. this->close ();
    44. }
    To copy to clipboard, switch view to plain text mode 

  11. #11
    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: I`m doing it wrong....

    Are you sure you read the article I gave you with proper care? By the way, update() is a method defined in QWidget that has a particular meaning.
    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.


  12. The following user says thank you to wysota for this useful post:

    KillGabio (12th February 2012)

  13. #12
    Join Date
    Jan 2012
    Location
    Argentina
    Posts
    167
    Thanks
    33
    Thanked 10 Times in 10 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: I`m doing it wrong....

    I must admit I read it with proper care, but not with proper understanding. As you and I said a good approach is the "step by step"...but im also finding it difficult to implement..

  14. #13
    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: I`m doing it wrong....

    Can't eat the cake and still have the cake. You must put some effort to have results.
    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.


  15. #14
    Join Date
    Jan 2012
    Location
    Argentina
    Posts
    167
    Thanks
    33
    Thanked 10 Times in 10 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: I`m doing it wrong....

    Quote Originally Posted by wysota View Post
    Can't eat the cake and still have the cake. You must put some effort to have results.
    Jajaja I know you are right I just wish I have more time to finish it properly. I know im duplicating code and some other mistakes, the thing is that im leaving town this Wends and I need to finish the app...So this last month has been very mentally busy for me: learning qt, oracle and the fucking fiscal printer API that by the way its documentation is badly written so I had to almost guess every command (thought installing the OCX would be more difficult than communicating with Strings, guess I was wrong lol)...Im going to sue those Hasar developing team...All that said Im very grateful to you and chris specially for all the help given

    And again sorry if i had some spellmistakes lol

Similar Threads

  1. What am I doing wrong??
    By Splatify in forum Newbie
    Replies: 3
    Last Post: 7th February 2011, 16:47
  2. What do I do wrong
    By Arif Bilgin in forum Newbie
    Replies: 12
    Last Post: 20th October 2010, 20:03
  3. i don't know what i do wrong..
    By Hardstyle in forum Newbie
    Replies: 2
    Last Post: 27th June 2010, 17:33
  4. what is wrong with my qt ?
    By lwb422 in forum Qt Programming
    Replies: 5
    Last Post: 12th April 2010, 14:07
  5. What's wrong??
    By dreamer in forum Qt Programming
    Replies: 2
    Last Post: 25th June 2008, 08:07

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.