首页 > *nix应用编程 > Linux下qml的完整示例cmake工程

Linux下qml的完整示例cmake工程

2013年7月9日 发表评论 阅读评论 6,709 次浏览

前些天用gtk写fcitx的界面,十足的蛋疼,现在fcitx开发组貌似打算是用qml了,我也跟着学,看同事用qml做出来的界面好绚,不错不错!qml是一种描述性的脚本语言,文件格式以.qml结尾。语法格式非常像CSS,但又支持javascript形式的编程控制(此句摘自百度百科)。甚好我初学计算机时就是从html/css做网页入手的,javascript掌握得也还马马虎虎,毕竟在上一家公司做一个项目时,整整写了8个月的js/Ext前端,不容易啊!

废话少说,下面看一个qml的完整示例cmake工程,最终运行结果界面如下图所示:

工程代码见附件或https://github.com/lenky0401/Hello-Hinata
其中文件main.qml的内容如下:

import QtQuick 1.1

Rectangle {
    width: 396
    height: 339
    id: main
    objectName: "main"

    Column {
        Image {
            width: 396; height: 299
            smooth: true
            source: "Hinata.jpg"
        }

        Text {
            text: "Hello-Hinata!"
            font.family: "Helvetica"
            font.pointSize: 24
            color: "red"
        }
    }
}

