Results 1 to 7 of 7

Thread: QAbstractSpinBox Subclass exhibits wierd behavour

  1. #1
    Join Date
    Jan 2006
    Location
    Kerala
    Posts
    371
    Thanks
    76
    Thanked 37 Times in 32 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default QAbstractSpinBox Subclass exhibits wierd behavour

    Hi,

    Does anyone know why this simple class exihits a wierd behavior ?

    Qt Code:
    1. class Test : public QAbstractSpinBox {
    2. Q_OBJECT
    3. public:
    4. Test( QWidget *parent =0 )
    5. :QAbstractSpinBox(parent){
    6. lineEdit()->setText( "Hello " );
    7. }
    8.  
    9. void setText( const QString &txt){
    10. lineEdit()->setText( txt );
    11. }
    12. };
    To copy to clipboard, switch view to plain text mode 
    I have subclassed a QAbstractSpinBox and in the constructor I have set the SpinBox's lineedit's text to "Hello" So In the code below I expect to see the text "Hello " inside the LineEdit but It remains blank

    Qt Code:
    1. Test t;
    2. t.show();
    To copy to clipboard, switch view to plain text mode 



    Now I tried this, note that the setText() call after the show works, but not before it ?
    Qt Code:
    1. Test t;
    2. t.setText( "Before show"); // Will not work, to test it comment the setText below show
    3. t.show();
    4. t.setText( "Hi Hi "); // Calls to setText works after show ??
    To copy to clipboard, switch view to plain text mode 

    Any clue why this happens?
    We can't solve problems by using the same kind of thinking we used when we created them

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QAbstractSpinBox Subclass exhibits wierd behavour

    Do you want to just show the text instead of numbers? You may reimplement textFromValue from QSpinBox.

  3. #3
    Join Date
    Jan 2006
    Location
    Kerala
    Posts
    371
    Thanks
    76
    Thanked 37 Times in 32 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QAbstractSpinBox Subclass exhibits wierd behavour

    I am Subclassing QAbstractSpinBox, My intension is to make a widget like QTimeEdit with a different functionality. But this does not explain why the setText() works after show() gets called ?
    We can't solve problems by using the same kind of thinking we used when we created them

  4. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QAbstractSpinBox Subclass exhibits wierd behavour

    I recall someone else getting bumped into this problem too. The problem lies within QAsbtractSpinBox::showEvent() which updates the content of the line edit like this:
    Qt Code:
    1. // from qabstractspinbox.cpp:
    2. void QAbstractSpinBox::showEvent(QShowEvent *)
    3. {
    4.  
    5. d->reset();
    6. d->updateEdit(); // <-- this is the one that causes the problem
    7. }
    To copy to clipboard, switch view to plain text mode 
    The problem is that QAbstractSpinBoxPrivate::updateEdit() uses QAbstractSpinBoxPrivate::textFromValue(). In QAbstractSpinBox subclasses like QSpinBox and QDoubleSpinBox the corresponding pimpl method is overridden and the call is redirected to the corresponding method in the public implementation class, QSpinBox::textFromValue() and so on. In my humble opinion this is a little design flaw by the Trolls..
    J-P Nurmi

  5. #5
    Join Date
    Jan 2006
    Location
    Kerala
    Posts
    371
    Thanks
    76
    Thanked 37 Times in 32 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QAbstractSpinBox Subclass exhibits wierd behavour

    Does that mean that QAbstractSpinBox is never meant to be overridden by users, since its private implementation is not documented? I am developing a widget like the QTimeEdit with one more field, like the frame, and the value of the frame can be anywhere from 0 - (frameRate -1). So is QDateTimeEdit or QTimeEdit a good candidate to override ?
    We can't solve problems by using the same kind of thinking we used when we created them

  6. #6
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QAbstractSpinBox Subclass exhibits wierd behavour

    The particular problem can be avoided by bypassing QAbstractSpinBox::showEvent():
    Qt Code:
    1. void MySpinBox::showEvent(QShowEvent* event)
    2. {
    3. // bypass QAbstractSpinBox::showEvent()
    4. QWidget::showEvent(event);
    5. }
    To copy to clipboard, switch view to plain text mode 
    Edit: I'm not 100% sure whether it will break something else, though
    J-P Nurmi

  7. The following user says thank you to jpn for this useful post:

    sunil.thaha (22nd February 2007)

  8. #7
    Join Date
    Jan 2006
    Location
    Kerala
    Posts
    371
    Thanks
    76
    Thanked 37 Times in 32 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QAbstractSpinBox Subclass exhibits wierd behavour

    Good thinking !!
    We can't solve problems by using the same kind of thinking we used when we created them

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.