How to add a tick mark to a slider if it cannot inherit QSlider

后端 未结 3 1743
忘掉有多难
忘掉有多难 2021-01-07 03:30

I have a Qt dialog and there is a slider in it, when the dialog is initialized the slider will be set a value. In order to remind the user what is the default value, I want

3条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-07 03:38

    Easiest way I can think off is:

    Add QSlider to QSlider (like you do it with layouts and QFrames). Slider above will be your current slider (clickable one). Slider below will be your "default tick position" value.

    #include 
    #include 
    #include 
    
    int main(int argc, char * argv[])
    {
        QApplication app(argc, argv);
    
        QSlider * defaultValueSlider = new QSlider();
        QSlider * valueSlider = new QSlider(defaultValueSlider);
        QVBoxLayout * lay = new QVBoxLayout(defaultValueSlider);
        lay->setContentsMargins(0, 0, 0, 0);
        lay->setSpacing(0);
        lay->addWidget(valueSlider);
        defaultValueSlider->setRange(0, 100);
        valueSlider->setRange(0, 100);
        defaultValueSlider->setValue(30);
    
        defaultValueSlider->show();
    
        return app.exec();
    }
    

    result

提交回复
热议问题