Game Programming Using QT第三章

(01) 游戏目的

学习使用Qt 集成开发环境开发带有图形交互接口的程序。

Learn to use Qt to develop applications with a graphical user interface using the Qt Creator IDE.

熟悉Qt的核心函数,系统属性,信号槽机制。

Get familiar with the core Qt functionality, property system, and the signals and slots mechanism。

(02) 开始创建游戏

下载安装完成Qt Creator,之后,选择Welcome -> New Project -> Application


(03) 选择Choose之后,命名为tictactoe,选择Next


(04) 新建类名TicTacToeWidget, 父类Qwidget

(05) Next -> Finsh,系统会自动生成代码

1. tictactoe.pro 文件目录结构

#-------------------------------------------------
#
# Project created by QtCreator 2017-03-11T15:01:01
#
#-------------------------------------------------

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = tictactoe
TEMPLATE = app


SOURCES += main.cpp\
        tictactoewidget.cpp

HEADERS  += tictactoewidget.h
2. tictactoewidget.h 头文件

#ifndef TICTACTOEWIDGET_H
#define TICTACTOEWIDGET_H

#include <QWidget>

class TicTacToeWidget : public QWidget
{
    Q_OBJECT

public:
    TicTacToeWidget(QWidget *parent = 0);
    ~TicTacToeWidget();
};

#endif // TICTACTOEWIDGET_H
3. tictactoewidget.cpp 源码文件

We can set a parent for an object in two ways. One way is to call the setParent method defined in QObject that accepts a QObject pointer.

The other way is to pass a pointer to the parent object to  the QWidget constructor of the child object.

#include "tictactoewidget.h"

TicTacToeWidget::TicTacToeWidget(QWidget *parent)
    : QWidget(parent)
{
}

TicTacToeWidget::~TicTacToeWidget()
{

}

4. main.cpp 主文件

QApplication is a singleton class that messages the whole application. In particular, it is responsible for processing event that come from within the application or from external sources.

#include "tictactoewidget.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    TicTacToeWidget w;
    w.show();

    return a.exec();
}
(06) 点击右下方三角按钮运行生成如下结果,

一个空的窗口,窗口的名字为命名的程序名tictactoe

(07)新建窗口完成之后,需要向空白窗口中添加组件。

In Qt, we group objects (such as widgets) into parent-child relationships. This scheme is defined in the superclass of QWidget - QObject.

What is important now is that each object can have a parent object and an arbitrary number of children.

If it doesn't have a parent, then it becomes a top-level window that can be usually be dragged around, resized, and closed.

(08) 布局layout

When we set a layout on a widget, we can start adding widgets and even other layouts, and the mechanism will resize and reposition them according to the rules that we specify.

Qt comes with a predefined set of layouts that are derived from the QLayout class.

QHBoxLayout

QVBoxLayout

QGridLayout

QFormLayout 创建表格形式的布局

To use a layout, we need to create an instance of it and pass a pointer to a widget that we want it to manage.

(09) 可以在构造函数中添加按钮

TicTacToeWidget::TicTacToeWidget(QWidget *parent)
    : QWidget(parent)
{
    QHBoxLayout *layout = new QHBoxLayout;
    QPushButton *button1 = new QPushButton;
    QPushButton *button2 = new QPushButton;
    button1->setText("QT");
    button2->setText("Program");
    layout->addWidget(button1);
    l
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值