Spin box of strings



  • Hi, i have to create a spinbox with strings instead of numbers.
    Is there any simple way to set the spin box to use my QVector with strings?
    If someone try to type any character which is not included in the vector, it should be rejected, if you increase or decrease, it should go by the index value, for example

    a
    b
    c
    d

    So if i hit increase or upper arrow, the next after a should be b,etc.


  • Lifetime Qt Champion

    Hi
    why not use a QCombobox and 2 buttons to raise/decrease the active index ?



  • @Loc888 you may want to subclass QSpinBox and use a list of accepted values in combination with the textFromValue() method. Pseudo-code follows:

    textspinbox.h

    class TextSpinBox : public QSpinBox
    {
    public:
        explicit TextSpinBox(QWidget *parent = 0);
    
    private:
        QString textFromValue(int value) const;
        QList<QString> acceptedValues;
    };
    

    textspinbox.cpp

    #include "textspinbox.h"
    
    TextSpinBox::TextSpinBox(QWidget *parent) :
        QSpinBox(parent)
    {
        acceptedValues << "First" << "Second" << "Third" << "Fourth";
        setRange(1, acceptedValues.size());
    }
    
    QString TextSpinBox::textFromValue(int value) const
    {
        return acceptedValues.value(value - 1);
    }
    


  • @mrjj It's just because of esthetics, i tought the same thing, but spinbox looks better in this case.



  • @Pablo-J.-Rogina

    It helped, thank you for help much.
    There was only one problem, if i set 0 - 20 range numbers, and add let's say few symbols before 0, a,b,c

    If i type 13 in the spinBox, instead of 13 i get 10, because the symbols took 3 indexes, i fixed this issue by moving all the symbols after the numbers.



  • @Loc888 ok, great you can achieve your goal. Could this post be marked as solved then? Thanks


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.