QtWidget使用Material风格的组件库

文章介绍了qt-material-widget组件库,这是一个基于QtWidget的Material设计风格组件集合。详细步骤指导了如何编译该库,包括使用mingw64编译得到.a静态库文件,以及在项目中导入和使用这些组件的方法。文章还列出了可用的Material组件示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、qt-material-widget组件库介绍

该组件库拥有炫酷的Material风格的组件,并且该组件库基于QtWidget开发的,目前实现了大约20个Material风格的组件,下面教大家如何编译该组件库,并将这个组件库运用到自己的项目中。

二、编译qt-material-widget组件库

该组件库的下载地址:qt-material-widgets
下载解压后的文件目录:
在这里插入图片描述
点击qt-material-widgets.pro的项目工程文件,选择Release模式并且通过mingw64位的编译器进行编译,就会得到一个以.a结尾的静态库文件:
在这里插入图片描述

注意:因为我使用的是mingw模式的编译器,所以它会采用gcc进行编译,得到的静态库文件会以.a结尾。如果采用msvc模式的编译器,它就会得到以.lib结尾的静态库文件

该组件库的项目工程分析:
在这里插入图片描述

如上图所示:components是封装组件的目录,examples是对封装组件的使用,叫我们如何使用这些组件到我们的项目中。

该组件库目前封装组件的效果图如下:

App Bar QtMaterialAppBar
Auto Complete QtMaterialAutoComplete
Avatar QtMaterialAvatar
Badge QtMaterialBadge
Check Box QtMaterialCheckBox
Circular Progress QtMaterialCircularProgress
Dialog QtMaterialDialog
Drawer QtMaterialDrawer
FAB QtMaterialFloatingActionButton
Flat Button QtMaterialFlatButton
Icon Button QtMaterialIconButton
Progress QtMaterialProgress
Radio Button QtMaterialRadioButton
Raised Button QtMaterialRaisedButton
Scroll Bar QtMaterialScrollBar
Slider QtMaterialSlider
Snackbar QtMaterialSnackBar
Tabs QtMaterialTabs
Text Field QtMaterialTextField
Toggle QtMaterialToggle

三、使用qt-material-widget组件库

使用qt-material-widgets组件库的流程如下:

  • 在项目的目录下创建名为SDK的文件夹
  • 然后在SDK文件夹下创建名为MaterialSDK的文件夹
  • 然后在MaterialSDK的文件夹下创建名为staticlib和components的文件夹
  • 最后将libcomponents.a复制到staticlib文件夹下,将组件库中的components的代码和资源复制到components文件夹下

做完上述操作后,进入pro文件中,输入以下命令:

//导入组件库的源文件
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 \
    main.cpp \
    widget.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 \
    widget.h

#import Material SDK(导入Material组件库的SDK)
LIBS += D:/qtDemo/ui/QtMaterialWidgets/SDK/MaterialSDK/staticlib/libcomponents.a
INCLUDEPATH += D:/qtDemo/ui/QtMaterialWidgets/SDK/MaterialSDK/components
PRE_TARGETDEPS += D:/qtDemo/ui/QtMaterialWidgets/SDK/MaterialSDK/staticlib/libcomponents.a

//subdirs模板告诉qmake生成一个makefile,它可以进入到特定子目录并为这个目录中的项目文件生成makefile并且为它调用make。
SUBDIRS += \
    SDK/MaterialSDK/components/components.pro

注意:这是通过qmake语句导入的Material的SDK,不同于cmake

做完以上操作后即可,使用Material组件库,测试代码如下:

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>

QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE

class Widget : public QWidget
{
    Q_OBJECT

public:
    Widget(QWidget *parent = nullptr);
    ~Widget();

private:
    Ui::Widget *ui;
};
#endif // WIDGET_H
#include "widget.h"
#include "ui_widget.h"
#include<qtmaterialtoggle.h>
#include<qtmaterialflatbutton.h>
#include<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;
}

测试结果如下:
在这里插入图片描述

### 如何美化 Qt Designer 界面 #### 使用风格表单 (Style Sheets) Qt 支持 CSS 类似的样式表来定制应用程序的外观。这同样适用于通过 Qt Designer 创建的界面。可以通过 `setStyleSheet` 方法应用全局或局部的样式。 ```cpp QString styleSheet = "QPushButton { background-color: red; color: white; }"; QWidget *widget = new QWidget; widget->setStyleSheet(styleSheet); ``` 这种方式允许改变按钮的颜色、字体大小和其他视觉属性[^1]。 #### 更改主题颜色方案 对于希望更改整个应用程序的主题颜色方案的情况,可以在主窗口类中设置全局样式表。例如: ```cpp QApplication::setStyleSheet("QWidget {background-color: #E0F8FF}"); ``` 此命令会将所有小部件背景设为浅蓝色。也可以针对特定的小部件类型指定更详细的规则[^2]。 #### 利用第三方库增强美观度 除了官方提供的功能外,还有许多开源项目可以帮助提升 GUI 的美感。比如 QDarkStyle 提供了一套深色模式下的样式文件;Material Widgets 实现了 Google Material Design 风格的设计组件等[^3]。 #### 自定义图标集 为了使程序更加吸引人,在界面上适当位置加入高质量图标的使用是非常重要的一步。可以从网上下载免费或者付费的专业图标包,并将其集成到自己的项目当中去。确保遵循版权协议并正确引用作者信息。 #### 寻找在线资源和支持社区 当遇到具体问题时,访问 Stack Overflow 或者其他技术论坛寻求帮助往往能获得有效的解决方案。此外,GitHub 上也有很多关于 Qt 和其扩展插件的好例子可供学习借鉴。
评论 22
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值