Results 1 to 11 of 11

Thread: QSpinBox

  1. #1
    Join Date
    Jan 2006
    Location
    Ohio
    Posts
    332
    Thanks
    37
    Thanked 8 Times in 4 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default QSpinBox

    I have some QSpinBoxes in which I always want to display 2 digits, even if the value is a single digit value. In other words, for values like 1 and 5 I would like to display 01 and 05. I have looked into valueFromText and textFromValue but don't think I need these since I am still just displaying integers and not characters besides 0-9. I also looked at validate, but I can't think of why I would need validate because the QSpinBox already limits the user to entering digits. I just want to prefix the single digit values with a 0. I thought about connecting the valueChanged signal to a slot to add a prefix to the number if it were less than 10, but would prefer to not handle this by connecting to the signal in case I need to capture this signal at a later time and do more complex things with it. Thanks for any ideas you may have!

  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

    Quote Originally Posted by ToddAtWSU View Post
    I have looked into valueFromText and textFromValue but don't think I need these since I am still just displaying integers and not characters besides 0-9.
    Yes, this is the way to go.
    J-P Nurmi

  3. #3
    Join Date
    Jan 2006
    Location
    Ohio
    Posts
    332
    Thanks
    37
    Thanked 8 Times in 4 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Smile Re: QSpinBox

    How exactly do I use textFromValue and valueFromText? I was hoping to avoid them because the docs don't really explain this very well. What exactly am I supposed to do with these functions and how I do get the QSpinBox to display my 2 digit numbers? Are there any examples of this? Thanks again!

  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: QSpinBox

    In a locale unaware way it's as simple as this:
    Qt Code:
    1. class SpinBox : public QSpinBox
    2. {
    3. protected:
    4. virtual QString textFromValue(int value) const
    5. {
    6. return QString("%1").arg(value, 2, 10, QLatin1Char('0'));
    7. }
    8. };
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  5. #5
    Join Date
    Apr 2011
    Posts
    10
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QSpinBox

    Hello, Sorry for reviving an old post.
    Did this solution work for you?, I need to implement two or more digits on a Spinbox, but I don't know how, I used Designer so I don't have the Spinbox class, is there any other way?
    Thanks in advance

  6. #6
    Join Date
    Jul 2008
    Location
    Germany
    Posts
    507
    Thanks
    11
    Thanked 76 Times in 74 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QSpinBox

    Hi, in the Qt Designer you can use "Promote to" to create a SpinBox instead of QSpinBox. Just create a QSpinBox, right-click and select "Promote to", then in the dialog enter the details of your class, in this case SpinBox.

    Ginsengelf

  7. #7
    Join Date
    Apr 2011
    Posts
    10
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QSpinBox

    Thanks for the reply, I Have been trying for a couple of days, with no succest I created a QWidget with a QSpinbox and a label, then promote the QSpinbox to spinbox.h, this is my spinbox.h file
    #ifndef SPINBOX_H
    #define SPINBOX_H

    #include <QSpinBox>
    #include <QString>

    class SpinBox : public QSpinBox
    {
    protected:
    virtual QString textFromValue(int value) const
    {
    return QString("%1").arg(value, 2, 10, QLatin1Char('0'));
    }
    };

    #endif // SPINBOX_H
    this code produce a error
    ui_myqwidget.h:35: error: no matching function for call to 'SpinBox::SpinBox(QWidget*&)'
    Wich is generated by designer, I'm I doing something wrong?

  8. #8
    Join Date
    Jul 2008
    Location
    Germany
    Posts
    507
    Thanks
    11
    Thanked 76 Times in 74 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QSpinBox

    Hi, your promotion dialog should look like this:
    spinbox.png
    Then click "Add" and "Promote", and Designer should use your SpinBox instead of the QSpinBox. Make sure the path for the header file is correct.

    Ginsengelf

  9. #9
    Join Date
    Apr 2011
    Posts
    10
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QSpinBox

    I don't think the problem is on Designer, I'm runing QT on Linux (that why it looks a little bit different)
    instantánea2.jpeg
    and this are the issues that the compiler give me
    issues.jpg

    my Qwidget is named pcargar.ui
    as you can see the error point to autogenerated ui_pcargar.h line 35
    Qt Code:
    1. /********************************************************************************
    2. ** Form generated from reading UI file 'pcargar.ui'
    3. **
    4. ** Created: Thu Aug 30 17:42:07 2012
    5. ** by: Qt User Interface Compiler version 4.7.4
    6. **
    7. ** WARNING! All changes made in this file will be lost when recompiling UI file!
    8. ********************************************************************************/
    9.  
    10. #ifndef UI_PCARGAR_H
    11. #define UI_PCARGAR_H
    12.  
    13. #include <QtCore/QVariant>
    14. #include <QtGui/QAction>
    15. #include <QtGui/QApplication>
    16. #include <QtGui/QButtonGroup>
    17. #include <QtGui/QHeaderView>
    18. #include <QtGui/QLabel>
    19. #include <QtGui/QWidget>
    20. #include "spinbox.h"
    21.  
    22. QT_BEGIN_NAMESPACE
    23.  
    24. class Ui_pcargar
    25. {
    26. public:
    27. SpinBox *spinBox;
    28. QLabel *label;
    29.  
    30. void setupUi(QWidget *pcargar)
    31. {
    32. if (pcargar->objectName().isEmpty())
    33. pcargar->setObjectName(QString::fromUtf8("pcargar"));
    34. pcargar->resize(400, 300);
    35. spinBox = new SpinBox(pcargar);
    36. spinBox->setObjectName(QString::fromUtf8("spinBox"));
    37. spinBox->setGeometry(QRect(190, 50, 56, 22));
    38. label = new QLabel(pcargar);
    39. label->setObjectName(QString::fromUtf8("label"));
    40. label->setGeometry(QRect(260, 60, 57, 14));
    41.  
    42. retranslateUi(pcargar);
    43.  
    44. QMetaObject::connectSlotsByName(pcargar);
    45. } // setupUi
    46.  
    47. void retranslateUi(QWidget *pcargar)
    48. {
    49. pcargar->setWindowTitle(QApplication::translate("pcargar", "Form", 0, QApplication::UnicodeUTF8));
    50. label->setText(QApplication::translate("pcargar", "TextLabel", 0, QApplication::UnicodeUTF8));
    51. } // retranslateUi
    52.  
    53. };
    54.  
    55. namespace Ui {
    56. class pcargar: public Ui_pcargar {};
    57. } // namespace Ui
    58.  
    59. QT_END_NAMESPACE
    60.  
    61. #endif // UI_PCARGAR_H
    To copy to clipboard, switch view to plain text mode 
    Any ideas.

  10. #10
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QSpinBox

    Promoting the widget in Designer has nothing to do with this error. Your definition of your SpinBox class is incorrect. Read what the compiler is telling you: It is looking for a constructor SpinBox( QWidget *& ). First, you need to define (and implement it). Second, QSpinBox is a QObject-based class, so any class derived from it must also include the Q_OBJECT macro as part of the class definition so the Qt meta-object and signal/sot mechanism work correctly for your class.

    Qt Code:
    1. class SpinBox : public QSpinBox
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. SpinBox( QWidget * parent ) : QSpinBox( parent ) {}
    7.  
    8. protected:
    9. virtual QString textFromValue(int value) const
    10. {
    11. return QString("%1").arg(value, 2, 10, QLatin1Char('0'));
    12. }
    13. };
    To copy to clipboard, switch view to plain text mode 
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  11. #11
    Join Date
    Apr 2011
    Posts
    10
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QSpinBox

    Sorry for the newbie mistake that solve the problem, Thank you very much.

Similar Threads

  1. QSpinBox or QLabels
    By ToddAtWSU in forum Qt Programming
    Replies: 10
    Last Post: 27th November 2007, 20:10
  2. QSpinBox and setStylesheet problem
    By Ace-X in forum Qt Programming
    Replies: 2
    Last Post: 19th January 2007, 11:21
  3. QSpinBox with checkbox
    By :db:sStrong in forum Qt Programming
    Replies: 4
    Last Post: 17th January 2007, 13:22
  4. float value to Qspinbox
    By nErnie in forum Qt Programming
    Replies: 6
    Last Post: 17th October 2006, 08:16
  5. Problem reimplementing QSpinBox in a custom widget
    By hvengel in forum Qt Programming
    Replies: 1
    Last Post: 30th March 2006, 08:12

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.