首页 > 其他分享 >Qt-Qt实现高斯模糊效果

Qt-Qt实现高斯模糊效果

时间:2023-02-09 21:24:22浏览次数:32  
标签:10 Qt 模糊 mainwindow ui include MainWindow 高斯

相关资料:

https://qa.1r1g.com/sf/ask/1044059061/

实例代码:

.pro

 1 QT       += core gui
 2 
 3 greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
 4 
 5 CONFIG += c++11
 6 
 7 # The following define makes your compiler emit warnings if you use
 8 # any Qt feature that has been marked deprecated (the exact warnings
 9 # depend on your compiler). Please consult the documentation of the
10 # deprecated API in order to know how to port your code away from it.
11 DEFINES += QT_DEPRECATED_WARNINGS
12 
13 # You can also make your code fail to compile if it uses deprecated APIs.
14 # In order to do so, uncomment the following line.
15 # You can also select to disable deprecated APIs only up to a certain version of Qt.
16 #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
17 
18 SOURCES += \
19     main.cpp \
20     mainwindow.cpp
21 
22 HEADERS += \
23     mainwindow.h
24 
25 FORMS += \
26     mainwindow.ui
27 
28 # Default rules for deployment.
29 qnx: target.path = /tmp/$${TARGET}/bin
30 else: unix:!android: target.path = /opt/$${TARGET}/bin
31 !isEmpty(target.path): INSTALLS += target
32 
33 RESOURCES += \
34     aa.qrc
View Code

main.cpp

 1 #include "mainwindow.h"
 2 
 3 #include <QApplication>
 4 #include <QtCore>
 5 #include <QtGui>
 6 #include <QLabel>
 7 #include <QBitmap>
 8 
 9 int main(int argc, char *argv[])
10 {
11     QApplication a(argc, argv);
12     MainWindow w;
13     w.show();
14     return a.exec();
15 }
View Code

mainwindow.h

 1 #ifndef MAINWINDOW_H
 2 #define MAINWINDOW_H
 3 
 4 #include <QMainWindow>
 5 
 6 QT_BEGIN_NAMESPACE
 7 namespace Ui { class MainWindow; }
 8 QT_END_NAMESPACE
 9 
10 class MainWindow : public QMainWindow
11 {
12     Q_OBJECT
13 
14 public:
15     MainWindow(QWidget *parent = nullptr);
16     ~MainWindow();
17 
18 private slots:
19     void on_horizontalSlider_valueChanged(int value);
20     void on_horizontalSlider_2_valueChanged(int value);
21 
22 private:
23     Ui::MainWindow *ui;
24 };
25 #endif // MAINWINDOW_H
View Code

mainwindow.cpp

 1 #include "mainwindow.h"
 2 #include "ui_mainwindow.h"
 3 #include <QGraphicsDropShadowEffect>
 4 #include <QGraphicsScene>
 5 #include <QPainter>
 6 #include <QGraphicsPixmapItem>
 7 
 8 // 源作者地址:https://qa.1r1g.com/sf/ask/1044059061/
 9 QImage applyEffectToImage(QImage src, QGraphicsEffect *effect, int extent=0)
