QTest::qSleep not working and shows some weird errors.



  • Hi I'm trying to load images in this QGraphicsView, once one second is over, another image is added to the window. I added QTest::qSleep(1000); inside the loops. Check the code:

    @for(int i=0;i<this->n;i++){
    for(int j=0;j<this->m;j++){
    if(mat[i][j]!=0 && mat[i][j]!=1){
    QPixmap *icon = new QPixmap(list.at(0));
    QGraphicsPixmapItem image = new QGraphicsPixmapItem(icon);
    scene->addItem(image);
    image->setPos(j
    icon->width()+200,i
    icon->height()+100);
    }
    QTest::qSleep(1000);
    QPixmap *icon = new QPixmap(list.at(mat[i][j]));
    QGraphicsPixmapItem image = new QGraphicsPixmapItem(icon);
    scene->addItem(image);
    image->setPos(j
    icon->width()+200,i
    icon->height()+100);

        }
    }@
    

    I added the library <QtTest/QTest>
    There are 44 issues, with almost the same error:
    undefined reference to '_imp___ZN5QTest5qWarnEPKcS1_i'

    Thanks for reading.



  • QTest belongs to "testlib":http://qt-project.org/doc/qt-5/QTest.html module, so you need to modify your .pro file
    @
    QT += testlib
    @

    Is this code running as a unit test?
    If it is not a unit-test then I would suggest to use "QTest static functions":http://qt-project.org/doc/qt-5/QThread.html#static-public-members
    @
    QThread::sleep()
    QThread::msleep()
    QThread::usleep()
    @

    In this case yuou don't need QTest and testlib.



  • [quote author="andreyc" date="1396634432"]QTest belongs to "testlib"...[/quote]

    Thanks for the reply, but what you mean about unit test? sorry Im not a native english speaker. You mean, my code in a single file? like .cpp?



  • QTest is a part of "Qt testing framework":http://qt-project.org/doc/qt-5/qtest-overview.html
    Usually you don't need to it in your regular application.
    Here is more info about "unit testing":http://en.wikipedia.org/wiki/Unit_testing


  • Lifetime Qt Champion

    Hi,

    To add to andreyc, if you are not writing a unit test (and it doesn't seem to be the case) do not use QTest. This module is not designed for application code, only unit tests.



  • [quote author="andreyc" date="1396642610"]QTest is a part...[/quote]
    All right I tried to make this work, but nothing happened, I tested the two options, neither works, the first one, give me two errors(could be that I already have QT += gui declarative int the .pro file), and the second one with QThread, works but my app freezes, didn't show the window at least.

    Maybe I should make another thread and explain what I'm trying to do. Or if you can help me, its cool to.



  • If you want to add some delay (why do you need it?) into your code and don't freeze UI then it is better to use QCoreApplication::processEvents() or QEventLoop::exec(). Something like this:
    @
    void processEventsAndWaitFor(int seconds)
    {
    if (seconds > 0) {
    QEventLoop loop;
    QTimer timer;
    QObject::connect(&timer, SIGNAL(timeout()), &loop, SLOT(quit()));
    int runTime = seconds * 1000;
    timer.start(runTime);
    loop.exec( );
    } else {
    QCoreApplication::processEvents( );
    }
    }

    for(int i=0;i<this->n;i++){
    for(int j=0;j<this->m;j++){
    if(mat[i][j]!=0 && mat[i][j]!=1){
    QPixmap *icon = new QPixmap(list.at(0));
    QGraphicsPixmapItem image = new QGraphicsPixmapItem(icon);
    scene->addItem(image);
    image->setPos(j
    icon->width()+200,i
    icon->height()+100);
    }
    processEventsAndWaitFor(1); // wait for one sec
    QPixmap *icon = new QPixmap(list.at(mat[i][j]));
    QGraphicsPixmapItem image = new QGraphicsPixmapItem(icon);
    scene->addItem(image);
    image->setPos(j
    icon->width()+200,i
    icon->height()+100);
    }
    }
    @


  • Lifetime Qt Champion

    Then you should rather make your function a slot and use QTimer::signleShot at the end of it. This requires to remove the for loops and refactor the logic a bit but it will be cleaner in the end.



  • [quote author="andreyc" date="1396645903"]If you want to..[/quote]
    Didn't see your reply, already made the post.. But anyways, I tried what you said, and the window appear, but the image is in the last position, is like, he move the image without showing the movement of the image, the window appears until every process is complete.
    I know your trying to solve this and I appreciate it, thank you so much bro.

    [quote author="SGaist" date="1396649126"]Then you should..[/quote]
    I'm gonna try it, just saw the replies right now, and the other solution was easy to implement, so I did that one first. Going to try your way and let you know if works.



  • Hi, any sleep function will usually halt the current thread and wait (that is one reason the sleep methods are in the QThread class), that is why it freezes your UI if you call sleep from the UI thread (the default main thread).

    To solve this you can either use a timer like SGaist said, or use your own background thread, the way I use simple background threads is with QtConcurrent::run (if you don't want or need to use your own QThread). You can simply provide a function pointer and the parameters to that function and it should be easy to use.
    @
    QtConcurrent::run(someFunction, 123, QString("hello"));
    @
    that will run someFunction in a background thread and pass an int and QString parameter to it. You can also use a lambda function if you are familiar with c++1. :)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.