Give a set of values to a QSlider



  • 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


  • Lifetime Qt Champion

    Hi,

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



  • @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 ?


  • Lifetime Qt Champion

    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.



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



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



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



  • 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();
    }
    


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



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



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


Log in to reply
 

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