Qt使用第三方组件库新手教程(一)

本文以Qt Material Style Widgets库为例,详细介绍如何把第三方库中的控件应用到自己的工程中。

  1. github下载第三方库文件:https://github.com/laserpants/qt-material-widgets

  2. 在Qt Creater中打开这个project,选择Release模式 并且通过mingw64位的编译器进行编译,就会得到一个以.a结尾的静态库文件:

如果采用msvc 模式的编译器,它就会得到以**.lib**结尾的静态库文件。

本工程中:components是封装组件的目录,examples是对封装组件的使用示例

  1. 将这个库导入到自己的project中

在项目的目录下创建名为SDK的文件夹。 然后在SDK文件夹下创建名为MaterialSDK的文件夹。 然后在MaterialSDK的文件夹下创建名为staticlib和components的文件夹。 最后将libcomponents.a复制到staticlib文件夹下,将组件库中的components的代码和资源复制到components文件夹下。再把components下的cpp文件和h文件添加到你的工程中。

cpp 复制代码
SOURCES += \
    SDK/MaterialSDK/components/qtmaterialappbar.cpp \
    SDK/MaterialSDK/components/qtmaterialautocomplete.cpp \
    SDK/MaterialSDK/components/qtmaterialautocomplete_internal.cpp \
    SDK/MaterialSDK/components/qtmaterialavatar.cpp \
    SDK/MaterialSDK/components/qtmaterialbadge.cpp \
    SDK/MaterialSDK/components/qtmaterialcheckbox.cpp \
    SDK/MaterialSDK/components/qtmaterialcircularprogress.cpp \
    SDK/MaterialSDK/components/qtmaterialcircularprogress_internal.cpp \
    SDK/MaterialSDK/components/qtmaterialdialog.cpp \
    SDK/MaterialSDK/components/qtmaterialdialog_internal.cpp \
    SDK/MaterialSDK/components/qtmaterialdrawer.cpp \
    SDK/MaterialSDK/components/qtmaterialdrawer_internal.cpp \
    SDK/MaterialSDK/components/qtmaterialfab.cpp \
    SDK/MaterialSDK/components/qtmaterialflatbutton.cpp \
    SDK/MaterialSDK/components/qtmaterialflatbutton_internal.cpp \
    SDK/MaterialSDK/components/qtmaterialiconbutton.cpp \
    SDK/MaterialSDK/components/qtmateriallist.cpp \
    SDK/MaterialSDK/components/qtmateriallistitem.cpp \
    SDK/MaterialSDK/components/qtmaterialmenu.cpp \
    SDK/MaterialSDK/components/qtmaterialmenu_internal.cpp \
    SDK/MaterialSDK/components/qtmaterialpaper.cpp \
    SDK/MaterialSDK/components/qtmaterialprogress.cpp \
    SDK/MaterialSDK/components/qtmaterialprogress_internal.cpp \
    SDK/MaterialSDK/components/qtmaterialradiobutton.cpp \
    SDK/MaterialSDK/components/qtmaterialraisedbutton.cpp \
    SDK/MaterialSDK/components/qtmaterialscrollbar.cpp \
    SDK/MaterialSDK/components/qtmaterialscrollbar_internal.cpp \
    SDK/MaterialSDK/components/qtmaterialslider.cpp \
    SDK/MaterialSDK/components/qtmaterialslider_internal.cpp \
    SDK/MaterialSDK/components/qtmaterialsnackbar.cpp \
    SDK/MaterialSDK/components/qtmaterialsnackbar_internal.cpp \
    SDK/MaterialSDK/components/qtmaterialtable.cpp \
    SDK/MaterialSDK/components/qtmaterialtabs.cpp \
    SDK/MaterialSDK/components/qtmaterialtabs_internal.cpp \
    SDK/MaterialSDK/components/qtmaterialtextfield.cpp \
    SDK/MaterialSDK/components/qtmaterialtextfield_internal.cpp \
    SDK/MaterialSDK/components/qtmaterialtoggle.cpp \
    SDK/MaterialSDK/components/qtmaterialtoggle_internal.cpp 

