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. [SOLVED] How to do a custom range for a Spinbox
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] How to do a custom range for a Spinbox

Scheduled Pinned Locked Moved General and Desktop
20 Posts 2 Posters 6.1k Views 1 Watching
  • 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.
  • A Offline
    A Offline
    astodolski
    wrote on last edited by
    #11

    [quote author="Adrien Leravat" date="1377179330"]it must be initialized to something like -INF in the cosntructor[/quote]

    I saw the need to do that after my question. However I get a nasty crash.

    "HEAP CORRUPTION DETECTED"

    This only occured in Qt Creator. VS2010 didn't dsiplay the error - Hmmm

    1 Reply Last reply
    0
    • A Offline
      A Offline
      Adrien Leravat
      wrote on last edited by
      #12

      When defining it to -INF ? Well any other mecanism to go through it the first time will do else, like a flag

      Adeneo Embedded - www.adeneo-embedded.com

      1 Reply Last reply
      0
      • A Offline
        A Offline
        astodolski
        wrote on last edited by
        #13

        [quote author="Adrien Leravat" date="1377183155"]When defining it to -INF ? Well any other mecanism to go through it the first time will do else, like a flag[/quote]

        No actually I set it to INT_MIN in the xtor. When I took out the code to do the validate and the actual validator object, there were no issues.

        It seems though that by doing an additional signal that my calling method defines a signal/slot mechanism for the slot to be called when the value is changed. I think what is happening is that the slot in the caller gets called twice.

        For example:

        The caller has in its xtor:

        @
        connect(ui->sbScaleImage, SIGNAL(valueChanged(int)), this, SLOT(zoomImage(int)));
        @

        The slot zoomImage gets called when the up/down arrows are clicked in the UI. The slot gets called now 2x

        1 Reply Last reply
        0
        • A Offline
          A Offline
          Adrien Leravat
          wrote on last edited by
          #14

          Yes you have to connect to the new signal (if it wasn't a typo). The original one will be emitted twice because the internal value changes twice. But the signal scaleValueChanged (note that this is a different signal) should be emitted only once if I made no mistake.

          Edit: In my code, the m_nPreviousValue variable should be updated BEFORE calling setValue, as the emit will be synchronous. My bad, wrote it too quickly.

          @
          if (val != m_nPreviousValue)
          {
          if(val == 0)
          {
          val = (m_nPreviousValue == 1) ? -1 : 1;
          m_nPreviousValue = val;
          setValue(val);
          }
          else
          {
          m_nPreviousValue = val;
          }

              emit scaleValueChanged(val);
          }
          

          @

          Adeneo Embedded - www.adeneo-embedded.com

          1 Reply Last reply
          0
          • A Offline
            A Offline
            astodolski
            wrote on last edited by
            #15

            It appears that the slot zoomImage(int ctlValue) gets called twice when passing through zero be it up or down. One call of the slot every other time. :( To be clear, this is the slot which is part of the UI. I don't want to confuse things. I DID make the corrections to the code you posted.

            EDIT: Changing to the new signal scaleValueChanged(int) doesn't prevent the spinner from selecting zero.

            1 Reply Last reply
            0
            • A Offline
              A Offline
              Adrien Leravat
              wrote on last edited by
              #16

              Any progress with your issue ?

              A step by step debugging in the slot would clearly highly any issue in the logic. Maybe I missed something in your explanation.

              Adeneo Embedded - www.adeneo-embedded.com

              1 Reply Last reply
              0
              • A Offline
                A Offline
                astodolski
                wrote on last edited by
                #17

                Hi,

                Well the slot in the UI works. That is, the slot that gets connected to the spinbox valueChanged signal in the UI class. However, the issue is that it gets called 2x depending on the signal that is used in the ScaleSpinBox class. If I
                use this constructor
                @
                ScaleSpinBox::ScaleSpinBox(QWidget *parent) : QSpinBox(parent)
                {
                connect(this, SIGNAL(scaleValueChanged(int)), SLOT(onValueChanged(int)));
                m_nPreviousValue = INT_MIN;

                }
                @

                Then the slot associated with the UI gets called ONCE. However the control is NOT prevented from going from 1 to zero. In fact
                @ScaleSpinBox::onValueChanged(int val)@ never gets called.

                If I were to change to this constructor:

                @
                ScaleSpinBox::ScaleSpinBox(QWidget *parent) : QSpinBox(parent)
                {
                connect(this, SIGNAL(valueChanged(int)), SLOT(onValueChanged(int)));
                m_nPreviousValue = INT_MIN;
                }
                @

                the slot in the UI gets called 2x.

                1 Reply Last reply
                0
                • A Offline
                  A Offline
                  Adrien Leravat
                  wrote on last edited by
                  #18

                  Ok so the problem to me is that your UI is connected to the "valueChanged" signal. Actually it should be connected only to "scaleValueChanged". The "valueChanged" is used only by your ScaneSpinBox class, but should not be used elsewhere.

                  It bowls down to:

                  • Connecting the "valueChanged" signal to the "onValueChanged" slot of the ScaleSpinBox class
                  • Connecting your UI to the "scaleValueChanged" signal, the one emitted by the ScaleSpinBox class, and only this one

                  Adeneo Embedded - www.adeneo-embedded.com

                  1 Reply Last reply
                  0
                  • A Offline
                    A Offline
                    astodolski
                    wrote on last edited by
                    #19

                    That solved it, thanks much

                    1 Reply Last reply
                    0
                    • A Offline
                      A Offline
                      Adrien Leravat
                      wrote on last edited by
                      #20

                      Glad to here ! :)

                      Don't forget the "solved" tag in addition the title ;)

                      Adeneo Embedded - www.adeneo-embedded.com

                      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