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. Give a set of values to a QSlider
Forum Updated to NodeBB v4.3 + New Features

Give a set of values to a QSlider

Scheduled Pinned Locked Moved Solved General and Desktop
11 Posts 3 Posters 4.9k Views 2 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.
  • D Offline
    D Offline
    DavidM29
    wrote on last edited by DavidM29
    #1

    Hello,

    I would like to have a QSlider with a set of value for example (15,18,21,24,28). The step is not always the same and that is why I have an issue with it. Is there a way that I can apply a set of value or I should change the step dynamically one I've reach specific values ?

    Thanks

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      If you want a slider with non-linear values, you'll likely have to go through making your own version starting with QAbstractSlider.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      D 1 Reply Last reply
      1
      • SGaistS SGaist

        Hi,

        If you want a slider with non-linear values, you'll likely have to go through making your own version starting with QAbstractSlider.

        D Offline
        D Offline
        DavidM29
        wrote on last edited by
        #3

        @SGaist
        I've never made my own Widget can you please guide me a bit with this ? I'm new to Qt and C++ I know more about Java and Javascript.

        Do you have a simple example ?

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          The documentation is full of examples.

          For custom painting, you can take a look at the Analog Clock Example

          For the use of sliders, the Sliders Example.

          To get something similar to QSlider, then take a look at the sources of QSlider.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          2
          • D Offline
            D Offline
            DavidM29
            wrote on last edited by DavidM29
            #5

            Thank you for all this documentation, even if there is none of them simple... I don't think I be able to do that kind of thing for now. I'm going to try to change the step once I reach a defined value instead.

            1 Reply Last reply
            0
            • Pablo J. RoginaP Offline
              Pablo J. RoginaP Offline
              Pablo J. Rogina
              wrote on last edited by
              #6

              @DavidM29 if you don't feel "brave enough" to create a custom logarithmic slider, I guess you may achieve your goal using a slider + a label next to it, and an QVector<QString> with the actual values you need (in the example 15,18,21,24,28)
              So the slider will be equally ticked and when the QSlider::valueChanged() signal is emitted, I'd use the value of the slide (0, 1, 2, etc.) as index to the QVector to get the actual value of interest and set that value as the text of the label next to the slide.

              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

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

                @DavidM29 if you don't feel "brave enough" to create a custom logarithmic slider, I guess you may achieve your goal using a slider + a label next to it, and an QVector<QString> with the actual values you need (in the example 15,18,21,24,28)
                So the slider will be equally ticked and when the QSlider::valueChanged() signal is emitted, I'd use the value of the slide (0, 1, 2, etc.) as index to the QVector to get the actual value of interest and set that value as the text of the label next to the slide.

                D Offline
                D Offline
                DavidM29
                wrote on last edited by
                #7

                @Pablo-J.-Rogina
                Thank you for that reply, I justed made something close to what you explained. It works but I don't really like the way it's made, I know that in an other language I could have achieve a better solution. But being new in C++, I'm not able to create my entire custom widget. Need to find an easy/simple example first. If you have one for me I would be glad to see it.

                1 Reply Last reply
                0
                • Pablo J. RoginaP Offline
                  Pablo J. RoginaP Offline
                  Pablo J. Rogina
                  wrote on last edited by
                  #8

                  The example is a MainWindow with a QSqlider and a QLabel.

                  mainwindow.h

                  #ifndef MAINWINDOW_H
                  #define MAINWINDOW_H
                  
                  #include <QMainWindow>
                  #include <QVector>
                  
                  namespace Ui {
                  class MainWindow;
                  }
                  
                  class MainWindow : public QMainWindow
                  {
                      Q_OBJECT
                  
                  public:
                      explicit MainWindow(QWidget *parent = 0);
                      ~MainWindow();
                  
                  public slots:
                      void on_sldTest_valueChanged(int idx);
                  
                  private:
                      Ui::MainWindow *ui;
                      QVector<QString> values;
                  };
                  
                  #endif // MAINWINDOW_H
                  

                  mainwindow.cpp

                  #include "mainwindow.h"
                  #include "ui_mainwindow.h"
                  
                  MainWindow::MainWindow(QWidget *parent) :
                      QMainWindow(parent),
                      ui(new Ui::MainWindow)
                  {
                      values.append("15");
                      values.append("18");
                      values.append("21");
                      values.append("24");
                      values.append("28");
                  
                      ui->setupUi(this);
                  
                      ui->lblIdx->setText(values[0]);
                  }
                  
                  void MainWindow::on_sldTest_valueChanged(int idx) {
                      ui->lblIdx->setText(values[idx]);
                  }
                  
                  MainWindow::~MainWindow()
                  {
                      delete ui;
                  }
                  

                  mainwindow.ui

                  <?xml version="1.0" encoding="UTF-8"?>
                  <ui version="4.0">
                   <class>MainWindow</class>
                   <widget class="QMainWindow" name="MainWindow">
                    <property name="geometry">
                     <rect>
                      <x>0</x>
                      <y>0</y>
                      <width>400</width>
                      <height>300</height>
                     </rect>
                    </property>
                    <property name="windowTitle">
                     <string>MainWindow</string>
                    </property>
                    <widget class="QWidget" name="centralWidget">
                     <layout class="QGridLayout" name="gridLayout">
                      <item row="0" column="1">
                       <widget class="QLabel" name="lblIdx">
                        <property name="text">
                         <string>15</string>
                        </property>
                       </widget>
                      </item>
                      <item row="0" column="0">
                       <widget class="QSlider" name="sldTest">
                        <property name="minimum">
                         <number>0</number>
                        </property>
                        <property name="maximum">
                         <number>4</number>
                        </property>
                        <property name="value">
                         <number>0</number>
                        </property>
                        <property name="orientation">
                         <enum>Qt::Horizontal</enum>
                        </property>
                        <property name="tickPosition">
                         <enum>QSlider::TicksBelow</enum>
                        </property>
                       </widget>
                      </item>
                     </layout>
                    </widget>
                    <widget class="QMenuBar" name="menuBar">
                     <property name="geometry">
                      <rect>
                       <x>0</x>
                       <y>0</y>
                       <width>400</width>
                       <height>22</height>
                      </rect>
                     </property>
                    </widget>
                    <widget class="QToolBar" name="mainToolBar">
                     <attribute name="toolBarArea">
                      <enum>TopToolBarArea</enum>
                     </attribute>
                     <attribute name="toolBarBreak">
                      <bool>false</bool>
                     </attribute>
                    </widget>
                    <widget class="QStatusBar" name="statusBar"/>
                   </widget>
                   <layoutdefault spacing="6" margin="11"/>
                   <resources/>
                   <connections/>
                  </ui>
                  

                  main.cpp

                  #include "mainwindow.h"
                  #include <QApplication>
                  
                  int main(int argc, char *argv[])
                  {
                      QApplication a(argc, argv);
                      MainWindow w;
                      w.show();
                  
                      return a.exec();
                  }
                  

                  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

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

                    The example is a MainWindow with a QSqlider and a QLabel.

                    mainwindow.h

                    #ifndef MAINWINDOW_H
                    #define MAINWINDOW_H
                    
                    #include <QMainWindow>
                    #include <QVector>
                    
                    namespace Ui {
                    class MainWindow;
                    }
                    
                    class MainWindow : public QMainWindow
                    {
                        Q_OBJECT
                    
                    public:
                        explicit MainWindow(QWidget *parent = 0);
                        ~MainWindow();
                    
                    public slots:
                        void on_sldTest_valueChanged(int idx);
                    
                    private:
                        Ui::MainWindow *ui;
                        QVector<QString> values;
                    };
                    
                    #endif // MAINWINDOW_H
                    

                    mainwindow.cpp

                    #include "mainwindow.h"
                    #include "ui_mainwindow.h"
                    
                    MainWindow::MainWindow(QWidget *parent) :
                        QMainWindow(parent),
                        ui(new Ui::MainWindow)
                    {
                        values.append("15");
                        values.append("18");
                        values.append("21");
                        values.append("24");
                        values.append("28");
                    
                        ui->setupUi(this);
                    
                        ui->lblIdx->setText(values[0]);
                    }
                    
                    void MainWindow::on_sldTest_valueChanged(int idx) {
                        ui->lblIdx->setText(values[idx]);
                    }
                    
                    MainWindow::~MainWindow()
                    {
                        delete ui;
                    }
                    

                    mainwindow.ui

                    <?xml version="1.0" encoding="UTF-8"?>
                    <ui version="4.0">
                     <class>MainWindow</class>
                     <widget class="QMainWindow" name="MainWindow">
                      <property name="geometry">
                       <rect>
                        <x>0</x>
                        <y>0</y>
                        <width>400</width>
                        <height>300</height>
                       </rect>
                      </property>
                      <property name="windowTitle">
                       <string>MainWindow</string>
                      </property>
                      <widget class="QWidget" name="centralWidget">
                       <layout class="QGridLayout" name="gridLayout">
                        <item row="0" column="1">
                         <widget class="QLabel" name="lblIdx">
                          <property name="text">
                           <string>15</string>
                          </property>
                         </widget>
                        </item>
                        <item row="0" column="0">
                         <widget class="QSlider" name="sldTest">
                          <property name="minimum">
                           <number>0</number>
                          </property>
                          <property name="maximum">
                           <number>4</number>
                          </property>
                          <property name="value">
                           <number>0</number>
                          </property>
                          <property name="orientation">
                           <enum>Qt::Horizontal</enum>
                          </property>
                          <property name="tickPosition">
                           <enum>QSlider::TicksBelow</enum>
                          </property>
                         </widget>
                        </item>
                       </layout>
                      </widget>
                      <widget class="QMenuBar" name="menuBar">
                       <property name="geometry">
                        <rect>
                         <x>0</x>
                         <y>0</y>
                         <width>400</width>
                         <height>22</height>
                        </rect>
                       </property>
                      </widget>
                      <widget class="QToolBar" name="mainToolBar">
                       <attribute name="toolBarArea">
                        <enum>TopToolBarArea</enum>
                       </attribute>
                       <attribute name="toolBarBreak">
                        <bool>false</bool>
                       </attribute>
                      </widget>
                      <widget class="QStatusBar" name="statusBar"/>
                     </widget>
                     <layoutdefault spacing="6" margin="11"/>
                     <resources/>
                     <connections/>
                    </ui>
                    

                    main.cpp

                    #include "mainwindow.h"
                    #include <QApplication>
                    
                    int main(int argc, char *argv[])
                    {
                        QApplication a(argc, argv);
                        MainWindow w;
                        w.show();
                    
                        return a.exec();
                    }
                    
                    D Offline
                    D Offline
                    DavidM29
                    wrote on last edited by
                    #9

                    @Pablo-J.-Rogina
                    I meant an example of a custom widget based on an AbstractWidget but thank you anyway.

                    1 Reply Last reply
                    0
                    • Pablo J. RoginaP Offline
                      Pablo J. RoginaP Offline
                      Pablo J. Rogina
                      wrote on last edited by
                      #10

                      @DavidM29 what about this post? But don't forget to go through the references suggested by @SGaist as well.

                      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

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

                        @DavidM29 what about this post? But don't forget to go through the references suggested by @SGaist as well.

                        D Offline
                        D Offline
                        DavidM29
                        wrote on last edited by
                        #11

                        @Pablo-J.-Rogina
                        That one seems to be detailed it's good ! I'll look at it ! Thanks

                        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