Upload
others
View
0
Download
0
Embed Size (px)
Citation preview
嵌入式计算机系统
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 Embedded Computer Systems 2
MeeGo SDK 1.2 Installation
11-8-18 Embedded Computer Systems 3
MeeGo SDK 1.2 Installation
11-8-18 Embedded Computer Systems 4
The following tools are the core of the MeeGo SDK.
MeeGo SDK 1.2 Installation
11-8-18 Embedded Computer Systems 5
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 Embedded Computer Systems 6
Windows On Windows either Microsoft Visual C++ compiler or MinGW compiler is needed.
MeeGo SDK 1.2 Installation
11-8-18 Embedded Computer Systems 7
Resource
http://developer.meego.com/meego-sdk
Qt Creator
• 跨平台Qt IDE • 专为满足 Qt 开发人员需
求而量身定制的跨平台集成开发环境 (IDE)
• Qt Creator 可在 Windows、Linux/X11 和 Mac OS X 桌面操作系统上运行
• 供开发人员针对多个桌面和移动设备平台创建应用程序
11-8-18 Embedded Computer Systems 8
Qt Creator
11-8-18 Embedded Computer Systems 9
• 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 Embedded Computer Systems 10
Resource: Qt Creator Manual
http://doc.qt.nokia.com/qtcreator-2.1/index.html
Demo
• Sdk environment
• Simple application examples
11-8-18 Embedded Computer Systems 11
Qt 基础
• Qt 是一个用C++编写的跨平台开发框架.
• 可绑定Python, Ruby, C#, Ada, Pascal, Perl, PHP
• ⼀一次性开发可在不同桌面和嵌入式操作系统部署
• Qt 由模块构成 • QtCore • QtGui • QtWebKit • QtNetwork • QtXML • QtMultimedia • …
11-8-18 Embedded Computer Systems 12
Demo: Hello World!
11-8-18 Embedded Computer Systems 13
#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 executables
object files *.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())); }
创建动作
菜单栏和工具栏
void MainWindow::createMenus() { fileMenu = menuBar()->addMenu(tr("&File")); fileMenu->addAction(newAction); fileMenu->addAction(openAction); editMenu = menuBar()->addMenu(tr("&Edit")); editMenu->addAction(cutAction); editMenu->addAction(copyAction); }
创建菜单并将动作添加到菜单上
菜单栏和工具栏
void MainWindow::createToolsBar() { fileToolBar = addToolBar(tr("&File")); fileToolBar->addAction(newAction); fileToolBar->addAction(openAction); editToolBar = addToolBar(tr("&Edit")); editToolBar->addAction(cutAction); editToolBar->addAction(copyAction); }
创建工具栏并将动作添加到工具栏上
状态栏
void MainWindow::createStatusBar()
{
locationLabel = new QLabel("XY");
statusBar()->addWidget(locationLabel);
}
创建状态栏
Qt主窗口介绍
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
创建main.cpp文件,测试主窗口的建立