【目的】
- 練習 Qt Range Control 元件基本用法。
【程式】
- rangectldemo.h
#ifndef RANGECTLDEMO_H #define RANGECTLDEMO_H #include <QWidget> class QSlider; class QDial; class QScrollBar; class QSpinBox; class QProgressBar; class QLCDNumber; class QVBoxLayout; class RangeCtlDemo : public QWidget { Q_OBJECT public: RangeCtlDemo(); private slots: void RangeAdj(const int val); private: QSlider *mySlider; QDial *myDial; QScrollBar *myScrollbar; QSpinBox *mySpinbox; QProgressBar *myProgressbar; QLCDNumber *myLcd; }; #endif
- rangectldemo.cpp
#include <QtGui> #include <QtDebug> #include "rangectldemo.h" RangeCtlDemo::RangeCtlDemo() { mySlider = new QSlider(Qt::Horizontal); mySlider->setRange(0, 99); mySlider->setValue(0); myDial = new QDial; myDial->setRange(0, 99); myDial->setValue(0); myScrollbar = new QScrollBar(Qt::Horizontal); myScrollbar->setRange(0, 99); myScrollbar->setValue(0); mySpinbox = new QSpinBox; mySpinbox->setRange(0, 99); mySpinbox->setValue(0); myProgressbar = new QProgressBar; myProgressbar->setRange(0, 99); myProgressbar->setValue(0); myLcd = new QLCDNumber; myLcd->display(0); connect(mySlider, SIGNAL(valueChanged(int)), this, SLOT(RangeAdj(int))); connect(myDial, SIGNAL(valueChanged(int)), this, SLOT(RangeAdj(int))); connect(myScrollbar, SIGNAL(valueChanged(int)), this, SLOT(RangeAdj(int))); connect(mySpinbox, SIGNAL(valueChanged(int)), this, SLOT(RangeAdj(int))); connect(mySpinbox, SIGNAL(valueChanged(int)), myProgressbar, SLOT(setValue(int))); connect(mySpinbox, SIGNAL(valueChanged(int)), myLcd, SLOT(display(int))); QVBoxLayout *layout = new QVBoxLayout; layout->addWidget(mySlider,0, 0); layout->addWidget(myDial,1, 0); layout->addWidget(myScrollbar,2,0); layout->addWidget(mySpinbox,3,0); layout->addWidget(myProgressbar,4,0); layout->addWidget(myLcd,5,0); layout->addStretch(1); setLayout(layout); } void RangeCtlDemo::RangeAdj(const int val) { mySlider->setValue(val); myDial->setValue(val); myScrollbar->setValue(val); mySpinbox->setValue(val); //myProgressbar->setValue(5); //myLcd->display(val); //qDebug() << val; }
- main.cpp
#include <QApplication> #include "rangectldemo.h" int main(int argc, char *argv[]) { QApplication app(argc, argv); RangeCtlDemo rangeCtlDemo; rangeCtlDemo.resize(300, 50); rangeCtlDemo.setWindowTitle(QObject::tr("Range Control Demo")); rangeCtlDemo.show(); return app.exec(); }
【結果】
【問題】
- 將rangectldemo.cpp註解的地方打開會產生Segment fault,這問題目前還無解。
//myProgressbar->setValue(5); //myLcd->display(val);
【備註】
- QProgressBar 與 QProgress 是不同的東西。