Results 1 to 5 of 5

Thread: QSpinBox : display values from a QVector

  1. #1
    Join Date
    Dec 2013
    Posts
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QSpinBox : display values from a QVector

    Hi,

    I'd like to have a QSpinBox to display value from a QVector only. At first I thought that a new slot and signal in my widget would do the trick, using valueChanged(int) signal and setValue(int) slot from QSpinBox, but I enter an infinite loop since setValue emit valueChanged...
    I heard that subclassing QAbstractSpinBox might work, but I really don't know how, nor which signals/slots to reimplement.
    Any idea ? Maybe a better solution than subclassing ?

    Thanks !!

  2. #2
    Join Date
    Jun 2006
    Posts
    74
    Thanks
    6
    Thanked 4 Times in 3 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QSpinBox : display values from a QVector

    If it's only for the emit, have a look at QObject::blockSignals().

  3. #3
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: QSpinBox : display values from a QVector

    Here is an example assuming the QVector is non-empty.
    Qt Code:
    1. class SpinBox : public QSpinBox
    2. {
    3. Q_OBJECT
    4. public:
    5. explicit SpinBox(const QVector<int> & values, QWidget * parent = 0)
    6. : QSpinBox(parent)
    7. , mValues(values)
    8. , mIndex(0)
    9. {
    10. qSort(mValues);
    11. setMinimum(mValues.at(0));
    12. setMaximum(mValues.at(mValues.size() - 1));
    13. setValue(mValues.at(0));
    14. }
    15.  
    16. protected:
    17. void stepBy(int steps) // re-implementaion
    18. {
    19. mIndex += steps;
    20. mIndex = qBound(0, mIndex, mValues.size() - 1);
    21. setValue(mValues.at(mIndex));
    22. }
    23.  
    24. private:
    25. QVector<int> mValues;
    26. int mIndex;
    27. };
    28.  
    29. int main(int argc, char ** argv)
    30. {
    31. QApplication app(argc, argv);
    32.  
    33. QVector<int> values = QVector<int>() << 100 << 20 << 25 << 36 << 42 << 56 << 98 << 105;
    34.  
    35. SpinBox spinbox(values);
    36. spinbox.show();
    37.  
    38. return app.exec();
    39. }
    To copy to clipboard, switch view to plain text mode 
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  4. #4
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: QSpinBox : display values from a QVector

    Did you explore using a QComboBox instead?
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  5. #5
    Join Date
    Dec 2013
    Posts
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSpinBox : display values from a QVector

    I use the same QSpinBox in two different contexts, one where it should work as usual, and another where I need to display only particular values. Thank you Santosh !

Similar Threads

  1. Replies: 11
    Last Post: 28th June 2012, 12:17
  2. sort a Qvector based on another Qvector
    By OzQTNoob in forum Qt Programming
    Replies: 2
    Last Post: 16th February 2012, 07:39
  3. Replies: 5
    Last Post: 3rd September 2011, 00:11
  4. Replies: 7
    Last Post: 4th November 2010, 00:18
  5. Replies: 3
    Last Post: 20th April 2006, 12:21

Tags for this Thread

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.