可以看到,也就是在一个矩形框里分栏放置一个图片和一串字符,关于各个元素的属性设置,可以在这里(http://qt-project.org/doc/qt-4.8/qdeclarativeelements.html)看到相关帮助。

采用cmake组织工程,其编译、执行过程如下:

lenky@lenky-Inspiron-545s:~/fcitx/Hello-Hinata$ mkdir build
lenky@lenky-Inspiron-545s:~/fcitx/Hello-Hinata$ cd build/
lenky@lenky-Inspiron-545s:~/fcitx/Hello-Hinata/build$ cmake ../
-- The C compiler identification is GNU 4.7.3
-- The CXX compiler identification is GNU 4.7.3
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Looking for Q_WS_X11
-- Looking for Q_WS_X11 - found
-- Looking for Q_WS_WIN
-- Looking for Q_WS_WIN - not found
-- Looking for Q_WS_QWS
-- Looking for Q_WS_QWS - not found
-- Looking for Q_WS_MAC
-- Looking for Q_WS_MAC - not found
-- Found Qt4: /usr/bin/qmake (found version "4.8.4") 
-- Configuring done
-- Generating done
-- Build files have been written to: /home/lenky/fcitx/Hello-Hinata/build
lenky@lenky-Inspiron-545s:~/fcitx/Hello-Hinata/build$ make
Scanning dependencies of target Hello-Hinata_automoc
[ 25%] Automoc for target Hello-Hinata
Generating moc_main_application.cpp
[ 25%] Built target Hello-Hinata_automoc
Scanning dependencies of target Hello-Hinata
[ 50%] Building CXX object CMakeFiles/Hello-Hinata.dir/main.cpp.o
[ 75%] Building CXX object CMakeFiles/Hello-Hinata.dir/main_application.cpp.o
[100%] Building CXX object CMakeFiles/Hello-Hinata.dir/Hello-Hinata_automoc.cpp.o
Linking CXX executable Hello-Hinata
[100%] Built target Hello-Hinata
lenky@lenky-Inspiron-545s:~/fcitx/Hello-Hinata/build$ ./Hello-Hinata 
...

cmake工程需要把几个依赖库加上,否则会出错,比如如下错误就是分别差${QT_QTCORE_LIBRARY}、${QT_QTGUI_LIBRARY}、${QT_QTDECLARATIVE_LIBRARY}的情况:

/usr/bin/ld: CMakeFiles/Hello-Hinata.dir/main_application.cpp.o: undefined reference to symbol '_ZN7QObject7connectEPKS_PKcS1_S3_N2Qt14ConnectionTypeE'
/usr/bin/ld: note: '_ZN7QObject7connectEPKS_PKcS1_S3_N2Qt14ConnectionTypeE' is defined in DSO /usr/lib/i386-linux-gnu/libQtCore.so.4 so try adding it to the linker command line
/usr/lib/i386-linux-gnu/libQtCore.so.4: could not read symbols: Invalid operation
collect2: error: ld returned 1 exit status
make[2]: *** [Hello-Hinata] Error 1
make[1]: *** [CMakeFiles/Hello-Hinata.dir/all] Error 2
make: *** [all] Error 2
/usr/bin/ld: CMakeFiles/Hello-Hinata.dir/Hello-Hinata_automoc.cpp.o: undefined reference to symbol '_ZN12QApplication11qt_metacallEN11QMetaObject4CallEiPPv'
/usr/bin/ld: note: '_ZN12QApplication11qt_metacallEN11QMetaObject4CallEiPPv' is defined in DSO /usr/lib/i386-linux-gnu/libQtGui.so.4 so try adding it to the linker command line
/usr/lib/i386-linux-gnu/libQtGui.so.4: could not read symbols: Invalid operation
collect2: error: ld returned 1 exit status
make[2]: *** [Hello-Hinata] Error 1
make[1]: *** [CMakeFiles/Hello-Hinata.dir/all] Error 2
make: *** [all] Error 2
CMakeFiles/Hello-Hinata.dir/main_application.cpp.o: In function `MainApplication::setup()':
main_application.cpp:(.text+0x88): undefined reference to `QDeclarativeView::QDeclarativeView(QWidget*)'
main_application.cpp:(.text+0xc5): undefined reference to `QDeclarativeView::setSource(QUrl const&)'
main_application.cpp:(.text+0xf9): undefined reference to `QDeclarativeView::rootContext() const'
main_application.cpp:(.text+0x10f): undefined reference to `QDeclarativeContext::setContextProperty(QString const&, QObject*)'
main_application.cpp:(.text+0x210): undefined reference to `QDeclarativeView::engine() const'
main_application.cpp:(.text+0x24a): undefined reference to `QDeclarativeView::rootContext() const'
main_application.cpp:(.text+0x260): undefined reference to `QDeclarativeContext::setContextProperty(QString const&, QObject*)'
collect2: error: ld returned 1 exit status
make[2]: *** [Hello-Hinata] Error 1
make[1]: *** [CMakeFiles/Hello-Hinata.dir/all] Error 2
make: *** [all] Error 2

附件:Hello-Hinata.tar.bz2

有用资料:
C++程序中使用QML绑定机制
http://blog.csdn.net/u010411742/article/details/8966037

Using QML Bindings in C++ Applications

http://qt-project.org/doc/qt-4.8/qtbinding.html

QML各种相关介绍
http://blog.csdn.net/henreash/article/category/1226184

C++ and QML integration
https://developer.blackberry.com/cascades/documentation/dev/integrating_cpp_qml/index.html

转载请保留地址:http://www.lenky.info/archives/2013/07/2321http://lenky.info/?p=2321


备注:如无特殊说明,文章内容均出自Lenky个人的真实理解而并非存心妄自揣测来故意愚人耳目。由于个人水平有限,虽力求内容正确无误,但仍然难免出错,请勿见怪,如果可以则请留言告之,并欢迎来讨论。另外值得说明的是,Lenky的部分文章以及部分内容参考借鉴了网络上各位网友的热心分享,特别是一些带有完全参考的文章,其后附带的链接内容也许更直接、更丰富,而我只是做了一下归纳&转述,在此也一并表示感谢。关于本站的所有技术文章,欢迎转载,但请遵从CC创作共享协议,而一些私人性质较强的心情随笔,建议不要转载。

法律:根据最新颁布的《信息网络传播权保护条例》,如果您认为本文章的任何内容侵犯了您的权利,请以Email或书面等方式告知,本站将及时删除相关内容或链接。

分类: *nix应用编程 标签: ,
  1. 本文目前尚无任何评论.
  1. 本文目前尚无任何 trackbacks 和 pingbacks.