HEADERS += \
    SDK/MaterialSDK/components/qtmaterialappbar.h \
    SDK/MaterialSDK/components/qtmaterialappbar_p.h \
    SDK/MaterialSDK/components/qtmaterialautocomplete.h \
    SDK/MaterialSDK/components/qtmaterialautocomplete_internal.h \
    SDK/MaterialSDK/components/qtmaterialautocomplete_p.h \
    SDK/MaterialSDK/components/qtmaterialavatar.h \
    SDK/MaterialSDK/components/qtmaterialavatar_p.h \
    SDK/MaterialSDK/components/qtmaterialbadge.h \
    SDK/MaterialSDK/components/qtmaterialbadge_p.h \
    SDK/MaterialSDK/components/qtmaterialcheckbox.h \
    SDK/MaterialSDK/components/qtmaterialcheckbox_p.h \
    SDK/MaterialSDK/components/qtmaterialcircularprogress.h \
    SDK/MaterialSDK/components/qtmaterialcircularprogress_internal.h \
    SDK/MaterialSDK/components/qtmaterialcircularprogress_p.h \
    SDK/MaterialSDK/components/qtmaterialdialog.h \
    SDK/MaterialSDK/components/qtmaterialdialog_internal.h \
    SDK/MaterialSDK/components/qtmaterialdialog_p.h \
    SDK/MaterialSDK/components/qtmaterialdrawer.h \
    SDK/MaterialSDK/components/qtmaterialdrawer_internal.h \
    SDK/MaterialSDK/components/qtmaterialdrawer_p.h \
    SDK/MaterialSDK/components/qtmaterialfab.h \
    SDK/MaterialSDK/components/qtmaterialfab_p.h \
    SDK/MaterialSDK/components/qtmaterialflatbutton.h \
    SDK/MaterialSDK/components/qtmaterialflatbutton_internal.h \
    SDK/MaterialSDK/components/qtmaterialflatbutton_p.h \
    SDK/MaterialSDK/components/qtmaterialiconbutton.h \
    SDK/MaterialSDK/components/qtmaterialiconbutton_p.h \
    SDK/MaterialSDK/components/qtmateriallist.h \
    SDK/MaterialSDK/components/qtmateriallist_p.h \
    SDK/MaterialSDK/components/qtmateriallistitem.h \
    SDK/MaterialSDK/components/qtmateriallistitem_p.h \
    SDK/MaterialSDK/components/qtmaterialmenu.h \
    SDK/MaterialSDK/components/qtmaterialmenu_internal.h \
    SDK/MaterialSDK/components/qtmaterialmenu_p.h \
    SDK/MaterialSDK/components/qtmaterialpaper.h \
    SDK/MaterialSDK/components/qtmaterialpaper_p.h \
    SDK/MaterialSDK/components/qtmaterialprogress.h \
    SDK/MaterialSDK/components/qtmaterialprogress_internal.h \
    SDK/MaterialSDK/components/qtmaterialprogress_p.h \
    SDK/MaterialSDK/components/qtmaterialradiobutton.h \
    SDK/MaterialSDK/components/qtmaterialradiobutton_p.h \
    SDK/MaterialSDK/components/qtmaterialraisedbutton.h \
    SDK/MaterialSDK/components/qtmaterialraisedbutton_p.h \
    SDK/MaterialSDK/components/qtmaterialscrollbar.h \
    SDK/MaterialSDK/components/qtmaterialscrollbar_internal.h \
    SDK/MaterialSDK/components/qtmaterialscrollbar_p.h \
    SDK/MaterialSDK/components/qtmaterialslider.h \
    SDK/MaterialSDK/components/qtmaterialslider_internal.h \
    SDK/MaterialSDK/components/qtmaterialslider_p.h \
    SDK/MaterialSDK/components/qtmaterialsnackbar.h \
    SDK/MaterialSDK/components/qtmaterialsnackbar_internal.h \
    SDK/MaterialSDK/components/qtmaterialsnackbar_p.h \
    SDK/MaterialSDK/components/qtmaterialtable.h \
    SDK/MaterialSDK/components/qtmaterialtable_p.h \
    SDK/MaterialSDK/components/qtmaterialtabs.h \
    SDK/MaterialSDK/components/qtmaterialtabs_internal.h \
    SDK/MaterialSDK/components/qtmaterialtabs_p.h \
    SDK/MaterialSDK/components/qtmaterialtextfield.h \
    SDK/MaterialSDK/components/qtmaterialtextfield_internal.h \
    SDK/MaterialSDK/components/qtmaterialtextfield_p.h \
    SDK/MaterialSDK/components/qtmaterialtoggle.h \
    SDK/MaterialSDK/components/qtmaterialtoggle_internal.h \
    SDK/MaterialSDK/components/qtmaterialtoggle_p.h 
  1. 添加库文件到当前project,注意,你当前的project也必须是mingw 编译(至于是debug模式还是release模式无所谓,两者都能运行),也就是说你用什么编译器生成的库文件,你应用的project也要用对应的的编译器,不然就会报错:找不到库文件。
