Qwt应用笔记

来源:互联网 发布:云帆翻墙软件下载 编辑:程序博客网 时间:2024/06/08 07:19

QWT,全称是Qt Widgets for Technical Applications,是一个基于LGPL版权协议的开源项目,可生成各种统计图。它为具有技术专业背景的程序提供GUI组件和一组实用类,其目标是以基于2D方式的窗体部件来显示数据,数据源以数值,数组或一组浮点数等方式提供,输出方式可以是Curves(曲线),Slider(滚动条),Dials(圆盘),Compasses(仪表盘)等等。该工具库基于Qt开发,所以也继承了Qt的跨平台特性。

本文根据Qwt的官方用户手册,整理出了最简洁使用步骤,按部就班操作即可。

  1. 获取Qwt

    https://sourceforge.net/projects/qwt/files/qwt/6.1.3/获取Qwt6.13的以下内容:

  • qwt-6.1.3.zip——Windows平台下的源码包和HTML帮助文档

Zip file with the Qwt sources and the html documentation for Windows

  • qwt-6.1.3.tar.bz2 ——UNIX平台下的源码包和HTML帮助文档

Compressed tar file with the Qwt sources and the html documentation for UNIX systems ( Linux, Mac, ... )

  • qwt-6.1.3.pdf——Qwt用户手册,有什么不懂就在这个文档上找答案,才能少走弯路。企图少读点英语,你将会绕更多弯路。

Qwt documentation as PDF document.

  • qwt-6.1.3.qch——QwtQt Creator中使用的帮助文档