10 {
11     if(src.isNull()) return QImage();   //No need to do anything else!
12     if(!effect) return src;             //No need to do anything else!
13     QGraphicsScene scene;
14     QGraphicsPixmapItem item;
15     item.setPixmap(QPixmap::fromImage(src));
16     item.setGraphicsEffect(effect);
17     scene.addItem(&item);
18     QImage res(src.size()+QSize(extent*2, extent*2), QImage::Format_ARGB32);
19     res.fill(Qt::transparent);
20     QPainter ptr(&res);
21     scene.render(&ptr, QRectF(), QRectF( -extent, -extent, src.width()+extent*2, src.height()+extent*2 ) );
22     return res;
23 }
24 
25 MainWindow::MainWindow(QWidget *parent)
26     : QMainWindow(parent)
27     , ui(new Ui::MainWindow)
28 {
29     ui->setupUi(this);
30     setWindowTitle(QStringLiteral("Qt实现高斯模糊效果"));
31 }
32 
33 MainWindow::~MainWindow()
34 {
35     delete ui;
36 }
37 
38 void MainWindow::on_horizontalSlider_valueChanged(int value)
39 {
40     QGraphicsBlurEffect *blur = new QGraphicsBlurEffect;
41     blur->setBlurRadius(ui->horizontalSlider->value());
42     QImage source(":/new/prefix1/a.jpeg");
43     QImage result = applyEffectToImage(source, blur);
44 
45     ui->label_2->setPixmap(QPixmap::fromImage(result));
46 //    result.save("d:\\2.bmp");
47 //    ui->widget->grab(QRect(0,0,400,200)).save("d:\\1.bmp");
48 }
49 
50 void MainWindow::on_horizontalSlider_2_valueChanged(int value)
51 {
52     QGraphicsBlurEffect *e0 = new QGraphicsBlurEffect(this);
53     e0->setBlurRadius(value);
54     ui->widget->setGraphicsEffect(e0);
55 }
View Code

mainwindow.ui

  1 <?xml version="1.0" encoding="UTF-8"?>
  2 <ui version="4.0">
  3  <class>MainWindow</class>
  4  <widget class="QMainWindow" name="MainWindow">
  5   <property name="geometry">
  6    <rect>
  7     <x>0</x>
  8     <y>0</y>
  9     <width>831</width>
 10     <height>376</height>
 11    </rect>
 12   </property>
 13   <property name="windowTitle">
 14    <string>MainWindow</string>
 15   </property>
 16   <widget class="QWidget" name="centralwidget">
 17    <widget class="QWidget" name="widget" native="true">
 18     <property name="geometry">
 19      <rect>
 20       <x>0</x>
 21       <y>10</y>
 22       <width>400</width>
 23       <height>300</height>
 24      </rect>
 25     </property>
 26     <layout class="QHBoxLayout" name="horizontalLayout">
 27      <property name="spacing">
 28       <number>0</number>
 29      </property>
 30      <property name="leftMargin">
 31       <number>0</number>
 32      </property>
 33      <property name="topMargin">
 34       <number>0</number>
 35      </property>
 36      <property name="rightMargin">
 37       <number>0</number>
 38      </property>
 39      <property name="bottomMargin">
 40       <number>0</number>
 41      </property>
 42      <item>
 43       <widget class="QLabel" name="label">
 44        <property name="text">
 45         <string/>
 46        </property>
 47        <property name="pixmap">
 48         <pixmap resource="aa.qrc">:/new/prefix1/a.jpeg</pixmap>
 49        </property>
 50        <property name="scaledContents">
 51         <bool>true</bool>
 52        </property>
 53        <property name="wordWrap">
 54         <bool>false</bool>
 55        </property>
 56       </widget>
 57      </item>
 58     </layout>
 59    </widget>
 60    <widget class="QSlider" name="horizontalSlider">
 61     <property name="geometry">
 62      <rect>
 63       <x>110</x>
 64       <y>330</y>
 65       <width>701</width>
 66       <height>20</height>
 67      </rect>
 68     </property>
 69     <property name="minimum">
 70      <number>1</number>
 71     </property>
 72     <property name="maximum">
 73      <number>200</number>
 74     </property>
 75     <property name="orientation">
 76      <enum>Qt::Horizontal</enum>
 77     </property>
 78    </widget>
 79    <widget class="QLabel" name="label_2">
 80     <property name="geometry">
 81      <rect>
 82       <x>410</x>
 83       <y>10</y>
 84       <width>400</width>
 85       <height>300</height>
 86      </rect>
 87     </property>
 88     <property name="text">
 89      <string>TextLabel</string>
 90     </property>
 91    </widget>
 92    <widget class="QLabel" name="label_3">
 93     <property name="geometry">
 94      <rect>
 95       <x>10</x>
 96       <y>330</y>
 97       <width>91</width>
 98       <height>16</height>
 99      </rect>
