跨平台编程的利器—Qt:概述

25 篇文章 4 订阅
3 篇文章 0 订阅

Qt是什么,这里就不说了。以前只读过关于它的一些新闻,总体的印象是:它是一个C++语言的跨平台的编程库,最擅长做UI。最近因为项目上的需要,所有工作都必须转移到跨平台上来,因此对跨平台的东西就特别关注,尤其是C/C++方面的(因不用Java,就不提啦)。

先将Qt的官方网站列出来:http://qt.nokia.com,本文属于介绍性质,所以一些内容来源于该网站。

我们使用Qt,主要需要两个东西,Qt Creator和Qt Library,当然也有些其它辅助工具。在Qt网站中,也有叫做"Qt SDK"的东西,它包含了所有的开发工具,下载它也可以,不过它确实很大(Windows版的有1.7G)。

在Windows下安装很简单,有安装程序,运行即可。Linux下安装也不难,现在的Linux都提供了程序安装功能,在图形界面下点几下按钮,与Windows一样了。在两个平台上安装,几乎没有碰到过什么问题,目前Qt Creator的版本为2.5(Qt SDK中集成的是2.4.1,要滞后一些),Library的版本为4.8.1,5.0的beta版出来了,相信不久会出正式版。

一、Qt Creator

Creator是Nokia为Qt专门开发的集成开发环境,就其功能来讲,相当于VS6.0那样一个水平,但它是跨平台的,不知道它是不是拿Qt做的,在Linux平台调用的是gcc编译器,在Windows下调用的是VS的编译器,也可以通过设置改用其它编译器。

我们基于Qt的开发,就要使用Creator,从资料上看,还有一个叫做“Visual Studio Add-In”的东西,将Qt与VS结合,个人感觉无此必要,因为用Qt,无非要跨平台,否则就用VS好了。

用Creator来开发,就编写程序来说,还是很不错的,感觉不太方便的就是调试(估计跨平台的IDE,也就如此了,尤其是在Linux下,这一点是不能和VS相比的,也不太公平)。

二、Qt Library

Qt的实质在于其Library,从其版本的时间变化率来看,Qt有一支很强的开发团队,版本不断在升高,功能也在不断丰富。本人Qt的使用时间并不长,所以对Qt的历史使用经验,就无从谈起,只好谈谈当前的Qt。

现在的Qt Library,是一个相当庞杂的辅助编程库,总体上来说,STL库是标准,Boost库是STL的有益补充,偏向于“系统”级,Qt库则偏向于“实用”级,非常适合程序员的口味。



从上面的模块中,可以看出,除了Qt的强项-图形显示界面及多媒体外,Qt库还提供了网络、脚本、数据库、XML、Web、单元测试、Help等方面的支持,针对特定的平台,还有特别的支持。

Qt支持的图形显示界面,除了正常的window窗体外,还支持OpenGL、OpenVG、SVG,本文将在下面重点介绍Qt的window窗体以及三维OpenGL。

Qt另一个有趣方面是对脚本的支持,给应用程序添加脚本支持,实际上就给了用户定制(自定义)程序行为的一种方法,作者认为这是一个相当酷的功能。Qt中,支持的是Javascript,QtScript模块实现了所有ECMA-262标准规定的对象和属性,并且还有所扩展。以下是一段原文。


QtScript Extensions to ECMAScript

  • __proto__
    The prototype of an object (QScriptValue::prototype()) can be accessed through its__proto__ property in script code. This property has theQScriptValue::Undeletable flag set. For example:
     var o = new Object();
     (o.__proto__ === Object.prototype); // this evaluates to true
  • Object.prototype.__defineGetter__
    This function installs a getter function for a property of an object. The first argument is the property name, and the second is the function to call to get the value of that property. When the function is invoked, thethis object will be the object whose property is accessed. For example:
     var o = new Object();
     o.__defineGetter__("x", function() { return 123; });
     var y = o.x; // 123
  • Object.prototype.__defineSetter__
    This function installs a setter function for a property of an object. The first argument is the property name, and the second is the function to call to set the value of that property. When the function is invoked, thethis object will be the object whose property is accessed. For example:
     var o = new Object();
     o.__defineSetter__("x", function(v) { print("and the value is:", v); });
     o.x = 123; // will print "and the value is: 123"
  • Function.prototype.connect
    This function connects a signal to a slot. Usage of this function is described in the sectionUsing Signals and Slots.
  • Function.prototype.disconnect
    This function disconnects a signal from a slot. Usage of this function is described in the sectionUsing Signals and Slots.
  • QObject.prototype.findChild
    This function is semantically equivalent to QObject::findChild().
  • QObject.prototype.findChildren
    This function is semantically equivalent to QObject::findChildren().
  • QObject.prototype.toString
    This function returns a default string representation of a QObject.
  • gc
    This function invokes the garbage collector.
  • Error.prototype.backtrace
    This function returns a human-readable backtrace, in the form of an array of strings.
  • Error objects have the following additional properties:
    • lineNumber: The line number where the error occurred.
    • fileName: The file name where the error occurred (if a file name was passed toQScriptEngine::evaluate()).
    • stack: An array of objects describing the stack. Each object has the following properties:
      • functionName: The function name, if available.
      • fileName: The file name, if available.
      • lineNumber: The line number, if available.

文本将在后面介绍如何将Javascript与C++相结合。

对数据库的支持也是应用程序的基本要求,可惜的是,为达到跨平台的要求,没有对MS SQL Server的直接支持(只能通过ODBC)。目前所支持的数据库如下表。当然,也可以自己编写数据库驱动。我们经常编写数据库接口函数,试图屏蔽数据库的不同,那么,就试试Qt的吧。


其它模块暂时没有看,以后有应用的机会再介绍吧。

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值