相关资料
https://download.csdn.net/download/zhujianqiangqq/86666124 CSDN代码下载
.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 += targetView Code
main.cpp
1 #include "mainwindow.h" 2 3 #include <QApplication> 4 5 int main(int argc, char *argv[]) 6 { 7 QApplication a(argc, argv); 8 MainWindow w; 9 w.show(); 10 return a.exec(); 11 }View Code
mainwindow.h
1 #ifndef MAINWINDOW_H 2 #define MAINWINDOW_H 3 4 #include <QMainWindow> 5 6 #include "QDesktopServices" 7 #include "QUrl" 8 #include "QMessageBox" 9 10 QT_BEGIN_NAMESPACE 11 namespace Ui { class MainWindow; } 12 QT_END_NAMESPACE 13 14 class MyHelpHandler : public QObject 15 { 16 Q_OBJECT 17 public: 18 19 public slots: 20 void showHelp(const QUrl &url) 21 { 22 QMessageBox mgsBox; 23 mgsBox.setText(url.toString()); 24 mgsBox.exec(); 25 } 26 }; 27 28 class MainWindow : public QMainWindow 29 { 30 Q_OBJECT 31 32 public: 33 MainWindow(QWidget *parent = nullptr); 34 ~MainWindow(); 35 36 private slots: 37 void on_pushButton_clicked(); 38 39 void on_pushButton_2_clicked(); 40 41 void on_pushButton_4_clicked(); 42 43 void on_pushButton_5_clicked(); 44 45 void on_pushButton_6_clicked(); 46 47 void on_pushButton_3_clicked(); 48 49 void on_pushButton_7_clicked(); 50 51 void on_pushButton_8_clicked(); 52 53 void on_pushButton_9_clicked(); 54 55 void on_pushButton_10_clicked(); 56 57 void on_pushButton_14_clicked(); 58 59 void on_pushButton_11_clicked(); 60 61 private: 62 Ui::MainWindow *ui; 63 64 MyHelpHandler handler; 65 }; 66 #endif // MAINWINDOW_HView Code
mainwindow.cpp
1 #include "mainwindow.h" 2 #include "ui_mainwindow.h" 3 4 MainWindow::MainWindow(QWidget *parent) 5 : QMainWindow(parent) 6 , ui(new Ui::MainWindow) 7 { 8 ui->setupUi(this); 9 10 setWindowTitle(QStringLiteral("Qt通过QDesktopServices操作外部资源")); 11 } 12 13 MainWindow::~MainWindow() 14 { 15 delete ui; 16 } 17 18 // 在资源管理器中打开某个文件夹 19 void MainWindow::on_pushButton_clicked() 20 { 21 QDesktopServices::openUrl(QUrl("file:///C:/Program Files", QUrl::TolerantMode)); 22 } 23 24 // 调用默认浏览器打开一个网页链接 25 void MainWindow::on_pushButton_2_clicked() 26 { 27 QDesktopServices::openUrl(QUrl("https://www.baidu.com/", QUrl::TolerantMode)); 28 } 29 30 void MainWindow::on_pushButton_4_clicked() 31 { 32 QDesktopServices::setUrlHandler("help", &handler, "showHelp"); 33 } 34 35 void MainWindow::on_pushButton_5_clicked() 36 { 37 QDesktopServices::unsetUrlHandler("help"); 38 } 39 40 void MainWindow::on_pushButton_6_clicked() 41 { 42 QDesktopServices::openUrl(QUrl("help://showthisword/", QUrl::TolerantMode)); 43 } 44 45 void MainWindow::on_pushButton_3_clicked() 46 { 47 QDesktopServices::openUrl(QUrl::fromLocalFile("C:/Program Files")); 48 } 49 50 void MainWindow::on_pushButton_7_clicked() 51 { 52 QDesktopServices::openUrl(QUrl("file:///D:/A.pdf", QUrl::TolerantMode)); 53 } 54 55 void MainWindow::on_pushButton_8_clicked() 56 { 57 QDesktopServices::openUrl(QUrl("file:///D:/B.docx", QUrl::TolerantMode)); 58 } 59 60 void MainWindow::on_pushButton_9_clicked() 61 { 62 QString strUrl = QString("mailto:%1?subject=%2&body=%3").arg("[email protected]").arg("Qt").arg("I am a Qter"); 63 QDesktopServices::openUrl(QUrl(strUrl)); 64 } 65 66 void MainWindow::on_pushButton_10_clicked() 67 { 68 QDesktopServices::openUrl(QUrl("file:///D:/RuanJian/BCompareLite/BCompareLite.exe", QUrl::TolerantMode)); 69 } 70 71 void MainWindow::on_pushButton_14_clicked() 72 { 73 // QDesktopServices::storageLocation(QDesktopServices::DesktopLocation) 74 // QDesktopServices::storageLocation(QDesktopServices::DocumentsLocation) 75 // QDesktopServices::storageLocation(QDesktopServices::HomeLocation) 76 // QDesktopServices::storageLocation(QDesktopServices::ApplicationsLocation) 77 // QDesktopServices::storageLocation(QDesktopServices::TempLocation) 78 79 // Qt4使用 QDesktopServices::storageLocation(QDesktopServices::xxxx) 来获取一些系统目录, 80 // Qt5则要改成QStandardPaths::writableLocation(QStandardPaths::xxxx)。 81 } 82 83 void MainWindow::on_pushButton_11_clicked() 84 { 85 QDesktopServices::openUrl(QUrl("file:///\\127.0.0.1")); 86 }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>816</width> 10 <height>359</height> 11 </rect> 12 </property> 13 <property name="windowTitle"> 14 <string>MainWindow</string> 15 </property> 16 <widget class="QWidget" name="centralwidget"> 17 <layout class="QHBoxLayout" name="horizontalLayout"> 18 <item> 19 <layout class="QVBoxLayout" name="verticalLayout"> 20 <item> 21 <layout class="QHBoxLayout" name="horizontalLayout_4"> 22 <item> 23 <widget class="QPushButton" name="pushButton"> 24 <property name="text"> 25 <string>打开资源管理器方式1</string> 26 </property> 27 </widget> 28 </item> 29 <item> 30 <widget class="QPushButton" name="pushButton_3"> 31 <property name="text"> 32 <string>打开资源管理器方式2</string> 33 </property> 34 </widget> 35 </item> 36 <item> 37 <widget class="QPushButton" name="pushButton_2"> 38 <property name="text"> 39 <string>默认浏览器打开网页</string> 40 </property> 41 </widget> 42 </item> 43 <item> 44 <widget class="QPushButton" name="pushButton_7"> 45 <property name="text"> 46 <string>打开PDF文件</string> 47 </property> 48 </widget> 49 </item> 50 <item> 51 <widget class="QPushButton" name="pushButton_8"> 52 <property name="text"> 53 <string>打开DOC文件</string> 54 </property> 55 </widget> 56 </item> 57 </layout> 58 </item> 59 <item> 60 <layout class="QHBoxLayout" name="horizontalLayout_2"> 61 <item> 62 <widget class="QPushButton" name="pushButton_4"> 63 <property name="text"> 64 <string>自定义QDesktopServices::openUrl的行为</string> 65 </property> 66 </widget> 67 </item> 68 <item> 69 <widget class="QPushButton" name="pushButton_5"> 70 <property name="text"> 71 <string>取消自定义的openUrl行为</string> 72 </property> 73 </widget> 74 </item> 75 <item> 76 <widget class="QPushButton" name="pushButton_6"> 77 <property name="text"> 78 <string>测试自定义行为</string> 79 </property> 80 </widget> 81 </item> 82 </layout> 83 </item> 84 <item> 85 <layout class="QHBoxLayout" name="horizontalLayout_3"> 86 <item> 87 <widget class="QPushButton" name="pushButton_9"> 88 <property name="text"> 89 <string>打开邮件</string> 90 </property> 91 </widget> 92 </item> 93 <item> 94 <widget class="QPushButton" name="pushButton_10"> 95 <property name="text"> 96 <string>启动程序</string> 97 </property> 98 </widget> 99 </item> 100 <item> 101 <widget class="QPushButton" name="pushButton_11"> 102 <property name="text"> 103 <string>打开网络共享</string> 104 </property> 105 </widget> 106 </item> 107 <item> 108 <widget class="QPushButton" name="pushButton_12"> 109 <property name="text"> 110 <string>PushButton</string> 111 </property> 112 </widget> 113 </item> 114 </layout> 115 </item> 116 <item> 117 <layout class="QHBoxLayout" name="horizontalLayout_5"> 118 <item> 119 <widget class="QPushButton" name="pushButton_14"> 120 <property name="text"> 121 <string>Qt5弃用的方法</string> 122 </property> 123 </widget> 124 </item> 125 </layout> 126 </item> 127 </layout> 128 </item> 129 </layout> 130 </widget> 131 </widget> 132 <resources/> 133 <connections/> 134 </ui>View Code
搜索
复制
<iframe height="240" width="320"></iframe> 标签:clicked,Qt,外部,void,pushButton,QUrl,QDesktopServices,MainWindow From: https://www.cnblogs.com/FKdelphi/p/16723626.html