Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Spin box of strings

Spin box of strings

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 3 Posters 1.3k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    Loc888
    wrote on last edited by
    #1

    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.

    Pablo J. RoginaP 1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

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

      L 1 Reply Last reply
      2
      • L Loc888

        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.

        Pablo J. RoginaP Offline
        Pablo J. RoginaP Offline
        Pablo J. Rogina
        wrote on last edited by
        #3

        @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);
        }
        

        Upvote the answer(s) that helped you solve the issue
        Use "Topic Tools" button to mark your post as Solved
        Add screenshots via postimage.org
        Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

        L 1 Reply Last reply
        4
        • mrjjM mrjj

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

          L Offline
          L Offline
          Loc888
          wrote on last edited by
          #4

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

          1 Reply Last reply
          1
          • Pablo J. RoginaP Pablo J. Rogina

            @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);
            }
            
            L Offline
            L Offline
            Loc888
            wrote on last edited by
            #5

            @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.

            Pablo J. RoginaP 1 Reply Last reply
            0
            • L Loc888

              @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.

              Pablo J. RoginaP Offline
              Pablo J. RoginaP Offline
              Pablo J. Rogina
              wrote on last edited by
              #6

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

              Upvote the answer(s) that helped you solve the issue
              Use "Topic Tools" button to mark your post as Solved
              Add screenshots via postimage.org
              Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved