# ← 註解開頭
# Qt 組件
QT += core gui
TEMPLATE = app
#欲編譯的檔案,每次編譯前可用下頁指令生成
SOURCES += \
main.cpp \
MyWidget.cpp \
... (and others)
HEADERS += \
MyWidget.h \
... (and others)
1.使用command line
qmake-project # 更新project檔
qmake # 生成makefile
make # 編譯
./程式名稱
2.使用Qt Creator
→QObject物件系統
使用QObject -記憶體
void some_function(){
QObject object;
} // ← 於此清除
QObject *object = new QObject( parent );
QObject *object = new QObject();
object -> deleteLater();
class MyWidget : public QWidget {
Q_OBJECT // 固定寫法,記得要補上,讓QT能在compile之前生成必要的程式碼
public:
MyWidget(QWidget *parent = 0) //讓此物件預設沒有parent,但可以明確指定
: QWidget(parent)
{
//...
}
//以下選擇性
signals: //接下來會解釋
public slots: //接下來會解釋
private:
protected:
//...
};
→Event事件實作
QApplication app;
MyWidget *widget = new Mywidget;
widget -> show();
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MyWidget *w = new MyWidget();
w -> show();
return a.exec();
}
MyWidget::MyWidget(QWidget *parent) : QWidget(parent) {
// signal&slot 機制,每隔一段時間得到timeout()事件,觸發Tick()這個自己寫的function QTimer* timer = new QTimer( this ); connect( timer, SIGNAL( timeout() ), this, SLOT( Tick() ) ); timer->start( 10 ); }
//class宣告內加上
protected:
mousePressEvent(QMouseEvent *event); //Override
//CPP檔案實作
MyWidget::mousePressEvent(QMouseEvent *event){
cout << "Click : "
<< event -> x() << ","
<< event -> y() << endl;
}
→Signal & slot
◎ 在送出事件的class:
signals:
void SomethingHappened(); //沒有實作,僅需要被呼叫
◎ 接收事件的class (直接使用public的function亦可)public slots: void SomethingToDo(); //其實跟普通的function沒有區別
//Signal只代表事件發生,不做處理,故沒有內容
//Slot做處理,但不知道是什麼事件 void MyQObjectClass::SomethingToDo(){ // do something... }
connect( 來源物件,SIGNAL( SomethingHappened() ), 目標物件, SLOT( SomethingToDo() ));
// 是 QObject 的static function
emit 來源物件 -> SomethingHappened(); //emit可省略
Child *child = new Child();
ScoreBoard *scoreBoard = new ScoreBoard();
QObject::connect( child , SIGNAL( beingAttack() ),
scoreBoard, SLOT( decreaseScore() ) );
//-----------
當執行child->beingAttack();時,會呼叫所有它連結到的slot
(Qt官方文件寫的是emit child->beingAttack(); , 但作用其實一模一樣)
MyWidget *widget = new MyWidget();
QTimer *timer = new QTimer();
QObject::connect( timer , SIGNAL( timeout() ),
widget, SLOT( repaint() ) );
timer -> start(10); //開始每0.01秒送出一個timeout()的signal
//每0.01秒重繪一次畫面
connect( source , SIGNAL( s() ), DoSomething )
→QPainter
void MyWidget::paintEvent( QPaintEvent * event ) { //當畫面需要重繪時
/* 繪出所有要出現在畫面上的東西,然後QWidget會把繪製出的圖片貼到畫面 */
QPainter painter;
painter.begin(this); //表示要在這個Widget上繪圖
// (Widget是一個QPaintDevice)
painter.fillRect( QRect(10,10,30,30) //x,y,寬,高
QColor(0,255,255,255) //yellow (RGB A)
);
}
MyWidget *widget = new MyWidget();
QTimer *timer = new QTimer();
QObject::connect( timer , SIGNAL( timeout() ),
widget, SLOT( repaint() ) );
timer -> start(10);
//每0.01秒重繪一次畫面
class Game : public QObject{ Q_OBJECT public: Game(){ QTimer *timer = new QTimer(this); QObject::connect( timer , SIGNAL( timeout() ), this , SLOT( Tick() ) ); MakeNewWindow; } void Tick() { UpdateGameObject(); window_ -> repaint(); } private: //... MyWidget *window_; }
int main(int argc,char **argv){ QApplication app(argc,argv); Game game; return app.exec(); }