[Qt-interest] strange behavior of spinbox when sleeping (Linux only)
Christian Gagneraud
cgagneraud at techworks.ie
Sun Nov 21 19:12:18 CET 2010
On 11/20/2010 03:28 PM, TP wrote:
> Hello,
>
> I obtain a strange behavior of QSpinBox when connecting its "valueChanged(
> int )" signal to a method that "sleeps" for some time: for one click on the
> increase and decrease arrows of the spinbox, the method is called two times
> instead of one, and the spinbox number is incremented or decremented by two
> units instead of one. Strangely, when using keyboard arrows, the problem
> disappears.
> When using a for loop (see commented code) instead of a sleep(), the problem
> disappears too.
>
> I have obtained this with Qt C++, PyQt, and PySide on Linux.
> I have not been able to reproduce it with PyQt on Windows (I have not tested
> the C++ and PySide version on Windows).
> So this problem seems to appear only on Linux.
> My Qt version: 4.6.2.
>
> Can you reproduce this behavior on Linux?
> Have I to enter a bug report?
>
> Additional question: what is the origin of the sleep() function used below?
> I am not familiar to C++. Is it the POSIX thread library (available only on
> Linux: "man pthreads")?
It seems that some of the included headers pull this function, I would
say, yes this is the POSIX sleep.
And using sleep() in a slot sounds strange to me. sleep() should
always be avoided as QT is an event-driven framework.
If you really want to use sleep-like things (I guess for testing
purpose), you could use this (Try searching the web for "QT sleep"):
#include <qwaitcondition.h>
...
QWaitCondition sleep;
sleep.wait(2000); // 2s
...
Or simply keep with the busy loop, since anyway you're trying to
simulate calculation time...
My 2 cents,
Chris
>
> Julien
>
> //// spinbox.hpp
> #include<QtGui>
> #include<QtCore>
>
> class MyObject: public QObject
> {
> Q_OBJECT
>
> public slots:
> void wait_a_moment( void );
> };
>
> //// spinbox.cpp
> #include "spinbox.hpp"
>
> void MyObject::wait_a_moment( void )
> {
> qDebug()<< "calculation begin";
> sleep( 1 );
> // for ( int i=0; i<100000000; i++ )
> // {
> // if ( i%10000000 == 0 )
> // qDebug()<< i;
> // }
> qDebug()<< "calculation end";
> }
>
> int main( int argc, char * argv[] )
> {
>
> QApplication app( argc, argv );
>
> MyObject * myobject_instance = new MyObject();
>
> QDialog * dialog = new QDialog();
> dialog->resize( 750, 550 );
>
> QSpinBox * spinbox = new QSpinBox( dialog );
> QObject::connect( spinbox
> , SIGNAL( valueChanged( int ) )
> , myobject_instance
> , SLOT( wait_a_moment() ) );
>
> QVBoxLayout * vboxlayout = new QVBoxLayout( dialog );
> dialog->setLayout( vboxlayout );
> dialog->show();
>
> app.exec();
> }
>
> //// spinbox.pro
> HEADERS += spinbox.hpp
> SOURCES += spinbox.cpp
>
>
> //// Compilation
> $ qmake
> $ make
>
>
--
Christian Gagneraud,
Electronics and software engineer
TechWorks Marine Ltd
4a, Park Lane
Dun Laoghaire, Co Dublin
Ireland
Tel: + 353 1 2365990
Fax: + 353 1 2365992
Web: www.techworks.ie
More information about the Qt-interest-old
mailing list