Qt工作筆記-進入資料夾或開啟網站(QDesktopServices::openUrl的使用)
阿新 • • 發佈:2019-01-29
QDesktopServices::openUrl這個是個神器,通過URL可以開啟本地的資料夾或某一個web網站
還是截張圖把:
widget.h
#ifndef WIDGET_H #define WIDGET_H #include <QWidget> namespace Ui { class Widget; } class Widget : public QWidget { Q_OBJECT public: explicit Widget(QWidget *parent = 0); ~Widget(); public slots: void openFileBtnClicked(); void openWebBtnClicked(); protected: void OpenUrl(QString str); private: Ui::Widget *ui; }; #endif // WIDGET_H
main.cpp
#include "widget.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}
widget.cpp
#include "widget.h" #include "ui_widget.h" #include <QDebug> #include <QUrl> #include <QDesktopServices> Widget::Widget(QWidget *parent) : QWidget(parent), ui(new Ui::Widget) { ui->setupUi(this); connect(ui->openFilePpushButton,SIGNAL(clicked(bool)),this,SLOT(openFileBtnClicked())); connect(ui->openWebPushButton,SIGNAL(clicked(bool)),this,SLOT(openWebBtnClicked())); } Widget::~Widget() { delete ui; } void Widget::openFileBtnClicked() { //open the disk "C" //OpenUrl("C:/"); you can write it in this way or in the following way OpenUrl("file:///C:/"); } void Widget::openWebBtnClicked() { OpenUrl("https://www.baidu.com"); } void Widget::OpenUrl(QString str) { QDesktopServices::openUrl(QUrl(str)); }