[Qt-interest] QWidget::setCursor() and qApp->processEvents()
John McClurkin
jwm at nei.nih.gov
Thu Jul 29 13:38:21 CEST 2010
Josh wrote:
> Hello all,
>
> Before I do some calculations in my program I want to set the cursor
> using: viewport()->setCursor(Qt::WaitCursor);
>
> However, the cursor doesn't change because of the calculations. I don't
> want to call qApp->processEvents() during the calculations because I
> actually want the ui *not* to respond while the calculations are taking
> place. So I was able to get the cursor to update BEFORE the calculations
> as follows:
>
> qApp->processEvents();
> qApp->processEvents();
> viewport()->setCursor(Qt::WaitCursor); for(int i=0; i<20; i++)
> qApp->processEvents();
> do my calculations...
>
> This is obviously a hack. I needed to call processEvents 20 times for
> the cursor to update. Any less and the cursor wouldn't update before my
> calculations. I would assume that on different machines I might need
> different number of calls to processEvents(), which is undesirable.
> Anyone have any ideas on the correct way to do this?
>
> So in summary:
> How can I make sure the cursor is changed before doing my calculations
> when I don't want to call processEvents() during my calculations?
>
AFAIR, processEvents() collapses all pending requests into a single
call. Your mulitple calls on qApp->processEvents() essentially
constitute a busy loop that halts your program until qApp processes
events. You might play around with QTimer, setting a timeout after
setCursor() and calling processEvents() in the timer slot.
More information about the Qt-interest-old
mailing list