Qwt documentation as Qt Compressed Help document, that can be loaded into the Qt Assistant or Creator. In the Qt Creator context sensitive help will be available like for Qt classes.

  1. 编译Qwt源码

    想要将Qwt编译成控件,在Qt Creater的设计师界面使用,或在Qt Designer中使用,必须先编译Qwt的源码。在Windows平台下,强烈建议使用MSVC工具链,例如:来编译Qwt,并确保后面的应用程序开发是基于此工具链。否则会带来不兼容问题,后面将说明原因。

    Qt Creater打开Qwt源码目录下的项目文件"qwt.pro",点击"运行"按钮,等待10分钟左右,待整个项目编译完毕。"编译输出"窗口会显示编译整个工程消耗了多长时间,例如:"Elapsed time: 06:04",即表示编译结束。

    若觉得编译太耗时,有个小技巧,使用多核编译。如下图所示配置即可,电脑CPU有几个线程,-j后面的数字最大就是几。

  2. 安装Qwt

    编译完成之后,就可以安装Qwt了,安装过程会将编译出来的有用文件提取出来,放置到Qwt的安装目录。Qwt的安装目录可以在qwtconfig.pri文件中配置,如下面的代码。

    ######################################################################

    # Install paths

    ######################################################################

     

    QWT_INSTALL_PREFIX = $$[QT_INSTALL_PREFIX]

     

    unix {

    QWT_INSTALL_PREFIX = /usr/local/qwt-$$QWT_VERSION

    # QWT_INSTALL_PREFIX = /usr/local/qwt-$$QWT_VERSION-qt-$$QT_VERSION

    }

     

    win32 {

    # QWT_INSTALL_PREFIX = C:/Qwt-$$QWT_VERSION

     

    #Qwt安装在Qt的安装目录下——WXLmjr

    QWT_INSTALL_PREFIX = $$[QT_INSTALL_PREFIX]/Qwt-$$QWT_VERSION-qt-$$QT_VERSION

     

    # QWT_INSTALL_PREFIX = C:/Qwt-$$QWT_VERSION-qt-$$QT_VERSION

    }

     

    QWT_INSTALL_DOCS = $${QWT_INSTALL_PREFIX}/doc

    QWT_INSTALL_HEADERS = $${QWT_INSTALL_PREFIX}/include

    QWT_INSTALL_LIBS = $${QWT_INSTALL_PREFIX}/lib

    本示例中,Qwt的安装路径为:

    QWT_INSTALL_PREFIX = $$[QT_INSTALL_PREFIX]/Qwt-$$QWT_VERSION-qt-$$QT_VERSION

    默认路径如下,即"C:\Qwt-6.1.3"QWT_VERSION表示Qwt的版本号。

    QWT_INSTALL_PREFIX = C:/Qwt-$$QWT_VERSION

    安装操作比较简单:

    1. 先在开始菜单中打开Qt的命令行窗口,如下图:

    1. 将命令行窗口的目录切换到Qwt源码的编译输出目录,本例为:

    build-qwt-Desktop_Qt_5_8_0_MSVC2013_32bit-Debug

    1. 执行指令(MSVC使用nmake installMinGW使用jom install):

    nmake install

    编译器会自动根据编译输出目录下的"Makefile"文件执行安装动作。命令行窗口截图如下:

    至此可以在指定的安装目录发现已安装好的Qwt文件。

  3. 为Qt添加Qwt控件

    Qt官方帮助文档https://doc.qt.io/qtcreator/adding-plugins.html详细讲述了"Adding Qt Designer Plugins"的步骤。总结如下:

    Qwt安装目录下,找到"qwt_designer_plugin.dll"文件,如下图:

    如果想Qt Creator集成的设计师中使用Qwt插件,需要将"qwt_designer_plugin.dll"文件放在下图所示目录:

    之后,打开Qt Creater设计师界面(打开任意一个*.ui文件,即可激活设计师界面),即可看到已经生效的Qwt插件,如下图:

    在【工具】→【Form Editor】→【About Qt Designer Plugins】菜单栏窗口,也可以查看Qt设计师已经找到的插件,如下图:

    如果想在独立的"Qt Designer"中使用Qwt插件,需要将"qwt_designer_plugin.dll"文件放在下图所示目录:

    之后,在开始菜单打开Qt Designer,即可看到已经生效的Qwt插件,如下图:

    在【帮助】→【关于插件】菜单栏窗口,也可以查看Qt设计师已经找到的插件,如下图:

    在这里,特别添加一小节,来为碰到问题的程序猿们解惑——"为什么我已经把"qwt_designer_plugin.dll"文件放到正确的文件夹了,可插件还是发现不了?"这个问题最初困扰了我很长时间,最终答案还是在Qt的官方文档上找到的,⊙﹏⊙b汗……(PS:如果不是最初不想艰难地啃英语,后面也不会绕那么多弯路!)

    Qt官方文档上特地有下面一段文字说明:

    Matching Build Keys

    The Qt Creator that is included in pre-built Qt packages on Windows is built with the Microsoft Visual Studio compiler, whereas the version of Qt shipped for building applications is configured and built to use the MinGW/g++ compiler. Plugins built by using this version of Qt cannot be loaded by Qt Creator because the build-keys do not match. The plugins can only be used in the standalone version of Qt Designer. Choose Help > About Qt Creator to check the Qt version Qt Creator was built with.

    To use Qt Designer plugins that were built for the shipped Qt version, make sure that Qt Creator is built with the same compiler by either recompiling Qt Creator using MinGW or recompiling Qt with Microsoft Visual Studio, depending on which configuration you want to use for your applications.

    总结下来就是:由于build-keys的匹配机制,要想在Qt Creator中使用Qt Designer plugins,必须保证Qt CreatorQt Designer plugins被同一工具链编译。Qt Creator的编译工具链可以在【帮助】→【关于Qt Creator】菜单中查看,如下图:

    从图中可以看到,Qt Creator是由"Qt 5.8.0MSVC 201332bit"工具链编译的,如果你最初安装的是:,你用这个工具链编译Qwt,并将编译产生的"qwt_designer_plugin.dll"文件放在相应的文件夹下,会发现:Qt Creator不能识别Qwt插件(独立的Qt Designer能识别Qwt插件)。在Qt Creator的【工具】→【Form Editor】→【About Qt Designer Plugins】菜单栏窗口中会显示下图中无法加载的结果:

    而使用MSVC工具链,则不存在上述问题,因为Qt CreatorQwt都是用MSVC工具链编译的,满足build-keys匹配机制。当然,你也可以用MSVC工具链把Qt Creator重新编译一遍,但这样做太麻烦,不如直接下载Windows平台下的MSVC工具链。这也是上文建议新手直接下的原因。

  4. Building a Qwt application

    新建一个含有ui文件的Qt应用程序,将想要使用的Qwt控件拖入ui文件,直接运行,肯定是不会通过的。

    Qwt用户手册中写道:

    When building a Qwt application with qmake you might want to load the compiler/linker flags, that are required to build a Qwt application from qwt.prf. Therefore all you need to do is to add "CONFIG += qwt" to your project file and take care, that qwt.prf can be found by qmake.( see http://doc.trolltech.com/4.7/qmake-advanced-usage.html#adding-new-configuration-features ) I recommend not to install the Qwt features together with the Qt features, because you will have to reinstall the Qwt features, with every Qt upgrade.

    All flags and settings that are necessary to compile and link an application using Qwt can be found in the file ${QWT_ROOT}/features/qwt.prf.

    Including qwt.prf in the application project file

    Instead of using qwt.prf as qmake feature it can be included from the application project file:

    include ( ${QWT_ROOT}/features/qwt.prf )

    由此可知,只需将qwt.prf文件包含进Qt应用程序的项目文件" *.pro "即可完成Qwt项目的构建运行。笔者的测试项目中,项目文件的配置如下图(只需添加黄色方框所圈代码):

    想省事的朋友,可以把下面的代码添加进自己的项目文件中。不过,不要忘记了,QWT_ROOT代表的是Qwt的安装目录(上文有说过),要根据自己的实际情况修改。

    #Qwt配置

    QWT_ROOT=X:\Qt\Qt5.8.0.MSVC2013\5.8\msvc2013\Qwt-6.1.3-qt-5.8.0

    include($${QWT_ROOT}/features/qwt.prf)

    至此,大家已经可以按照上述教程开发一个自己的基于Qwt的应用程序了,是不是比网上的大多数教程讲解的都清楚,操作都简洁!!!这也是在笔者根据网上教程做,处处碰壁,最终硬着头皮肯官方英文手册得出的最"正统"的操作步骤,奉献给大家~