版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq78442761/article/details/81868494
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));
}