首页 > 其他分享 >Qt-Qt通过QDesktopServices操作外部资源

Qt-Qt通过QDesktopServices操作外部资源

时间:2022-09-23 18:01:49浏览次数:69  
标签:clicked Qt 外部 void pushButton QUrl QDesktopServices MainWindow

相关资料

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 += target
View 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_H
View 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

相关文章

  • Qt通过类名动态创建对象(反射机制)
    1 反射机制C#中支持反射机制而C++中不支持,基于QT的元对象系统,之前使用QT的反射机制创建属性表,现学习使用QT通过类名动态创建对象。反射机制的优点:1、反射提高了程序的......
  • Qt-Qt之QSS文件使用
    相关资料https://blog.csdn.net/github_55295425/article/details/126091163  原文地址https://download.csdn.net/download/zhujianqiangqq/86663074   CSDN代......
  • Qt-Qt在开发过程中提示“报错C1189 No Target Architecture”
    相关资料:https://blog.csdn.net/wcc27857285/article/details/85197877 问题现象:有个启动外部bat文件的工程,今天突然想再建个相同功能的工程。代码如“WinExec("D:/a.b......
  • qt 线程的创建和退出
    qt常用的线程创建有两种方法,一种是继承QThread,重写run函数,一种是创建一个业务处理类,moveToThread。我创建了一个widget工程,演示一下。示例地址:https://files-cdn.cnblogs......
  • phthon+qt5
    1.安装phcharm2.Pycharm打开Setiings->PythonInterpreter,选择Add:virtualenvEnvironment-->baseinterpreter:加入本地的phython路径3.安装QT5和工具包命令:pip......
  • 【代码片段】Qt6.2.4 C++ 执行CMD命令
    参考https://blog.csdn.net/vample/article/details/78872587https://blog.csdn.net/liuyukuan/article/details/5974517环境环境版本windows10QT6.2.......
  • 【代码片段】Qt6.2.4 C++ 获取目录与排除指定文件夹
    参考https://www.bilibili.com/video/BV14t411b7EL环境环境版本windows10QT6.2.4QtCreator8.0.1(Community)qmake代码QStringphpD......
  • qt的基本控件——按钮控件
    按钮控件一、button控件(按钮控件)1、button通过setText设置文字//在类中定义一个boolisDown;构造中初始化为false,拖入一个button,直接修改名字为连接设备,转到槽写上如......
  • 自定义组件中使用half-screen-dailog 无法舒勇外部样式
     为了将弹窗更方便的调用,封装了组件,但是发现一个问题,外部样式类在组件中是无法正常使用的。 也就说在组件的wxss中是无法正常使用.weui-half-screen-dialog这个外部......
  • QT 子控件 键盘事件及键盘事件无法响应
    解决办法:重新的子控件构造函数中加入:setFocusPolicy(Qt::StrongFocus);遇事多看帮助文档,Qt的帮助文档是写的最好的。帮助文档中描述如下:  setFocusPolicy()解释......