Qt:QSlider 比眉伴天荒 2023-01-12 11:47 168阅读 0赞 1、新建QtGUI应用,选取QWidget为基类,取消创建界面 2、widget.h中 #ifndef WIDGET_H #define WIDGET_H #include <QSlider> #include <QLabel> class Widget : public QWidget { Q_OBJECT public: Widget(QWidget *parent = 0); ~Widget(); private: QSlider *slider[6]; QLabel *lbl[3]; private slots: void valueChanged1(int value); void valueChanged2(int value); void valueChanged3(int value); }; #endif // WIDGET_H widget.cpp中 #include "widget.h" Widget::Widget(QWidget *parent) : QWidget(parent) { int xpos = 20, ypos = 20; for(int i = 0 ; i < 6 ; i++) { if(i <= 2) { slider[i] = new QSlider(Qt::Vertical, this); slider[i]->setGeometry(xpos, 20, 30, 80); xpos += 30; } else if(i >= 3) { slider[i] = new QSlider(Qt::Horizontal, this); slider[i]->setGeometry(130, ypos, 80, 30); ypos += 30; } slider[i]->setRange(0, 100); slider[i]->setValue(50); } xpos = 20; for(int i = 0 ; i < 3 ; i++) { lbl[i] = new QLabel(QString("%1").arg(slider[i]->value()), this); lbl[i]->setGeometry(xpos+10, 100, 30, 40); xpos += 30; } connect(slider[0], SIGNAL(valueChanged(int)), this, SLOT(valueChanged1(int))); connect(slider[1], SIGNAL(valueChanged(int)), this, SLOT(valueChanged2(int))); connect(slider[2], SIGNAL(valueChanged(int)), this, SLOT(valueChanged3(int))); } void Widget::valueChanged1(int value) { lbl[0]->setText(QString("%1").arg(value)); slider[3]->setValue(slider[0]->value()); } void Widget::valueChanged2(int value) { lbl[1]->setText(QString("%1").arg(value)); slider[4]->setValue(slider[1]->value()); } void Widget::valueChanged3(int value) { lbl[2]->setText(QString("%1").arg(value)); slider[5]->setValue(slider[2]->value()); } Widget::~Widget() { } 效果: ![96df8625da44da6702acbf7e063c2dbd.png][] [96df8625da44da6702acbf7e063c2dbd.png]: /images/20221119/184f1b868199405a9e89ad083dc2fd97.png
还没有评论,来说两句吧...