Results 1 to 11 of 11

Thread: QSpinBox: how to disable line editing?

  1. #1
    Join Date
    Apr 2007
    Location
    Rakovnik, Czech Republic
    Posts
    175
    Thanks
    43
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default QSpinBox: how to disable line editing?

    I've looked thru all the members that take a "bool" parameter, yet I cannot find a way to disable line editing on QSpinBox. Seems like an obvious feature to have, I must be overlooking something.

    Basically I want my spinBox to work with the arrows only; user shouldn't be able to type his own text in the 'lineEdit' part.

  2. #2
    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: QSpinBox: how to disable line editing?

    Hint: QSpinBox inherits QAbstractSpinBox.
    J-P Nurmi

  3. #3
    Join Date
    Apr 2007
    Location
    Rakovnik, Czech Republic
    Posts
    175
    Thanks
    43
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QSpinBox: how to disable line editing?

    I'd like to buy a vowel: A

    Seriously: setReadOnly(true) does as expected--and inherets QAbstractSpinBox--but it disables the arrows, too. Is this what you were thinking?

  4. #4
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: QSpinBox: how to disable line editing?

    No, is to exp nsive. I give you the other ch r cters nd nother hint: QLineEdit inside the Q bstr ctSpinBox.

  5. #5
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QSpinBox: how to disable line editing?

    Quote Originally Posted by Lykurg View Post
    No, is to exp nsive. I give you the other ch r cters nd nother hint: QLineEdit inside the Q bstr ctSpinBox.
    wow, I don't understand nothing
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  6. #6
    Join Date
    Apr 2007
    Location
    Rakovnik, Czech Republic
    Posts
    175
    Thanks
    43
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QSpinBox: how to disable line editing?

    I still don't get it. Presumably something like:

    Qt Code:
    1. QSpinBox *mbox = new QSpinBox;
    2. mbox->someMember(QLineEdit::readOnly);
    To copy to clipboard, switch view to plain text mode 

    but I don't know. Or maybe something thru QAbstractSpinBox::setLineEdit. Obviously I'm unfamiliar with the way classes and the members they inherit relate to one another.

    Of course I see LineEdit exists in AbstractSpinBox; but how to get at it? And to double-check: not suggesting I use QAbstractSpinBox instead of QSpinBox, right?

  7. #7
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QSpinBox: how to disable line editing?

    you can do next this (it's a little bit tricky but works)
    Qt Code:
    1. ...
    2. spinBox->installEventFilter(this);
    3. ...
    4. bool Dialog::eventFilter(QObject *o, QEvent *e)
    5. {
    6. if (o == spinBox && e->type() == QEvent::KeyPress) {
    7. e->ignore();
    8. return true;
    9. }
    10. return QDialog::eventFilter(o, e);
    11. }
    To copy to clipboard, switch view to plain text mode 
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  8. #8
    Join Date
    Apr 2007
    Location
    Rakovnik, Czech Republic
    Posts
    175
    Thanks
    43
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QSpinBox: how to disable line editing?

    thanks spirit; yeah, this works. If there's no better solution, I'll just employ this.

    I'm surprised QSpinBox doesn't do this off the shelf... but I've looked around and few--perhaps none--have asked about it.

  9. #9
    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: QSpinBox: how to disable line editing?

    Ok, QAbstractSpinBox::lineEdit() is protected, but you can work it around like this:
    Qt Code:
    1. spinBox->findChild<QLineEdit*>()->setReadOnly(true);
    To copy to clipboard, switch view to plain text mode 
    I just don't get why do you want to rip off an important functionality. It certainly decreases the usability of that input widget. At least make sure that shortcuts like up/down, pgup/pgdown etc. still work or your users will truly hate you. :P
    J-P Nurmi

  10. The following 2 users say thank you to jpn for this useful post:

    Mesozoic (29th October 2013), vonCZ (23rd April 2009)

  11. #10
    Join Date
    Apr 2007
    Location
    Rakovnik, Czech Republic
    Posts
    175
    Thanks
    43
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QSpinBox: how to disable line editing?

    Quote Originally Posted by jpn View Post
    I just don't get why do you want to rip off an important functionality.
    This is related to my other question about the 50 x 4,000 widget. This spinBox will have values 400 1200 2000, for example. The value selected in the spinBox will determine how much of that huge QWidget will be visible. In a sense: it allows the user to zoom into/out of the 50x4000 widget. After I get it all built and tested: perhaps I'll go back to not restricting the text.

    Thanks for your help, btw.

  12. #11
    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: QSpinBox: how to disable line editing?

    If you want to restrict spinbox text, the correct way is not to disable text input but to reimplement QAbstractSpinBox::validate() and/or QAbstractSpinBox::fixup() appropriately.
    J-P Nurmi

Similar Threads

  1. Why is my application's CPU usage always > 50%
    By richardander in forum Qt Programming
    Replies: 10
    Last Post: 14th October 2010, 22:22
  2. QTcpSocket exception.
    By Fastman in forum Qt Programming
    Replies: 9
    Last Post: 29th January 2008, 13:51
  3. Qwizard crashed when created in a slot
    By joshlareau in forum Qt Programming
    Replies: 9
    Last Post: 15th January 2008, 09:16
  4. KDE/QWT doubt on debian sarge
    By hildebrand in forum KDE Forum
    Replies: 13
    Last Post: 25th April 2007, 06:13
  5. QTableView paints too much
    By Jimmy2775 in forum Qt Programming
    Replies: 2
    Last Post: 26th July 2006, 18:42

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.