Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1063097
  • 博文数量: 264
  • 博客积分: 6005
  • 博客等级: 大校
  • 技术积分: 2798
  • 用 户 组: 普通用户
  • 注册时间: 2007-08-08 20:15
文章分类

全部博文(264)

文章存档

2011年(42)

2010年(213)

2009年(4)

2008年(2)

2007年(3)

分类:

2010-10-14 21:35:55

Source

stringhelper.h

#ifndef STRINGHELPER_H
#define STRINGHELPER_H
 
#include
#include
 
class StringHelper : public QObject
{
Q_OBJECT
public:
StringHelper(QObject *parent = 0) : QObject(parent), reverse(false) { }
 
Q_INVOKABLE QString echo(const QString &text) const {
if(reverse == false) { return text; }
 
QString reversed;
for(QString::const_iterator it = text.begin(); it != text.end(); it++) {
reversed.push_front(*it);
}
 
return reversed;
}
 
public slots:
void toggleEcho(bool reverse) { this->reverse = reverse; }
 
protected:
bool reverse;
};
 
#endif // STRINGHELPER_H

ui.qml

import Qt 4.7
 
Rectangle {
id: rect
 
property string text: "Using Qt class to echo this"
 
function updateUI() {
StringHelper.toggleEcho(button.pressed); // calling StringHelper::toggleEcho
text.text = StringHelper.echo(rect.text) // calling StringHelper::echo
}
 
anchors.fill: parent
color: "black"
 
Component.onCompleted: updateUI()
 
Text {
id: text
anchors.centerIn: parent
color: "white"
}
 
Rectangle {
id: button
 
property bool pressed: false
 
width: 100; height: 40
anchors.right: parent.right; anchors.rightMargin: 20
anchors.bottom: parent.bottom; anchors.bottomMargin: 20
radius: 6
color: pressed ? "gray" : "white"
 
Text {
anchors.centerIn: parent
text: "Reverse"
}
 
MouseArea {
anchors.fill: parent
onClicked: { button.pressed = !button.pressed; updateUI() }
}
}
}

main.cpp

#include 
#include
#include
#include "stringhelper.h"
 
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
 
StringHelper stringHelper;
 
QDeclarativeView view;
view.setResizeMode(QDeclarativeView::SizeRootObjectToView);
view.rootContext()->setContextProperty("StringHelper", &stringHelper);
view.setSource(QUrl("./ui.qml"));
 
#if defined(Q_WS_S60) || defined(Q_WS_MAEMO)
view.showMaximized();
#else
view.setGeometry(100, 100, 800, 480);
view.show();
#endif
 
return a.exec();
}

阅读(938) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~