Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
qtws26_logo

The Qt Event of the Year: Qt World Summit 2026

Save Your Spot >
Brand Logo
  1. Home
  2. General talk
  3. Brainstorm
  4. How do I customize a QSlider that would work with timestamps?
Qt 6.12 LTS is out! See what's new in the release blog

How do I customize a QSlider that would work with timestamps?

Scheduled Pinned Locked Moved Solved Brainstorm
4 Posts 3 Posters 1.8k 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.
  • D Offline
    D Offline
    dracodraco
    wrote on last edited by
    #1

    I am new working with QT. I am familiar with the QSlider class of Qt. But I am not sure whether it works with Timestamps. I have a QSlider object that can return an integer based on the position of its tick. However I want to customize it so that it can return timestamps (Ex. 10:50:20) instead. Could you tell me whether its possible ? or how can I implement it?

    My goal is to create a slider that has values between 0:00:00 to 23:59:59. And based on the position of the tick it can return a value between 0:00:00 and 23:59:59

    J.HilkJ 1 Reply Last reply
    0
    • D dracodraco

      I am new working with QT. I am familiar with the QSlider class of Qt. But I am not sure whether it works with Timestamps. I have a QSlider object that can return an integer based on the position of its tick. However I want to customize it so that it can return timestamps (Ex. 10:50:20) instead. Could you tell me whether its possible ? or how can I implement it?

      My goal is to create a slider that has values between 0:00:00 to 23:59:59. And based on the position of the tick it can return a value between 0:00:00 and 23:59:59

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

      @dracodraco
      you can simply map the range of the slider to the seconds in a day and calculate that way the selected time:

      #include <QApplication>
      
      #include <QSlider>
      #include <QLabel>
      #include <QTime>
      #include <QVBoxLayout>
      
      int main(int argc, char *argv[])
      {
          QApplication a(argc, argv);
          QWidget w;
          w.resize(500, 200);
          w.show();
      
          auto vLayout = new QVBoxLayout(&w);
          auto slider = new QSlider(&w);
          slider->setOrientation(Qt::Horizontal);
          slider->setMinimum(0);
          slider->setMaximum(10000);
      
          auto timeLabel = new QLabel(&w);
          timeLabel->setText("00:00:00");
          vLayout->addWidget(slider,1);
          vLayout->addWidget(timeLabel,1);
      
          auto mapToRange = [](int minOutRange, int maxOutRange, int minInRange, int maxInRange, int value) ->double {
              return (value-minInRange)/static_cast<double>(maxInRange-minInRange)*(maxOutRange-minOutRange)+minOutRange;
          };
      
          QObject::connect(slider, &QSlider::valueChanged, timeLabel, [timeLabel, slider, mapToRange](int value) ->void {
              const QTime t = QTime::fromMSecsSinceStartOfDay(mapToRange(0,86399999, slider->minimum(), slider->maximum(), value));
              timeLabel->setText(t.toString("hh:mm:ss"));
          });
      
          return a.exec();
      }
      

      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.

      1 Reply Last reply
      5
      • D Offline
        D Offline
        dracodraco
        wrote on last edited by
        #3

        thank you sir, the code is simple and easy to understand <3

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

          thank you sir, the code is simple and easy to understand <3

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

          @dracodraco if your issue is solved, please don't forget to mark your post as such!

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