cpp 复制代码
unix|win32: LIBS += -L$$PWD/SDK/MaterialSDK/staticlib/ -lcomponents

INCLUDEPATH += $$PWD/SDK/MaterialSDK/staticlib
DEPENDPATH += $$PWD/SDK/MaterialSDK/staticlib

5.在当前project中调用控件

cpp 复制代码
// widget.cpp
#include "widget.h"
#include "ui_widget.h"
#include<SDK/MaterialSDK/components/qtmaterialtoggle.h>
#include<SDK/MaterialSDK/components/qtmaterialflatbutton.h>
#include<SDK/MaterialSDK/components/qtmaterialflatbutton_internal.h>
using namespace Material;

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    QtMaterialToggle *toggle=new QtMaterialToggle(this);
    QtMaterialFlatButton *btn=new QtMaterialFlatButton(this);
    btn->setForegroundColor(QColor(0,198,231));
    btn->setGeometry(100,100,200,40);
    btn->setText("I am flat btn");
    btn->applyPreset(Material::FlatPreset);
    QFont font;
    font.setFamily("Calibri Light");
    font.setPixelSize(25);
    btn->setFont(font);

}

Widget::~Widget()
{
    delete ui;
}

此系列笔者会继续学习,持续更新,敬请期待!

相关推荐
白杨尚青1 小时前
C++进阶篇(二):多态——虚函数、重写、虚函数表与三个经典坑(附完整代码仓库)
开发语言·c++·多态·虚函数·重写·动态绑定
syagain_zsx1 小时前
spdlog实战:日志分级、滚动与性能调优
linux·c++·日志库·二次开发日志库
WJ.Polar19 小时前
Java封装、继承、多态
java·开发语言
摇滚侠19 小时前
Java 中如何表示负数,有符号和无符号的区别,补码的计算规则,char byte short int long 的范围
java·开发语言
weixin_3077791319 小时前
C++代码实现MATLAB中的fitrtree函数功能
开发语言·c++·算法·matlab
AI wins19 小时前
c++新增頭文件
c++
Shadow(⊙o⊙)19 小时前
仿muduo实现高并发服务器——事件就绪管理Channel.hpp
服务器·c++
爱读书的小胖20 小时前
Codewars:Rot13(C)解题分析
c语言·开发语言
鸽鸽程序猿20 小时前
【项目】【在线判题系统】b端题目增删改
开发语言·python
by2099920 小时前
哈希表——快速找到数据:冲突、删除与扩容的实现
数据结构·c++·哈希算法·散列表