嵌入式计算机系统
Lecture #6
MeeGo SDK 与 QT 基础
内容来自于meego.com以及MeeGo相关公开教程
MeeGo SDK 1.2 Installation
• Installation prerequisites
• Windows / Linux
• Support for VT required to use QEMU VM
• 3~10GB disk space
11-8-18 2 Embedded Computer Systems
MeeGo SDK 1.2 Installation
11-8-18 3 Embedded Computer Systems
MeeGo SDK 1.2 Installation
11-8-18 4 Embedded Computer Systems
The following tools are the core of the MeeGo SDK.
MeeGo SDK 1.2 Installation
11-8-18 5 Embedded Computer Systems
SDK targets
An SDK target is a collection of 3 components: toolchain, build libraries, and virtual machine image with device skin.
Targets exist for x86 and ARM-based devices such as tablets, handsets and netbooks.
MeeGo SDK 1.2 Installation
11-8-18 6 Embedded Computer Systems
Windows
On Windows either Microsoft Visual C++ compiler or MinGW compiler is needed.
MeeGo SDK 1.2 Installation
11-8-18 7 Embedded Computer Systems
Resource
http://developer.meego.com/meego-sdk
Qt Creator
• 跨平台 Qt IDE
•
专为满足 Qt 开发人员需求而量身定制的跨平台集 成开发环境 (IDE)
•
Qt Creator 可在 Windows、Linux/X11 和 Mac OS X 桌面操作系统上运行
•
供开发人员针对多个桌面和移动设备平台创建应用 程序
11-8-18 8 Embedded Computer Systems
Qt Creator
11-8-18 9 Embedded Computer Systems
• Sophisticated Code Editor
• Qt Creator´s advanced code editor provides support for editing C++ and QML (JavaScript), context-sensitive help, code completion, navigation and more .
• Version Control
• Qt Creator integrates with most popular version control systems, including Git, Subversion, Perforce, CVS and Mercurial.
• Integrated UI Designers
• Qt Creator provides two integrated visual editors: Qt Designer for building UIs from Qt widgets, and Qt Quick Designer for developing animated UIs with the QML language.
• Project & Build Management
• Whether you import an existing project or create one from scratch, Qt Creator generates all the necessary files.
Support for cross-qmake and CMake is included.
• Desktop & Mobile Targets
• Qt Creator provides support for building and running Qt applications for desktops and mobile devices. Build settings allow you to quickly switch between build targets.
• Qt Simulator
• Available as part of the SDK, the Qt Simulator to test Qt application for mobile devices in an environment similar to that of the target device.
Qt Creator
11-8-18 10 Embedded Computer Systems
Resource: Qt Creator Manual
http://doc.qt.nokia.com/qtcreator-2.1/index.html
Demo
• Sdk environment
• Simple application examples
11-8-18 11 Embedded Computer Systems
Qt 基础
• Qt
是一个用C++
编写的跨平台开发框架.
• 可绑定Python, Ruby, C#, Ada, Pascal, Perl, PHP
• ⼀一次性开发可在不同桌面和嵌入式操作系统部署
• Qt 由模块构成
• QtCore
• QtGui
• QtWebKit
• QtNetwork
• QtXML
• QtMultimedia
• …
11-8-18 12 Embedded Computer Systems
Demo : Hello World!
11-8-18 13 Embedded Computer Systems
#include <QtGui/QApplication>
#include <QLabel>
int main(int argc, char *argv[]) {
QApplication a(argc, argv);
QLabel lab("Hello World!");
lab.show();
return a.exec();
}
Qt 特征⼀一
• 面向对象
• Qt 具有模块设计和注重软件构件或元素的可重用性的 特点
• 构件支持
• Qt提供信号signal和槽slot的概念 ,支持对象之间在彼 此不知道对方的情况下合作,使得 Qt适合构件编程
• 友好的联机帮助
• Qt提供了大量的联机参考文件
Qt 特征二
• 便利性
• Qt是跨平台的GUI工具包 ,对编程者隐藏了处理不同 窗口系统时的潜在问题
• 国际化
• Qt为本地化应用提供了完全的支持 ,所有用户界面文
本都可以基于消息翻译表被翻译成各国语言
Qt特征三
• 丰富的 API函数
• Qt为开发者提供了大量的函数
• 可用户化外观
• Qt支持主题 ,基于 Qt的应用程序能够在Windows外观 , Motif外观以及其他⼀一些用户外观主题之间切换
• 完整的组件工具
• Qt编程的基本模块叫做组件 , Qt含有用来创建专业外
观用户界面需要的所有组建
Qt介绍
• Who uses Qt ?
• Write code once to target multiple platforms
• Create amazing user experience
• Do more with less and faster
• Blend web and native code in a single application
Qt SDK
• Qt SDK
Qt中主要的类介绍
1. QObject
• 几乎所有 Qt类和部件(widget )的基类
• QObject类是所有能够处理signal , slot和事件的Qt对象的基类 , 原型如下 :
QObject::QObject(Qobject *parent=0,const char *name=0)
• 包含很多组成 Qt的机制 :
1. 事件
2. 信号和槽
3. 属性
4. 内存管理
Qt中主要的类介绍
2. QApplication
• QApplication类负责GUI应用程序的控制流和主要的 设置,它包括主事件循环体,负责处理和调度所有 来自窗口系统和其他资源的事件
• 处理应用程序的开始,结束以及会话管理
• QApplication是QObject类的子类
Qt中主要的类介绍
3. Qwidget
• QWidget是所有用户接口对象的基类,继承了 QObject类的属性
• 组件是用户界面的单元组成部分,接收鼠标,
键盘和从其它窗口系统来的事件
• QWidget类有很多成员函数,但⼀一般不直接使 用,而通过子类继承来使用其函数功能
• QWidget 是QObject类的子类
元数据( Meta data)
l 每一个 QObject 都有一个元对象
l 元数据通过元对象编译器(moc)在编译时组合在一起。
l 元对象涉及:
l 类名 (QObject::className)
l 继承 (QObject::inherits)
l 属性
l 信号和槽
l 普通信息(QObject::classInfo)
sources
*.cpp object files executables
*.o
headers
*.h generated
moc_*.cpp
includes
compiles links
compiles mocs
属性 (Properties)
l
QObject有getter 和 setter 函数属性
l
命名策略 : angle, setAngle
class AngleObject : public QObject {
Q_OBJECT
Q_PROPERTY(qreal angle READ angle WRITE setAngle) public:
AngleObject(qreal angle, QObject *parent = 0);
qreal angle() const;
void setAngle(qreal);
private:
qreal m_angle;
};
内存管理
l
QObject 可以有父对象和子对象
l
当一个父对象被删除,它的子对象也同样被删除。
l
以上代码执行后,会发生什么情况?
QObject *parent = new QObject();
QObject *child1 = new QObject(parent);
QObject *child2 = new QObject(parent);
QObject *child1_1 = new QObject(child1);
QObject *child1_2 = new QObject(child1);
delete parent;
信号和槽
• 信号和槽机制是 Qt的⼀一个主要特征 ,是 Qt与其它工 具包最不相同的部分。
• 在图形用户界面编程中,经常会出现窗口的⼀一个部 件发生的变化会被通知给另⼀一个部件。
• Qt采用信号和槽实现对象部件之间的通信
• 通过反馈的方式动态地或松散地将事件和状态变化联
系起来
什么是槽 ?
l
槽在各种槽段( section )中定义。
l
槽可以返回值,但并不是通过联接。
l
任何数量的信号可以关联到一个槽。
l
它以一个普通函数的方式实现,可以作为普通函数被调用。
public slots:
void aPubSlot();
protected slots:
void aProtSlot();
private slots:
void aPrivSlot();
connect(src, SIGNAL(sig()), dest, SLOT(slt()));
什么是信号 ?
l
信号在信号段( section )中定义
l
信号总是返回空
l
信号不必实现
l 由moc来提供实现
l
信号可以关联到任意数量的槽上
l
槽能以任意次序被激发
l
信号使用 emit 关键字发射出去。
signals:
void aSign();
信号和槽的声明
class Student:public Qobject { Q_OBJECT
public:
Student() {myMark = 0;}
int getMark() const {return myMark;}
public slots:
void setMark(int newMark);
signals:
void markChanged(int newMark);
private:
int myMark;
}
在 Qt程序设计中,包含signals和slot的类都要加上Q_OBJECT的定义。下面给出
⼀一个在类中定义 signal和slot的例子
信号和槽的声明
Signal的发出⼀一般在事件的处理函数中 ,利用 emit发出 signal ,下面的例子中在事件处理结束后发出 signal void Student::setMark(int newMark) {
if(newMark != myMark) { myMark = newMark;
emit markChanged(myMark);
}
}
信号和槽的连接
在 signal 和 slot 声明以后,需要使用 connect() 函数 将它们连接起来。 Connect() 函数属于 QObject类 的成员函数,能够连接 signal 和 slot ,也能够连 signal和signal。函数原型如下
bool connect(const QObject *sender, const char
*signal, const QObject *receiver, const char *slot) 其中第⼀一个和第三个参数指出 signal和slot属于的
对象或组件
信号和槽的连接
使用connect()函数进行连接的时候 , 还需要用到 SIGNAL() 和 SLOT() 两 个宏,使用方法如下:
QLabel *label = new QLabel;
QScrollBar *scroll = new QScrollBar;
QObject::connect(scroll,
SIGNAL(valueChanged(int)), label,
SLOT(setNum(int)));
信号和槽的连接
QObject::connect( src, SIGNAL( signature ), dest, SLOT( signature ) );
<fun name> ( <arg type>... )
clicked() toggled(bool) setText(QString)
textChanged(QString) rangeChanged(int,int) setTitle(QString txt)
setValue(42)
签名由函数名和参数类型组成。不允许有变量名或值。
信号和槽的连接方式
• 同⼀一个信号连接到多个槽
connect(slider, SIGNAL(valueChanged(int)), spinBox,
SLOT(setValue(int)));
connect(slider, SIGNAL(valueChanged(int)), this, SLOT(updateStatusBarIndicator (int)));
• 多个信号连接到同⼀一个槽
Connect(lcd, SIGNAL(overflow()), this, SLOT(handleMathError()));
Connect(calculator, SIGNAL(divisionByZero()), this,
SLOT(handleMathError()));
信号和槽的连接方式
• ⼀一个信号连接到另外⼀一个信号
connect(lineEdit, SIGNAL(textChanged(const Qstring&)), this, SLOT(updateRecord(const Qstring&)))
• 取消⼀一个连接
disconnect(lcd, SIGNAL(overflow()),this, SLOT(handleMathError()));
取消⼀一个连接不是很常用 ,因为 Qt会在⼀一个对象被删除后
自动取消这个对象所包含的所有连接
Qt对话框
• 对话框是⼀一种特殊的窗口,⼀一般用来提供反馈信息 或从用户获取输入。
• 给用户提供了同应用程序进行交互的方式
Qt对话框
• 模态对话框
• 最普遍的对话框。在未消失前用户不能够与同⼀一个应 用程序的其他窗口进行交互,直到该对话框关闭。
• 非模态对话框
• 当对话框打开的同时,用户还可与同⼀一应用程序的其
他窗口进行交互。
Qt对话框
• Qt对话框的类层次结构图
QWidget
QDialog
QColorDialog QProcessDialog QFileDialog QInputDialog
几种 Qt内置对话框介绍
• 颜色对话框 QColorDialog,允许用户选择设置颜色
• 错误对话框 QErrorDialog,显示错误信息
• 文件对话框 QFileDialog,允许用户选择⼀一个或多个文件或目录
• 字体对话框 QFontDialog,允许用户选择 、设置字体
• 输入对话框 QInputDialog,允许用户输入文本信息
• 页设置对话框 QPageSetupDialog,可配置与打印相关的页面设置
• 进度对话框 QProgressDialog,指示出⼀一项操作的工作进度 ,而且提 示用户该操作是否停滞
• 打印对话框 QPrintDialog,配置打印机相关选项
查找对话框示例
• 通过⼀一个简单的查找对话框的实现了解 Qt中对话框的
创建过程,布局管理,信号和槽机制以及⼀一些简单控
件的使用
查找对话框示例
头文件finddialog.h
#ifndef FINDDIALOG_H
#define FINDDIALOG_H
#include <QDialog>
class QCheckBox;
class QLabel;
class QLineEdit;
class QPushButton;
class FindDialog : public QDialog {
Q_OBJECT public:
FindDialog(QWidget *parent = 0);
查找对话框示例
signals:
void findNext(const QString &str, Qt::CaseSensitivity cs);
void findPrevious(const QString &str, Qt::CaseSensitivity cs);
private slots:
void findClicked();
void enableFindButton(const QString &text);
private:
QLabel *label;
QLineEdit *lineEdit;
QCheckBox *caseCheckBox;
QCheckBox *backwardCheckBox;
QPushButton *findButton;
QPushButton *closeButton;
};
#endif
查找对话框示例
1 #include <QtGui>
2 #include "finddialog.h"
3 FindDialog::FindDialog(QWidget *parent) 4 : QDialog(parent)
5 {
6 label = new QLabel(tr("Find &what:"));
7 lineEdit = new QLineEdit;
8 label->setBuddy(lineEdit);
9 caseCheckBox = new QCheckBox(tr("Match &case"));
10 backwardCheckBox = new QCheckBox(tr("Search &backward"));
11 findButton = new QPushButton(tr("&Find"));
12 findButton->setDefault(true);
13 findButton->setEnabled(false);
14 closeButton = new QPushButton(tr("Close"));
查找对话框示例
15 connect(lineEdit, SIGNAL(textChanged(const QString &)),
16 this, SLOT(enableFindButton(const QString &)));
17 connect(findButton, SIGNAL(clicked()), this, SLOT(findClicked()));
19 connect(closeButton, SIGNAL(clicked()), this, SLOT(close()));
查找对话框示例
• 通过 connect函数实现了信号和槽的连接
• 只要行编辑器中的文本发生变化,就会调用私有槽 enableFindButton
• 当用户单击 Find按钮时 ,会调用 findClicked私有槽
• 当用户单击 Close时 ,对话框关闭
查找对话框示例
21 QHBoxLayout *topLeftLayout = new QHBoxLayout;
22 topLeftLayout->addWidget(label);
23 topLeftLayout->addWidget(lineEdit);
24 QVBoxLayout *leftLayout = new QVBoxLayout;
25 leftLayout->addLayout(topLeftLayout);
26 leftLayout->addWidget(caseCheckBox);
27 leftLayout->addWidget(backwardCheckBox);
28 QVBoxLayout *rightLayout = new QVBoxLayout;
29 rightLayout->addWidget(findButton);
30 rightLayout->addWidget(closeButton);
31 rightLayout->addStretch();
32 QHBoxLayout *mainLayout = new QHBoxLayout;
33 mainLayout->addLayout(leftLayout);
34 mainLayout->addLayout(rightLayout);
35 setLayout(mainLayout);
36 setWindowTitle(tr("Find"));
37 setFixedHeight(sizeHint().height());
38 }
查找对话框示例
• Qt提供了布局管理器布局子窗口部件
• 通过 QHBoxLayout , QVBoxLayout和QGridLayout这
三个布局的不同嵌套组合,就可以构建出相当复杂
的对话框。
查找对话框示例
39 void FindDialog::findClicked() 40 {
41 QString text = lineEdit->text();
42 Qt::CaseSensitivity cs =
43 caseCheckBox->isChecked() ? Qt::CaseSensitive :Qt::CaseInsensitive;
44 if (backwardCheckBox->isChecked()) { 45 emit findPrevious(text, cs);
46 } else {
47 emit findNext(text, cs);
48 } 49 }
50 void FindDialog::enableFindButton(const QString &text) 51 {
52 findButton->setEnabled(!text.isEmpty());
53 }
查找对话框示例
• 以上两个函数是对话框中用到的槽
• 当用户单击 Find按钮时,就会调用findClicked槽。
而该槽会发射 findPrevious或findNext信号,这取 决于 Search backward选项的取值
• 只要用户改变了行编辑器中的文本,就会调用 enableFindButton槽。如果在行编辑器中有文本,
该槽就会启用 Find按钮,否则它会禁用Find按钮
查找对话框示例
1 #include <QApplication>
2 #include "finddialog.h"
3 int main(int argc, char *argv[]) 4 {
5 QApplication app(argc, argv);
6 FindDialog *dialog = new FindDialog;
7 dialog->show();
8 return app.exec();
9 }
创建 main.cpp文件测试FindDialog窗口部件
Qt主窗口介绍
• 应用程序的主窗口提供了用于构建应用程序用户界 面的框架
• 主窗口⼀一般包括菜单栏,工具栏,状态栏,中央窗 口部件等子控件
• 在 Qt中 ,通过子类化 QMainWindow可以创建⼀一个应
用程序的主窗口
Qt主窗口介绍
• 下面通过⼀一个简单的代码分析了解 Qt中主窗口的创 建过程
• 实现主窗口的源代码分别放在 mainwindow.h和
mainwindow.cpp中
Qt主窗口介绍
Mainwindow.h
class MainWindow : public QMainWindow {
Q_OBJECT public:
MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void newFile();
void openFile();
void cut();
void copy();
private:
void createActions();
void createMenus();
void createStatusBar();
void createToolsBar();
private:
QMenu *fileMenu;
QMenu *editMenu;
QAction *newAction;
QAction *openAction;
QAction *cutAction;
QAction *copyAction;
QToolBar *fileToolBar;
QToolBar *editToolBar;
};
Qt主窗口介绍
• 在头文件中,将 MainWindow类定义为 QMainWindow的子类 。
• 像 File->New这样的菜单项 ,在 MainWindow中被实
现为私有槽
Qt主窗口介绍
mainwindow.cpp
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
{
createActions();
createMenus();
createToolsBar();
createStatusBar();
}
MainWindow类的构造函数,通过调用类的私有函数 createActions();
createMenus(); createToolsBar(); createStatusBar();分别创建主窗
口的菜单栏,工具栏和状态栏
菜单栏和工具栏
• Qt通过 动作的概念简化了菜单和工具栏的编程
• ⼀一个动作 action就是⼀一个可以添加到任意数量的菜 单和工具栏上的项
• 创建菜单和工具栏⼀一般包括如下步骤:
• 创建并设置动作
• 创建菜单并把动作添加到菜单上
• 创建工具栏并把动作添加到工具栏上
菜单栏和工具栏
void MainWindow::createActions() {
//actions in fileMenu
newAction = new QAction(tr("&New"),this);
connect(newAction,SIGNAL(triggered()),this,SLOT(newFile()));
openAction = new QAction(tr("&Open"),this);
connect(openAction,SIGNAL(triggered()),this,SLOT(openFile()));
//actions in editMenu
cutAction = new QAction(tr("&Cut"),this);
connect(cutAction,SIGNAL(triggered()),this,SLOT(cut()));
copyAction = new QAction(tr("&Copy"),this);
connect(copyAction,SIGNAL(triggered()),this,SLOT(copy()));
}