昨天開始上QT第一天,今天是第二天了,已經上到各種 Event Process 部分,也在課堂上實際撰寫一支小範例,這一個範例是用 specific KeyPress event handler,模擬按上下左右鍵,可以移動坦克車。
執行畫面如下:
補充一點,拜QT Designer的方便,寫這一整支程式可能僅需花費約10分鐘左右喔^^,程式碼如下:
k.h 檔:
/*
*k.h - A QT program header file.
*Authored by YiHua,Chiang
*EMail: microcyh@seed.net.tw
*CYH' Blog: http://tw.myblog.yahoo.com/yh-chiang
*
*Copyright(C) 2009/01/07 http://tw.myblog.yahoo.com/yh-chiang
*
*This program is free software. you can redistribute it and/or
*modify it under the terms of the GNU Public License as published
*by the Free Software Foundation: version 2 of the license.
*
*This program is distributed in the hope that it will be useful,
*but WITHOUT ANY WARRANTY; without even the implied warranty of
*MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
*GNU Lesser Public License for more details.
*/
#include
#include
#include "ui_k.h"
class myGUI : public QWidget, public Ui::Form
{
Q_OBJECT
public:
myGUI(QWidget *parent = 0);
void keyPressEvent( QKeyEvent * );
};
main.cpp 檔:
/*
*main.cpp - A QT program file.
*Authored by YiHua,Chiang
*EMail: microcyh@seed.net.tw
*CYH' Blog: http://tw.myblog.yahoo.com/yh-chiang
*
*Copyright(C) 2009/01/07 http://tw.myblog.yahoo.com/yh-chiang
*
*This program is free software. you can redistribute it and/or
*modify it under the terms of the GNU Public License as published
*by the Free Software Foundation: version 2 of the license.
*
*This program is distributed in the hope that it will be useful,
*but WITHOUT ANY WARRANTY; without even the implied warranty of
*MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
*GNU Lesser Public License for more details.
*/
#include
#include
#include
#include "k.h"
int dir = 0; // o is up 1 is down 2 is left 3 is right
myGUI::myGUI(QWidget *parent):QWidget( parent )
{
setupUi( this );
dir = 0;
}
void myGUI::keyPressEvent( QKeyEvent *event )
{
int x=label->x(); int y=label->y(); int w=label->width(); int h=label->height();
int fw=this->width(); int fh=this->height();
switch( event->key() )
{
case Qt::Key_Up:
{
if( dir != 0 )
{
label->setPixmap( QPixmap(QString::fromUtf8("qt_up.PNG")) );
dir = 0;
}
y = y -2;
if( y <=0 )
y = 0;
}break;
case Qt::Key_Down:
{
if( dir != 1 )
{
label->setPixmap( QPixmap(QString::fromUtf8("qt_dw.PNG")) );
dir = 1;
}
y = y + 2;
if( y+h >= fh )
y = fh-h;
}break;
case Qt::Key_Left:
{
if( dir != 2 )
{
label->setPixmap( QPixmap(QString::fromUtf8("qt_lt.PNG")) );
dir = 2;
}
x = x -2;
if( x <= 0 )
x = 0;
}break;
case Qt::Key_Right:
{
if( dir != 3 )
{
label->setPixmap( QPixmap(QString::fromUtf8("qt_rt.PNG")) );
dir = 3;
}
x = x + 2;
if( x+w>= fw )
x = fw - w;
}break;
}
label->setGeometry( x, y, w, h );
}
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
myGUI *form = new myGUI;
form->show();
return app.exec();
}
ui_k.h 檔:
/********************************************************************************
** Form generated from reading ui file 'k.ui'
**
** Created: Wed Jan 7 19:17:54 2009
** by: Qt User Interface Compiler version 4.4.3
**
** WARNING! All changes made in this file will be lost when recompiling ui file!
********************************************************************************/
#ifndef UI_K_H
#define UI_K_H
#include
#include
#include
#include
#include
#include
QT_BEGIN_NAMESPACE
class Ui_Form
{
public:
QLabel *label;
void setupUi(QWidget *Form)
{
if (Form->objectName().isEmpty())
Form->setObjectName(QString::fromUtf8("Form"));
Form->resize(276, 190);
label = new QLabel(Form);
label->setObjectName(QString::fromUtf8("label"));
label->setGeometry(QRect(90, 85, 38, 38));
QSizePolicy sizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
sizePolicy.setHorizontalStretch(0);
sizePolicy.setVerticalStretch(0);
sizePolicy.setHeightForWidth(label->sizePolicy().hasHeightForWidth());
label->setSizePolicy(sizePolicy);
label->setAutoFillBackground(false);
label->setStyleSheet(QString::fromUtf8(""));
label->setPixmap( QPixmap(QString::fromUtf8("qt_up.PNG")) );
label->setScaledContents(true);
label->setAlignment(Qt::AlignCenter);
retranslateUi(Form);
QMetaObject::connectSlotsByName(Form);
} // setupUi
void retranslateUi(QWidget *Form)
{
Form->setWindowTitle(QApplication::translate("Tank game", "Tank game", 0, QApplication::UnicodeUTF8));
label->setText(QString());
Q_UNUSED(Form);
} // retranslateUi
};
namespace Ui {
class Form: public Ui_Form {};
} // namespace Ui
QT_END_NAMESPACE
#endif // UI_K_H
沒有留言:
張貼留言