Qt3 gui編程

在裝好的linux系統下第一次進行了GUI編程,使用的是Qt框架,只是實現了簡單的幾個控件,不過個人覺得效果挺好的。
實現了顯示label,button,spin和slider結合。主要還是瞭解了這個概念--信號和槽。學習中.....
  1. #include<qapplication.h> //應用框架
  2. #include<qlabel.h>            //label頭文件 
  3. #include<qpushbutton.h> //button頭文件
  4. #include<qhbox.h>             //box
  5. #include<qslider.h>          //slider
  6. #include<qspinbox.h>       //spin
  7. int main(int argc,char *argv[])
  8. {
  9.   QApplication app(argc,argv);
  10.   //label
  11.   QLabel *pLabel = new QLabel("hello Qt!",0);//這裏內容可以使用HTML格式例如:
  12. //QLabel *pLabel = new QLabel("<h2><i>Hello</i> <font color = red >Qt</font>!</h2>,0);
  13.   app.setMainWidget(pLabel);
  14.   pLabel->show();
  15.   //button
  16.   QPushButton* pButton = new QPushButton("Quit",0);
  17.  //我認爲有點像子類化的感覺,就是將響應和事件綁定
  18.   QObject::connect(pButton,SIGNAL(clicked()),&app,SLOT(quit()));
  19.   app.setMainWidget(pButton);
  20.   pButton->show();
  21.   //hbox spinbox slider
  22.   //hbox
  23.   QHBox *pHBox = new QHBox(0);
  24.   pHBox->setCaption("Entering your ages:");
  25.   //設定顯示格式
  26.   pHBox->setMargin(6);
  27.   pHBox->setSpacing(6);
  28.   //spinbox slider
  29.   QSpinBox *pSpin = new QSpinBox(pHBox);
  30.   QSlider *pSlider = new QSlider(Qt::Horizontal,pHBox);
  31.   pSpin->setRange(0,330);
  32.   pSlider->setRange(0,130);
  33.   //信號和槽
  34.   QObject::connect(pSpin,SIGNAL(valueChanged(int)),pSlider,SLOT(setValue(int)));
  35.   QObject::connect(pSlider,SIGNAL(valueChanged(int)),pSpin,SLOT(setValue(int)));
  36.   app.setMainWidget(pHBox);
  37.   pHBox->show();
  38.   return app.exec(); 
  39. }
  40.  //效果見附圖,抓於Mandriva系統下

       本來以爲編譯不會成功,出人意料的是一次性通過:
       1.  生成Qt pro文件 :                            qmake -project
       2. 編譯工程文件生成 make文件 :   qmake  -helloqt.cpp
       3.  make                                         :   make
       4. 運行  . /helloqt
       感覺還是挺好用的,不過還沒有安裝相關文檔,所以現在所做的只能是做一點知道一點了。還是先把文檔安裝上,即用即查!
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章