Flurite
|
  |
| Joined: 03 Apr 2011 |
| Total Posts: 5386 |
|
|
| 17 Mar 2012 06:11 PM |
Hi, I'm working in Qt Creator, specifically in a Qt Widget Project, and coding in C++.
It seems to be that infinite loops cause the widget project to not respond. Examine this pastebin code, which seems to cause the program to become unresponsive:
/7RVWmk9p
(note that most of the lines are commented out)
~Flurite |
|
|
| Report Abuse |
|
|
myrkos
|
  |
| Joined: 06 Sep 2010 |
| Total Posts: 8072 |
|
|
| 17 Mar 2012 06:19 PM |
| Yeah, in Qt you shouldn't have infinite loops in events. You can set a timer to run a function every now and then. |
|
|
| Report Abuse |
|
|
Flurite
|
  |
| Joined: 03 Apr 2011 |
| Total Posts: 5386 |
|
|
| 17 Mar 2012 06:23 PM |
like threading?
if so, how?
~Flurite |
|
|
| Report Abuse |
|
|
myrkos
|
  |
| Joined: 06 Sep 2010 |
| Total Posts: 8072 |
|
|
| 17 Mar 2012 06:26 PM |
| Create a QTimer object and set its timeout signal to whatever slot you want (I assume you're familiar with slots because it's what Qt uses for its events... if not then I'll try to explain) |
|
|
| Report Abuse |
|
|
Flurite
|
  |
| Joined: 03 Apr 2011 |
| Total Posts: 5386 |
|
|
| 17 Mar 2012 06:34 PM |
| Thank you very much. I'm going to look into it and reply back with any questions! |
|
|
| Report Abuse |
|
|
Flurite
|
  |
| Joined: 03 Apr 2011 |
| Total Posts: 5386 |
|
|
| 17 Mar 2012 06:49 PM |
Wow, in the first example om the documentation of QTimer, there are lots of things I don't understand.. like the * operator in front of the declaration of the timer, and the new data type or whatever, the connect() function, and oh dear!
Is there a simpler way to create a QTimer? |
|
|
| Report Abuse |
|
|
myrkos
|
  |
| Joined: 06 Sep 2010 |
| Total Posts: 8072 |
|
|
| 17 Mar 2012 06:52 PM |
Yeah, like:
QTimer timer;
connect(&timer,SIGNAL(timeout),this,SLOT(idkwhatever));
should work fine... I think... |
|
|
| Report Abuse |
|
|
Flurite
|
  |
| Joined: 03 Apr 2011 |
| Total Posts: 5386 |
|
| |
|
myrkos
|
  |
| Joined: 06 Sep 2010 |
| Total Posts: 8072 |
|
|
| 17 Mar 2012 06:56 PM |
| u use it to connect signals to slots... |
|
|
| Report Abuse |
|
|
Flurite
|
  |
| Joined: 03 Apr 2011 |
| Total Posts: 5386 |
|
|
| 17 Mar 2012 06:59 PM |
| is that somethin like built into the widget system? |
|
|
| Report Abuse |
|
|
myrkos
|
  |
| Joined: 06 Sep 2010 |
| Total Posts: 8072 |
|
| |
|
Flurite
|
  |
| Joined: 03 Apr 2011 |
| Total Posts: 5386 |
|
|
| 18 Mar 2012 08:30 AM |
i dun get it >:(
Clock_Application::Clock_Application(QWidget *parent) : QMainWindow(parent), ui(new Ui::Clock_Application) { ui->setupUi(this); QTimer *timer_Stopwatch = new QTimer(this); connect(timer_Stopwatch, SIGNAL(timeout()), this, SLOT(timer_Start())); timer_Stopwatch->start(1000); }
still not responding |
|
|
| Report Abuse |
|
|
Flurite
|
  |
| Joined: 03 Apr 2011 |
| Total Posts: 5386 |
|
|
| 18 Mar 2012 08:32 AM |
If you need it in a better format: /WcFJeaF1
Also, do you think using QThreads would work better? |
|
|
| Report Abuse |
|
|
myrkos
|
  |
| Joined: 06 Sep 2010 |
| Total Posts: 8072 |
|
|
| 18 Mar 2012 11:46 AM |
Do you have a timer_Start slot in your Clock_Application class? You can make one like:
class Clock_Application { Q_OBJECT public slots: void mySlot(void); };
void Clock_Application::mySlot(void) { I->like("pie"); } |
|
|
| Report Abuse |
|
|
Flurite
|
  |
| Joined: 03 Apr 2011 |
| Total Posts: 5386 |
|
|
| 18 Mar 2012 11:58 AM |
Oh I solved my little problem by putting this at the end of my loop:
QCoreApplication::processEvents() |
|
|
| Report Abuse |
|
|