解决方法
我会使用附带QLabel的QSlider,如下所示.我通常的免责声明是你可能需要自己进行一些微调和调整.
main.cpp中
#include <QMainWindow> #include <QApplication> #include <QGridLayout> #include <QSlider> #include <QLabel> class MainWindow Q_DECL_FINAL : public QMainWindow { Q_OBJECT public: explicit MainWindow(QWidget *parent = Q_NULLPTR) : QMainWindow(parent) { QSlider *slider = new QSlider(Qt::Horizontal,this); slider->setRange(1,4); slider->setSingleStep(1); QLabel *label1 = new QLabel("Novice",this); QLabel *label2 = new QLabel("Intermediate",this); QLabel *label3 = new QLabel("Advanced",this); QLabel *label4 = new QLabel("Expert",this); QGridLayout *layout = new QGridLayout; layout->addWidget(slider,1,4); layout->addWidget(label1,1); layout->addWidget(label2,1); layout->addWidget(label3,2,1); layout->addWidget(label4,3,1); setLayout(layout); } }; #include "main.moc" int main(int argc,char **argv) { QApplication application(argc,argv); MainWindow mainWindow; mainWindow.show(); return application.exec(); }
main.pro
TEMPLATE = app TARGET = main QT += widgets SOURCES += main.cpp
构建并运行
qmake && make && ./main