實現了顯示label,button,spin和slider結合。主要還是瞭解了這個概念--信號和槽。學習中.....
- #include<qapplication.h> //應用框架
- #include<qlabel.h> //label頭文件
- #include<qpushbutton.h> //button頭文件
- #include<qhbox.h> //box
- #include<qslider.h> //slider
- #include<qspinbox.h> //spin
- int main(int argc,char *argv[])
- {
- QApplication app(argc,argv);
- //label
- QLabel *pLabel = new QLabel("hello Qt!",0);//這裏內容可以使用HTML格式例如:
- //QLabel *pLabel = new QLabel("<h2><i>Hello</i> <font color = red >Qt</font>!</h2>,0);
- app.setMainWidget(pLabel);
- pLabel->show();
- //button
- QPushButton* pButton = new QPushButton("Quit",0);
- //我認爲有點像子類化的感覺,就是將響應和事件綁定
- QObject::connect(pButton,SIGNAL(clicked()),&app,SLOT(quit()));
- app.setMainWidget(pButton);
- pButton->show();
- //hbox spinbox slider
- //hbox
- QHBox *pHBox = new QHBox(0);
- pHBox->setCaption("Entering your ages:");
- //設定顯示格式
- pHBox->setMargin(6);
- pHBox->setSpacing(6);
- //spinbox slider
- QSpinBox *pSpin = new QSpinBox(pHBox);
- QSlider *pSlider = new QSlider(Qt::Horizontal,pHBox);
- pSpin->setRange(0,330);
- pSlider->setRange(0,130);
- //信號和槽
- QObject::connect(pSpin,SIGNAL(valueChanged(int)),pSlider,SLOT(setValue(int)));
- QObject::connect(pSlider,SIGNAL(valueChanged(int)),pSpin,SLOT(setValue(int)));
- app.setMainWidget(pHBox);
- pHBox->show();
- return app.exec();
- }
- //效果見附圖,抓於Mandriva系統下
本來以爲編譯不會成功,出人意料的是一次性通過:
1. 生成Qt pro文件 : qmake -project
2. 編譯工程文件生成 make文件 : qmake -helloqt.cpp
3. make : make
4. 運行 . /helloqt
感覺還是挺好用的,不過還沒有安裝相關文檔,所以現在所做的只能是做一點知道一點了。還是先把文檔安裝上,即用即查!