Josh Beckwith
Josh Beckwith

Reputation: 1540

QLabel not responding to `setText()` at the time of calling

I am using the QT IDE to make a better looking version of my (once working) command line auto clicker.

Mind you, the clicking still works great... the functionality is not compromised.

However, the problem comes when 'updating' the text fields on the form itself.

In my code, you can see that pressing the 'Q' button, ends the loop. At this point, the text fields become updated (as they should be continuously during the loop). I'm really not sure why they aren't updating until I hit 'Q'. Is there any reason this isn't supposed to work as I think it should, or is there a way to fix it?

Thanks. See my code below. Mind you, I will only include the bits I believe are necessary. Upon request I can post more code.

CODE.cpp

bool running = 1;
float x = 1500;
float interval;
int breakCounter = 0;
float breakSeconds = 0;
int clicks = 0;

Sleep (3000);

while (running == 1)
{
    float breakChance = (rand()%650+1);
    if (breakChance == 100){
        breakCounter++;

        float breakTime = (rand()%180+1);
        breakTime = (breakTime + 60) * 1000;
        breakSeconds = breakSeconds + breakTime/1000;

        QString a = QString::number(breakCounter);
        QString b = QString::number(breakSeconds/60);
        QString c = QString::number(breakTime/1000);

        ui->label_6->setText(a);
        ui->label_7->setText(b + " mins");
        ui->label_8->setText(c);
        ui->label_9->setText("N/A");
        Sleep (breakTime);
    } else {
        float variance = (rand()%500+1);
        int flip = (rand()%2+1);
        if (flip == 1){
            interval = x + variance;
        } else {
            interval = x - variance;
        }

        QString a = QString::number(breakCounter);
        QString b = QString::number(breakSeconds/60);
        QString d = QString::number(interval);

        ui->label_6->setText(a);
        ui->label_7->setText(b + " mins");
        ui->label_8->setText("N/A");
        ui->label_9->setText(d);

        Sleep (interval);

        mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
        Sleep (100);
        mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);

        clicks++;
        if (GetAsyncKeyState( 'Q' ))
        {
            running = 0;
        }
    }

}

CODE.ui

<item>
 <layout class="QHBoxLayout" name="horizontalLayout_3">
  <item>
   <widget class="QLabel" name="label_4">
    <property name="text">
     <string>Current break timer:</string>
    </property>
   </widget>
  </item>
  <item>
   <widget class="QLabel" name="label_8">
    <property name="text">
     <string>N/A</string>
    </property>
   </widget>
  </item>
 </layout>
</item>
<item>
 <layout class="QHBoxLayout" name="horizontalLayout_4">
  <item>
   <widget class="QLabel" name="label_5">
    <property name="text">
     <string>Next click time:</string>
    </property>
   </widget>
  </item>
  <item>
   <widget class="QLabel" name="label_9">
    <property name="text">
     <string>N/A</string>
    </property>
   </widget>
  </item>
 </layout>
</item>

Upvotes: 0

Views: 1276

Answers (1)

Qt's paint messages happen when the application message pump is given a chance to run. What you appear to have here is a loop that keeps your code on the stack and doesn't relinquish control back to the message pump.

If you want delays...the way to do that is with a QTimer. Say how long you want it to be until your code runs next, return out of your code, and give the message pump a chance to run while time passes.

Upvotes: 2

Related Questions