Results 1 to 13 of 13

Thread: QFuture and QtConcurrent Problem

  1. #1
    Join Date
    Jan 2007
    Posts
    201
    Thanks
    22
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default QFuture and QtConcurrent Problem

    Dear All

    I have problem about my application which is Qt 4.8.1 when using QFuture and QtConcurrent i get error code

    QObject::setParent: Cannot set parent, new parent is in a different thread
    QPixmap: It is not safe to use pixmaps outside the GUI thread
    QObject::startTimer: timers cannot be started from another thread
    QObject::setParent: Cannot set parent, new parent is in a different thread

    And also my application keep crushing unexpectedly. I never know when it going to crush.

    I would be so happy is someone can help me.

    Here is my code
    tryfuture.h
    Qt Code:
    1. #ifndef TRYFUTURE_H
    2. #define TRYFUTURE_H
    3.  
    4. #include <QtGui>
    5. #include <QDialog>
    6. #include <QMessageBox>
    7. #include <QTime>
    8. #include <qtconcurrentrun.h>
    9. #include <QThread>
    10.  
    11. namespace Ui {
    12. class tryfuture;
    13. }
    14.  
    15. class tryfuture: public QWidget
    16. {
    17. Q_OBJECT
    18. int pagenumber;
    19. public:
    20. explicit tryfuture(QWidget *parent = 0);
    21. ~tryfuture();
    22. public slots:
    23. void test1();
    24. void test2();
    25. void test3();
    26. private:
    27. Ui::tryfuture*ui;
    28. };
    29. #endif //
    To copy to clipboard, switch view to plain text mode 

    tryfuture.cpp
    Qt Code:
    1. tryfuture::tryfuture(QWidget *parent) :
    2. QWidget(parent),
    3. ui(new Ui::tryfuture)
    4. {
    5.  
    6. QTimer *changetimer= new QTimer(this);
    7. connect(changetimer, SIGNAL(timeout()), this, SLOT(changepage()));
    8. changetimer->start(10000);
    9.  
    10. }
    11.  
    12. void tryfuture::changepage(){
    13.  
    14. QFuture<void> future;
    15.  
    16. switch(pagenumber%3)
    17. {
    18. case 0:
    19. future = QtConcurrent::run(this, &tryfuture::test1);
    20. break;
    21. case 1:
    22. future = QtConcurrent::run(this, &tryfuture::test2);
    23. break;
    24. case 2:
    25. future = QtConcurrent::run(this, &tryfuture::test3);
    26. break;
    27. }
    28.  
    29. pagenumber++;
    30. }
    31. void tryfuture::test1(){
    32. QMessageBox::information(this, tr("Qt Example 1"),
    33. tr("The function my_func has finished."));
    34. }
    35.  
    36. void tryfuture::test2(){
    37. QMessageBox::information(this, tr("Qt Example 2"),
    38. tr("The function my_func has finished."));
    39. }
    40. void formUretimBilgiEkrani::test3(){
    41. QMessageBox::information(this, tr("Qt Example 3"),
    42. tr("The function my_func has finished."));
    43. }
    To copy to clipboard, switch view to plain text mode 

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

    Default Re: QFuture and QtConcurrent Problem

    The messages clearly indicate what is wrong -- there are some resources in Qt (like all widget classes, pixmaps, etc.) that cannot be used from within worker threads.
    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 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: QFuture and QtConcurrent Problem

    if you want to know if something has happened use qDebug()

    Aside from threading issues logging is way less intrusive and interrupting than message boxes popping up everywhere.

    Cheers,
    _

  4. #4
    Join Date
    Jan 2007
    Posts
    201
    Thanks
    22
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QFuture and QtConcurrent Problem

    What is the solution for this?

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

    Default Re: QFuture and QtConcurrent Problem

    As already said, use qDebug() instead of message boxes.
    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.


  6. #6
    Join Date
    Jan 2007
    Posts
    201
    Thanks
    22
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QFuture and QtConcurrent Problem

    Normally I use QSqlQuery and post them on tree in QStandardItemModel. But this operation takes time. So i use QFuture. So that would be the solution if I use QStandardItemModel or QTableModel in QFuture?

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

    Default Re: QFuture and QtConcurrent Problem

    The concurrent operation should return a container with data required to build the model in the main thread.
    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.


  8. #8
    Join Date
    Jan 2007
    Posts
    201
    Thanks
    22
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QFuture and QtConcurrent Problem

    Ok, i have change my code like this, but i still get an error like this.

    QObject::connect: Cannot connect (null)::layoutAboutToBeChanged() to ProxyTableView::beforeLayoutChange()

    fromBilgiEkrani.h
    Qt Code:
    1. #ifndef BILGIEKRANI_H
    2. #define BILGIEKRANI_H
    3.  
    4. #include <QtGui>
    5. #include <QtSql>
    6. #include <QWidget>
    7. #include <QDateTime>
    8. #include <QSqlDatabase>
    9. #include <qtconcurrentrun.h>
    10.  
    11. namespace Ui {
    12. class fromBilgiEkrani;
    13. }
    14.  
    15. class fromBilgiEkrani : public QWidget
    16. {
    17. Q_OBJECT
    18. public:
    19. explicit fromBilgiEkrani(QWidget *parent = 0);
    20. ~fromBilgiEkrani();
    21.  
    22. public slots:
    23. bool loadSql();
    24. void loadTable(QSqlTableModel *model);
    25.  
    26. private:
    27.  
    28. Ui::fromBilgiEkrani *ui;
    29. };
    30.  
    31. #endif
    To copy to clipboard, switch view to plain text mode 

    fromBilgiEkrani.cpp

    Qt Code:
    1. #include "fromBilgiEkrani.h"
    2. #include "ui_fromBilgiEkrani.h"
    3.  
    4. fromBilgiEkrani::fromBilgiEkrani(QWidget *parent) :
    5. QWidget(parent),
    6. ui(new Ui::fromBilgiEkrani)
    7. {
    8.  
    9. Qt::WindowFlags flags;
    10. flags = Qt::Window;
    11. QWidget::setWindowFlags(flags);
    12.  
    13. ui->setupUi(this);
    14.  
    15. QTimer *sayfadegistir = new QTimer(this);
    16. connect(sayfadegistir, SIGNAL(timeout()), this, SLOT(slotSayfaDegistir()));
    17. sayfadegistir->start(2000);
    18. }
    19.  
    20. fromBilgiEkrani::~fromBilgiEkrani()
    21. {
    22. delete ui;
    23. }
    24.  
    25. void fromBilgiEkrani::slotSayfaDegistir(){
    26.  
    27. QFuture<void> future;
    28. future = QtConcurrent::run(this, &fromBilgiEkrani::uretimAcikIsler);
    29. break;
    30. loaduretimAcikIsler(model);
    31. }
    32.  
    33. bool fromBilgiEkrani::uretimAcikIsler(){
    34.  
    35. bool success = false;
    36.  
    37. model = new QSqlTableModel(this);
    38. model->setTable("sqlview");
    39. model->select();
    40.  
    41. return success;
    42. }
    43.  
    44. void fromBilgiEkrani::loaduretimAcikIsler(QSqlTableModel *model){
    45. ui->tableAcikIsler->setModel(isclik);
    46. ui->tableAcikIsler->resizeColumnsToContents();
    47. }
    To copy to clipboard, switch view to plain text mode 

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

    Default Re: QFuture and QtConcurrent Problem

    Hmm.... Does this code even compile? It definitely shouldn't as uretimAcikIsler isn't static. Even if it was, you'd still get errors on setting a parent from another thread (line #37).

    Maybe you should start from the beginning and tell us what you are trying to achieve?
    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 2007
    Posts
    201
    Thanks
    22
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QFuture and QtConcurrent Problem

    I dont if it compiles, i try to reduce my core before sending. I have changed the class names and everything.

    What i am trying to is, i want create my QSqlTableModel in my Future, and after that i want to publish them in a QTableView. Since in QFuture i cant use QTableView, so i made a separate function for this. But then i try to use that function i get an error QObject::connect: Cannot connect (null)::layoutAboutToBeChanged() to ProxyTableView::beforeLayoutChange()

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

    Default Re: QFuture and QtConcurrent Problem

    In future functions you can't use QSqlTableModel as well, at least not the way you are trying to do this. As I said befor, you should gather the data in the concurrent function and then use the data in the main thread to construct the model. Also remember that if you want to connect to SQL database from a worker thread, you will have to establish a separate SQL connection for that thread and you will have to do that from within that thread.
    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. #12
    Join Date
    Jan 2007
    Posts
    201
    Thanks
    22
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QFuture and QtConcurrent Problem

    Is there any example for this?

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

    Default Re: QFuture and QtConcurrent Problem

    I think there is a "threaded item model" example bundled with Qt. But in general I said everything that needs to be said -- the concurrent function should return data which then should be fed into the model using a custom method.
    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.


Similar Threads

  1. Replies: 4
    Last Post: 14th January 2013, 10:41
  2. problem with QtConcurrent::run
    By etognoni in forum Newbie
    Replies: 2
    Last Post: 4th April 2012, 23:14
  3. Problem with QtConcurrent::run
    By januszmk in forum Newbie
    Replies: 5
    Last Post: 30th July 2011, 17:57
  4. QtConcurrent mappedReduced QFuture Core Dump
    By shawno in forum Qt Programming
    Replies: 0
    Last Post: 16th July 2010, 02:29
  5. Replies: 14
    Last Post: 8th September 2009, 11:01

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
  •  
Qt is a trademark of The Qt Company.