qt 使用html5 | 您所在的位置:网站首页 › qt打开html文件 › qt 使用html5 |
1)首先新建一个qt工程。在pro文件里面添加 webkitwidgets 和 network模块 QT += core gui webkitwidgets network greaterThan(QT_MAJOR_VERSION, 4): QT += widgets TARGET = html1 TEMPLATE = app SOURCES += main.cpp\ mainwindow.cpp HEADERS += mainwindow.h FORMS += mainwindow.ui 2)在MainWindow类中添加一个成员。该指针指向一个QWebView类: #ifndef MAINWINDOW_H #define MAINWINDOW_H #include #include namespace Ui { class MainWindow; } class MainWindow : public QMainWindow { Q_OBJECT public: explicit MainWindow(QWidget *parent = 0); ~MainWindow(); QWebView *mp_HTML; virtual void resizeEvent(QResizeEvent *); private: Ui::MainWindow *ui; }; #endif // MAINWINDOW_H 3)在路径E:\Qt\build-html1-Desktop_Qt_5_4_1_MinGW_32bit-Debug\debug里面创建文件new1.html: hello 4)在mainwindow.cpp文件中将html文件和mp_HTML关联起来。注意,路径前要写明file:/// #include "mainwindow.h" #include "ui_mainwindow.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); mp_HTML = new QWebView(this); mp_HTML->show(); mp_HTML->load(QUrl("file:///E:/Qt/build-html1-Desktop_Qt_5_4_1_MinGW_32bit-Debug/debug/new1.html")); } MainWindow::~MainWindow() { delete mp_HTML; delete ui; } void MainWindow::resizeEvent(QResizeEvent *e)//这个函数用途是让QWebView的大小随着整个窗体缩放而变大或者变小 { int w, h; w = width(); h = height(); mp_HTML->setGeometry(w / 5, h / 5, w * 3 / 5, h * 3 / 5); QMainWindow::resizeEvent(e); } 结果: 参考文章: http://www.cnblogs.com/Romi/archive/2012/05/03/2480387.html |
CopyRight 2018-2019 实验室设备网 版权所有 |