100     </property>
101     <property name="text">
102      <string>截取显示图片</string>
103     </property>
104    </widget>
105    <widget class="QLabel" name="label_4">
106     <property name="geometry">
107      <rect>
108       <x>10</x>
109       <y>350</y>
110       <width>91</width>
111       <height>16</height>
112      </rect>
113     </property>
114     <property name="text">
115      <string>直接使用模糊</string>
116     </property>
117    </widget>
118    <widget class="QSlider" name="horizontalSlider_2">
119     <property name="geometry">
120      <rect>
121       <x>110</x>
122       <y>350</y>
123       <width>701</width>
124       <height>20</height>
125      </rect>
126     </property>
127     <property name="minimum">
128      <number>1</number>
129     </property>
130     <property name="maximum">
131      <number>200</number>
132     </property>
133     <property name="orientation">
134      <enum>Qt::Horizontal</enum>
135     </property>
136    </widget>
137   </widget>
138  </widget>
139  <resources>
140   <include location="aa.qrc"/>
141  </resources>
142  <connections/>
143 </ui>
View Code

 

搜索

复制

<iframe></iframe>

标签:10,Qt,模糊,mainwindow,ui,include,MainWindow,高斯
From: https://www.cnblogs.com/FKdelphi/p/17107070.html

相关文章

  • python mqtt服务器搭建
    一.在Linux中搭建mqtt服务环境:Linux版本Ubuntu 18.04.1 LTS1.进入https://www.emqx.com/zh/try?product=broker下载开源版本 EMQX 此处选择zip格式2.下载后将e......
  • 基于QT实现的影院订票系统[2023-02-09]
    基于QT实现的影院订票系统[2023-02-09](1)订票模块:用户选择影院影片和场次,进行选座购票的操作等。(2)用户模块:用户的登录,修改信息等。(3)影院管理模块:影院负责人对自己影院的......
  • 基于QT的校园共享平台的设计与实现[2023-02-09]
    基于QT的校园共享平台的设计与实现[2023-02-09]随着经济的迅速发展和科技日新月异的进步,产品更新换代速度越来越快,物价水平持续高升。大学生作为消费者,但其消费水平不高,消......
  • Qt多线程编程之QThread
    背景引言[GUI主线程+子线程]跟C++11中很像的是,Qt中使用QThread来管理线程,一个QThread对象管理一个线程,在使用上有很多跟C++11中相似的地方,但更多的是Qt中独有的内容......
  • QTcpSocket 设置接收数据延时等待时间
    /*客户端接入槽函数*/voidTcpServer::slotNewConnect(void){/*获取连接的客户端句柄这里设置刷新数据时间1ms*/QTcpSocket*pSocket=this->m_pServer->ne......
  • Qt多线程编程之QThreadPool 和 QRunnable使用
     说到线程通常会想到QThread,但其实Qt中创建线程的方式有多种,这里主要介绍其中一种QRunnable,QRunnable和QThread用法有些不同,并且使用场景也有区别。要介绍QRunnable的用......
  • PyQt5-快速上手笔记-03
    盒布局fromPyQt5.QtWidgetsimportQWidget,QPushButtonfromPyQt5.QtWidgetsimportQHBoxLayout,QVBoxLayoutclassmyWidget(QWidget):def__init__(self):......
  • QT Dom修改xml
    QFilefile("Init.xml");if(!file.open(QFile::ReadOnly))return;QDomDocumentdoc;if(!doc.setContent(&file)){file.close();re......
  • QT comboBox简单使用
    拖一个控件双击然后就点加号生成下拉菜单connect(ui->comboBox,SIGNAL(currentIndexChanged(int)),this,SLOT(deal(int)));这样就可以接收你选择的菜单信号了你可以把初始......
  • PyQt5-快速上手笔记-02
    状态栏状态栏是用来显示应用状态信息的组件fromPyQt5.QtWidgetsimportQMainWindowclassmyWidget(QMainWindow):def__init__(self):super().__init_......