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. QDial pointer start position(value - 0) at top
QtWS25 Last Chance

QDial pointer start position(value - 0) at top

Scheduled Pinned Locked Moved Solved General and Desktop
qdialqslider
12 Posts 4 Posters 2.5k 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.
  • S Offline
    S Offline
    sayan275
    wrote on last edited by
    #1

    Hello All,

    I want a qdial pointer with the start value(0 or 360) at the top. By default it appears at the bottom.
    6701ea17-6735-4368-b3eb-92b699cdba02-image.png
    So the pointer of the dial with value 0 should be at the green mark .
    Is there any flag to do so, or-else, I have do manual calculation with +/- 180 .

    Any suggestion is appreciated.

    Regards,
    Sayan

    1 Reply Last reply
    0
    • S sayan275

      @Pablo-J-Rogina yes...we need to wrap...so that the starting and endpoints are similar.

      J.HilkJ Offline
      J.HilkJ Offline
      J.Hilk
      Moderators
      wrote on last edited by J.Hilk
      #11

      @sayan275
      here ya go:

      //.h
      #ifndef JHDIAL_H
      #define JHDIAL_H
      
      #include <QDial>
      
      class JhDial : public QDial
      {
          Q_OBJECT
          Q_PROPERTY(int adjustedValue READ adjustedValue WRITE setAdjustedValue NOTIFY adjustedValueChanged)
      public:
          explicit JhDial(QWidget *parent = nullptr);
      
          inline int adjustedValue() const { return m_adjustedValue; }
      
      signals:
          void adjustedValueChanged(int adjustedValue);
      
      public slots:
          void setAdjustedValue(int adjustedValue);
      
      private:
          int mapValueToRange(int y, int y_min, int y_max, int x_min, int x_max);
          void adjustValue(int value);
          int m_adjustedValue = -1;
      };
      
      #endif // JHDIAL_H
      
      
      //.cpp
      #include "jhdial.h"
      
      JhDial::JhDial(QWidget *parent) : QDial(parent)
      {
          connect(this, &QDial::valueChanged, this, &JhDial::adjustValue);
      
          setWrapping(true);
          setRange(0,360);
          setAdjustedValue(0);
      }
      
      void JhDial::setAdjustedValue(int adjustedValue)
      {
          if (m_adjustedValue == adjustedValue)
              return;
      
          if(adjustedValue < 180)
              setValue(mapValueToRange(adjustedValue,0,179,180,360));
          else
              setValue(mapValueToRange(adjustedValue,180,360, 0, 180));
      
      }
      
      int JhDial::mapValueToRange(int y, int y_min, int y_max, int x_min, int x_max)
      {
          return qRound((y-y_min)/static_cast<double>(y_max-y_min)*(x_max-x_min)+x_min);
      }
      
      void JhDial::adjustValue(int value)
      {
          if(value < 180)
              m_adjustedValue = mapValueToRange(value,0, 179, 180, 360);
          else
              m_adjustedValue = mapValueToRange(value,180, 360, 0, 180);
      
          emit adjustedValueChanged(m_adjustedValue);
      }
      
      

      You may have to adjust the borders for the mapping, so that they don't overlap, I mad this in a rush ;)
      you had to, cleaned it up a bit.


      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

      S 1 Reply Last reply
      3
      • mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by
        #2

        Hi
        Did you try with
        alt text

        If that is not working with Dial then you have to do it manually.

        S 1 Reply Last reply
        0
        • mrjjM mrjj

          Hi
          Did you try with
          alt text

          If that is not working with Dial then you have to do it manually.

          S Offline
          S Offline
          sayan275
          wrote on last edited by
          #3
          This post is deleted!
          mrjjM 1 Reply Last reply
          0
          • S sayan275

            This post is deleted!

            mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by
            #4

            @sayan275
            Hi
            Ok you must have something that block images then.
            Anyway, it show the property
            "inverted appearance "
            that normally flips a slider to be reversed.
            But sadly it didn't work with 0-360 as the zero is still in the bottom.

            http://imagizer.imageshack.com/img922/2451/awXZvi.png

            S 1 Reply Last reply
            0
            • mrjjM mrjj

              @sayan275
              Hi
              Ok you must have something that block images then.
              Anyway, it show the property
              "inverted appearance "
              that normally flips a slider to be reversed.
              But sadly it didn't work with 0-360 as the zero is still in the bottom.

              http://imagizer.imageshack.com/img922/2451/awXZvi.png

              S Offline
              S Offline
              sayan275
              wrote on last edited by
              #5

              @mrjj sorry I got your posted image..its the property.
              dbfebf2e-5bc1-4907-89ed-a71b586dfc3d-image.png
              Yes it didn't work...I tried all possible combinations with the flags available.
              Can we rotated a widget? .. can't remember any api.
              Meanwhile I got the expected output by calculating with +/- 180

              //    if(value > 180)
              //        value -= 180;
              //    else if(value <= 180)
              //        value += 180;
              

              I didn't want to update the value in this manner.

              @mrjj Anyways thanks for your reply.

              mrjjM 1 Reply Last reply
              0
              • S sayan275

                @mrjj sorry I got your posted image..its the property.
                dbfebf2e-5bc1-4907-89ed-a71b586dfc3d-image.png
                Yes it didn't work...I tried all possible combinations with the flags available.
                Can we rotated a widget? .. can't remember any api.
                Meanwhile I got the expected output by calculating with +/- 180

                //    if(value > 180)
                //        value -= 180;
                //    else if(value <= 180)
                //        value += 180;
                

                I didn't want to update the value in this manner.

                @mrjj Anyways thanks for your reply.

                mrjjM Offline
                mrjjM Offline
                mrjj
                Lifetime Qt Champion
                wrote on last edited by
                #6

                @sayan275
                well we cant rotate a widget as such
                but we could subclass it and then flip the painter.
                If that works in terms of navigation, im not sure.

                void QDial::paintEvent(QPaintEvent *)
                {
                    QStylePainter p(this);
                    QStyleOptionSlider option;
                    initStyleOption(&option);
                    p.drawComplexControl(QStyle::CC_Dial, option);
                }
                
                

                use p and
                https://doc.qt.io/qt-5/qtwidgets-painting-transformations-example.html
                to flip it and see :)

                S 1 Reply Last reply
                1
                • mrjjM mrjj

                  @sayan275
                  well we cant rotate a widget as such
                  but we could subclass it and then flip the painter.
                  If that works in terms of navigation, im not sure.

                  void QDial::paintEvent(QPaintEvent *)
                  {
                      QStylePainter p(this);
                      QStyleOptionSlider option;
                      initStyleOption(&option);
                      p.drawComplexControl(QStyle::CC_Dial, option);
                  }
                  
                  

                  use p and
                  https://doc.qt.io/qt-5/qtwidgets-painting-transformations-example.html
                  to flip it and see :)

                  S Offline
                  S Offline
                  sayan275
                  wrote on last edited by
                  #7

                  @mrjj Thanks for the suggestion.
                  I'll try out the overriding the paintEvent()

                  Pablo J. RoginaP 1 Reply Last reply
                  0
                  • S sayan275

                    @mrjj Thanks for the suggestion.
                    I'll try out the overriding the paintEvent()

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

                    @sayan275 just in case, do you really need the dial to wrap?

                    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

                    S 1 Reply Last reply
                    0
                    • mrjjM Offline
                      mrjjM Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on last edited by mrjj
                      #9

                      Hi
                      Just as a note.

                      void MyDial::paintEvent(QPaintEvent *event)
                      {
                          QStylePainter p(this);
                          QStyleOptionSlider option;
                          initStyleOption(&option);
                          p.translate(0, height());
                          p.scale(1.0, -1.0);
                          p.drawComplexControl(QStyle::CC_Dial, option);
                      }
                      

                      Did draw as we wanted and using keys (left , right ) moves it ok
                      but click and drag with the mouse it jumps back to the old zero.

                      So sadly was not a solution as navigation did went banans.

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

                        @sayan275 just in case, do you really need the dial to wrap?

                        S Offline
                        S Offline
                        sayan275
                        wrote on last edited by
                        #10

                        @Pablo-J-Rogina yes...we need to wrap...so that the starting and endpoints are similar.

                        J.HilkJ 1 Reply Last reply
                        0
                        • S sayan275

                          @Pablo-J-Rogina yes...we need to wrap...so that the starting and endpoints are similar.

                          J.HilkJ Offline
                          J.HilkJ Offline
                          J.Hilk
                          Moderators
                          wrote on last edited by J.Hilk
                          #11

                          @sayan275
                          here ya go:

                          //.h
                          #ifndef JHDIAL_H
                          #define JHDIAL_H
                          
                          #include <QDial>
                          
                          class JhDial : public QDial
                          {
                              Q_OBJECT
                              Q_PROPERTY(int adjustedValue READ adjustedValue WRITE setAdjustedValue NOTIFY adjustedValueChanged)
                          public:
                              explicit JhDial(QWidget *parent = nullptr);
                          
                              inline int adjustedValue() const { return m_adjustedValue; }
                          
                          signals:
                              void adjustedValueChanged(int adjustedValue);
                          
                          public slots:
                              void setAdjustedValue(int adjustedValue);
                          
                          private:
                              int mapValueToRange(int y, int y_min, int y_max, int x_min, int x_max);
                              void adjustValue(int value);
                              int m_adjustedValue = -1;
                          };
                          
                          #endif // JHDIAL_H
                          
                          
                          //.cpp
                          #include "jhdial.h"
                          
                          JhDial::JhDial(QWidget *parent) : QDial(parent)
                          {
                              connect(this, &QDial::valueChanged, this, &JhDial::adjustValue);
                          
                              setWrapping(true);
                              setRange(0,360);
                              setAdjustedValue(0);
                          }
                          
                          void JhDial::setAdjustedValue(int adjustedValue)
                          {
                              if (m_adjustedValue == adjustedValue)
                                  return;
                          
                              if(adjustedValue < 180)
                                  setValue(mapValueToRange(adjustedValue,0,179,180,360));
                              else
                                  setValue(mapValueToRange(adjustedValue,180,360, 0, 180));
                          
                          }
                          
                          int JhDial::mapValueToRange(int y, int y_min, int y_max, int x_min, int x_max)
                          {
                              return qRound((y-y_min)/static_cast<double>(y_max-y_min)*(x_max-x_min)+x_min);
                          }
                          
                          void JhDial::adjustValue(int value)
                          {
                              if(value < 180)
                                  m_adjustedValue = mapValueToRange(value,0, 179, 180, 360);
                              else
                                  m_adjustedValue = mapValueToRange(value,180, 360, 0, 180);
                          
                              emit adjustedValueChanged(m_adjustedValue);
                          }
                          
                          

                          You may have to adjust the borders for the mapping, so that they don't overlap, I mad this in a rush ;)
                          you had to, cleaned it up a bit.


                          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                          Q: What's that?
                          A: It's blue light.
                          Q: What does it do?
                          A: It turns blue.

                          S 1 Reply Last reply
                          3
                          • J.HilkJ J.Hilk

                            @sayan275
                            here ya go:

                            //.h
                            #ifndef JHDIAL_H
                            #define JHDIAL_H
                            
                            #include <QDial>
                            
                            class JhDial : public QDial
                            {
                                Q_OBJECT
                                Q_PROPERTY(int adjustedValue READ adjustedValue WRITE setAdjustedValue NOTIFY adjustedValueChanged)
                            public:
                                explicit JhDial(QWidget *parent = nullptr);
                            
                                inline int adjustedValue() const { return m_adjustedValue; }
                            
                            signals:
                                void adjustedValueChanged(int adjustedValue);
                            
                            public slots:
                                void setAdjustedValue(int adjustedValue);
                            
                            private:
                                int mapValueToRange(int y, int y_min, int y_max, int x_min, int x_max);
                                void adjustValue(int value);
                                int m_adjustedValue = -1;
                            };
                            
                            #endif // JHDIAL_H
                            
                            
                            //.cpp
                            #include "jhdial.h"
                            
                            JhDial::JhDial(QWidget *parent) : QDial(parent)
                            {
                                connect(this, &QDial::valueChanged, this, &JhDial::adjustValue);
                            
                                setWrapping(true);
                                setRange(0,360);
                                setAdjustedValue(0);
                            }
                            
                            void JhDial::setAdjustedValue(int adjustedValue)
                            {
                                if (m_adjustedValue == adjustedValue)
                                    return;
                            
                                if(adjustedValue < 180)
                                    setValue(mapValueToRange(adjustedValue,0,179,180,360));
                                else
                                    setValue(mapValueToRange(adjustedValue,180,360, 0, 180));
                            
                            }
                            
                            int JhDial::mapValueToRange(int y, int y_min, int y_max, int x_min, int x_max)
                            {
                                return qRound((y-y_min)/static_cast<double>(y_max-y_min)*(x_max-x_min)+x_min);
                            }
                            
                            void JhDial::adjustValue(int value)
                            {
                                if(value < 180)
                                    m_adjustedValue = mapValueToRange(value,0, 179, 180, 360);
                                else
                                    m_adjustedValue = mapValueToRange(value,180, 360, 0, 180);
                            
                                emit adjustedValueChanged(m_adjustedValue);
                            }
                            
                            

                            You may have to adjust the borders for the mapping, so that they don't overlap, I mad this in a rush ;)
                            you had to, cleaned it up a bit.

                            S Offline
                            S Offline
                            sayan275
                            wrote on last edited by
                            #12

                            @J-Hilk Thanks ...I tried this.

                            1 Reply Last reply
                            1

                            • Login

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