一、介绍
QWT 是一个 Qt 图形控件库
- https://qwt.sourceforge.io/index.html
提供以下控件:
- 2D绘图(2D plots)
- 刻度(scales)
- 滑块(sliders)
- 刻度盘(dials)
- 指南针(compasses)
- 温度计(thermometers)
- 滚轮(wheels)
- 和旋钮(knobs)
QWT 是一个 Qt 图形控件库
提供以下控件:
从简单的例子来看:
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApp1 { class Program { static void Main(string[] args) { Func<string> greet = () => "Hello, World!"; Console.WriteLine(greet()); Console.ReadLine(); } } }
这句
Func<string> greet = () => "Hello, World!";
到底是什么意思?
如何使用 Qt Creator 的向导建立和编写一个控件插件,下面是步骤。
向导生成的项目,New Project -> Other Project -> Qt Custom Designer Widget -> Custom Widget List 中输入 Widget Class 名字,如果只有一个 Widget,例如 xxx,则源码包含两部分,一是 xxx 类,一是 xxxplugin 类。
到此步,可以在 xxx 类中写最基本的代码,然后编译,生成的 dll 就可以在 Designer 发现控件和拖放控件了。但是,此时控件还没有属性可供设置和读取。
还需要在自定义控件类声明中添加 QDESIGNER_WIDGET_EXPORT 宏用以暴露接口,需要头文件 <QtDesigner/QtDesigner>
#include <QtDesigner/QtDesigner> class QDESIGNER_WIDGET_EXPORT LED : public QWidget { };
另,
构建 Designer plugin 只需要构建 release 版本。
前三步可以在Designer 中使用,如果要在 Creator 中使用,则需要参见下一步。
编译器需要和 Creator 被编译时的编译器保持一致,这点非常重要。
文件部署需要放置 *.h *.lib *dll。参见前文 《Qt5 自定义 Widget 控件 LED(插件)》。
延申阅读:
本文将系统介绍如何编写一个LED控件,并集成到 Designer 和 Creator 中。由最初翻译一篇博文开始。
以 qt-opensource-windows-x86-5.12.2 为例,Qt 安装目录在 c:\Qt\Qt5.12.2\5.12.2\msvc2015_64\ ,即采用 MSVC 2015 64位的编译器和链接库。
这篇文章对于入门作用很大,介绍了如何实现一个 LED 类,以及如何实现 Designer Plungin 类。但是,没有介绍如何如何使用编译出来的共享库。原文一下跳跃到了在 Designer 中拖放和设置控件。
本文做一些补充,先对 pro 文件做一些修改和解释:
MinGW 是 “minimalist GNU for Windows”.的缩写,发音可 Min-gee-double-u
参考:
原文:https://www.ics.com/blog/integrating-custom-widget-qt-designer
This blog post will describe how to write a custom Qt widget and how to integrate it into Qt Designer so that you can drag and drop it onto your designs. It will also provide an understanding of important differences between Qt 4 and Qt 5 when it comes to creating designable widgets.
本博文将描述如何编写一个自定义的 Qt Widget 并且如何将其集成到Qt Designer 中,这样你就能够在设计的时候拖放此控件。同时,本文还说明 Qt4 和 Qt5 中创建 widget 控件时的重要的不同之处。
The example we will work through is an LED (Light Emitting Diode) object. It is designed to be a realistic representation of the real thing:
这个例子将通过一个 LED ( 发光二极管) 对象开始,它被设计成真实呈现实体的特性。
如果要派生一个Qt类,按照向导Add New… > C++ Class,在 Base Class 填入 QGraphicsRectItem , 则默认生成的代码是这样的:
class custom_item : public QGraphicsRectItem { public: custom_item(); };
#include "custom_item.h" custom_item::custom_item() { }
并不像生成的 MainWindow 类
class MainWindow : public QMainWindow { Q_OBJECT public: explicit MainWindow(QWidget *parent = nullptr); ~MainWindow(); private: Ui::MainWindow *ui; };
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); } MainWindow::~MainWindow() { delete ui; }
那么,怎么来添加类具有这样的构造函数呢?
Base Class 选择 QObject ,然后再修改即可。