Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QTest::qSleep not working and shows some weird errors.
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved General and Desktop
10 Posts 4 Posters 4.7k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    vitaR
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • A Offline
      A Offline
      andreyc
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • V Offline
        V Offline
        vitaR
        wrote on last edited by
        #3

        [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?

        1 Reply Last reply
        0
        • A Offline
          A Offline
          andreyc
          wrote on last edited by
          #4

          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

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            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.

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            0
            • V Offline
              V Offline
              vitaR
              wrote on last edited by
              #6

              [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.

              1 Reply Last reply
              0
              • A Offline
                A Offline
                andreyc
                wrote on last edited by
                #7

                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);
                }
                }
                @

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  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.

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  0
                  • V Offline
                    V Offline
                    vitaR
                    wrote on last edited by
                    #9

                    [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.

                    1 Reply Last reply
                    0
                    • X Offline
                      X Offline
                      Xander84
                      wrote on last edited by
                      #10

                      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. :)

                      1 Reply Last reply
                      0

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved