相关资料:
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.qrcView 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_HView 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