Qt在windows下和linux下使用WEB 由于在linux下没有QAxWidget,但为了保证之前的Windows程序可以使用的,就需要将这两个分开 qt创建。pro文件win32{QTcoreguiQTaxcontainer}else{QTcoreguiQTwebenginewidgets}greaterThan(QTMAJORVERSION,4):QTwidgetsCONFIGc11YoucanmakeyourcodefailtocompileifitusesdeprecatedAPIs。Inordertodoso,uncommentthefollowingline。DEFINESQTDISABLEDEPRECATEDBEFORE0x060000disablesalltheAPIsdeprecatedbeforeQt6。0。0SOURCESmain。cppmainwindow。cppHEADERSmainwindow。hFORMSmainwindow。uiDefaultrulesfordeployment。qnx:target。pathtmp{TARGET}binelse:unix:!android:target。pathopt{TARGET}bin!isEmpty(target。path):INSTALLStarget mainwindow。hifndefMAINWINDOWHdefineMAINWINDOWHincludeQMainWindowifdefined(QOSWIN32)includeelseincludeQWebEngineViewendifincludeQStackedLayoutQTBEGINNAMESPACEnamespaceUi{classMainWindow;}QTENDNAMESPACEclassMainWindow:publicQMainWindow{QOBJECTpublic:MainWindow(QWidgetparentnullptr);MainWindow();ifdefined(QOSWIN32)QAxWidgetaxWidget;elseQWebEngineViewpdev;QWebEngineViewpv;endifQStackedLayoutlayout;protected:virtualvoidresizeEvent(QResizeEventevent)override;private:Ui::MainWindowui;};endifMAINWINDOWH mainwindow。cppincludemainwindow。hincludeuimainwindow。hMainWindow::MainWindow(QWidgetparent):QMainWindow(parent),ui(newUi::MainWindow){uisetupUi(this);ifdefined(QOSWIN32)axWidgetnewQAxWidget();axWidgetsetControl(QString::fromUtf8({8856F961340A11D0A96B00C04FD705A2}));axWidgetsetObjectName(QString::fromUtf8(WEBBROWSER));设置控件的名称axWidgetsetFocusPolicy(Qt::StrongFocus);设置控件接收键盘焦点的方式:鼠标单击、Tab键axWidgetsetProperty(DisplayAlerts,false);不显示任何警告信息。axWidgetsetProperty(DisplayScrollBars,true);显示滚动条QStackedLayoutlayoutnewQStackedLayout(uiframe);uiframesetLayout(layout);layoutaddWidget(axWidget);axWidgetdynamicCall(Navigate(constQString),www。baidu。com);显示网页elseQWebEngineViewpdevnewQWebEngineView();pdevshow();pvnewQWebEngineView();pvsetGeometry(thisframeGeometry()。width()0。1,thisframeGeometry()。height()0。1,thisframeGeometry()。width()8,thisframeGeometry()。height()8);pvsetUrl(QUrl(http:www。baidu。com));pvshow();pvpage()setDevToolsPage(pdevpage());QStackedLayoutlayoutnewQStackedLayout(uiframe);uiframesetLayout(layout);layoutaddWidget(pv);endif}MainWindow::MainWindow(){deleteui;}voidMainWindow::resizeEvent(QResizeEventevent){随界面大小改变uiframesetGeometry(thisframeGeometry()。width()0。1,thisframeGeometry()。height()0。1,thisframeGeometry()。width()0。8,thisframeGeometry()。height()0。8);} 要在mianwindow。ui中添加一个QFrame 【领QT开发教程学习资料,点击下方链接莬费领取,先码住不迷路】 点击这里:链接