Qt4.4.3自帶教程-幾個Widget間通訊

//幾個widget間通訊
#include <QApplication>
#include <QFont>
#include <QLCDNumber>
#include <QPushButton>
#include <QSlider>
#include <QVBoxLayout>
#include <QWidget>

//建立自定義Widget類
class MyWidget : public QWidget
{
public:
        MyWidget(QWidget *parent = 0);
};

MyWidget::MyWidget(QWidget *parent)
        : QWidget(parent)
{
        QPushButton *quit = new QPushButton(tr( "Quit"));
        quit->setFont(QFont( "Times", 18, QFont::Bold));

        QLCDNumber *lcd = new QLCDNumber(3); //3位數字
        lcd->setSegmentStyle(QLCDNumber::Flat); //設置LCD數字的顯示樣式

        QSlider *slider = new QSlider(Qt::Horizontal);
        slider->setRange(0, 999); //設置Slider的範圍0-999
        slider->setValue(0); //設置Slider的位置

   //quit按鈕負責程序退出
        connect(quit, SIGNAL(clicked()), qApp, SLOT(quit()));
        
         //鏈接slider的valueChanged信號和lcd的display槽
        connect(slider, SIGNAL(valueChanged( int)),
                        lcd, SLOT(display( int)));

        QVBoxLayout *layout = new QVBoxLayout; //垂直佈局
        layout->addWidget(quit); //添加子部件
        layout->addWidget(lcd);
        layout->addWidget(slider);
        
        setLayout(layout); //設置佈局
}

int main( int argc, char *argv[])
{
        QApplication app(argc, argv);
        MyWidget widget;
        widget.show();
         return app.exec();
}
 
截圖:
相關文章
相關標籤/搜索