<?xml version="1.0" encoding="iso-8859-1"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <!-- webbrowser.qdoc --> <head> <title>Qt 4.6: Web Browser Example (ActiveQt)</title> <link href="classic.css" rel="stylesheet" type="text/css" /> </head> <body> <table border="0" cellpadding="0" cellspacing="0" width="100%"> <tr> <td align="left" valign="top" width="32"><a href="http://qt.nokia.com/"><img src="images/qt-logo.png" align="left" border="0" /></a></td> <td width="1"> </td><td class="postheader" valign="center"><a href="index.html"><font color="#004faf">Home</font></a> · <a href="classes.html"><font color="#004faf">All Classes</font></a> · <a href="functions.html"><font color="#004faf">All Functions</font></a> · <a href="overviews.html"><font color="#004faf">Overviews</font></a></td></tr></table><h1 class="title">Web Browser Example (ActiveQt)<br /><span class="subtitle"></span> </h1> <p>Files:</p> <ul> <li><a href="activeqt-webbrowser-mainwindow-ui.html">activeqt/webbrowser/mainwindow.ui</a></li> <li><a href="activeqt-webbrowser-mainwindow-windowsmobile-ui.html">activeqt/webbrowser/mainwindow_windowsmobile.ui</a></li> <li><a href="activeqt-webbrowser-webaxwidget-h.html">activeqt/webbrowser/webaxwidget.h</a></li> <li><a href="activeqt-webbrowser-main-cpp.html">activeqt/webbrowser/main.cpp</a></li> <li><a href="activeqt-webbrowser-webbrowser-pro.html">activeqt/webbrowser/webbrowser.pro</a></li> </ul> <p>The Web Browser example uses the Microsoft Web Browser ActiveX control to implement a fully functional Web Browser application. The user interface has been developed using the Qt Designer integration of the <a href="qaxwidget.html">QAxWidget</a> class.</p> <p>The code demonstrates how the Qt application can communicate with the embedded ActiveX controls using signals, slots and the dynamicCall() function.</p> <pre> class MainWindow : public QMainWindow, public Ui::MainWindow { Q_OBJECT public: MainWindow(); public slots: void on_WebBrowser_TitleChange(const QString &title); void on_WebBrowser_ProgressChange(int a, int b); void on_WebBrowser_CommandStateChange(int cmd, bool on); void on_WebBrowser_BeforeNavigate(); void on_WebBrowser_NavigateComplete(QString); void on_actionGo_triggered(); void on_actionNewWindow_triggered(); void on_actionAbout_triggered(); void on_actionAboutQt_triggered(); void on_actionFileClose_triggered(); private: QProgressBar *pb; };</pre> <p>The <tt>MainWindow</tt> class declares a <tt>QMainWindow</tt> based user interface, using the <tt>Ui::MainWindow</tt> class generated by Qt Designer. A number of slots are implemented to handle events from the various user interface elements, including the <tt>WebBrowser</tt> object, which is a <a href="qaxwidget.html">QAxWidget</a> hosting the Microsoft Web Browser control.</p> <pre> MainWindow::MainWindow() { setupUi(this); connect(addressEdit, SIGNAL(returnPressed()), actionGo, SLOT(trigger())); connect(actionBack, SIGNAL(triggered()), WebBrowser, SLOT(GoBack())); connect(actionForward, SIGNAL(triggered()), WebBrowser, SLOT(GoForward())); connect(actionStop, SIGNAL(triggered()), WebBrowser, SLOT(Stop())); connect(actionRefresh, SIGNAL(triggered()), WebBrowser, SLOT(Refresh())); connect(actionHome, SIGNAL(triggered()), WebBrowser, SLOT(GoHome())); connect(actionSearch, SIGNAL(triggered()), WebBrowser, SLOT(GoSearch())); pb = new QProgressBar(statusBar()); pb->setTextVisible(false); pb->hide(); statusBar()->addPermanentWidget(pb); WebBrowser->dynamicCall("GoHome()"); }</pre> <p>The constructor initializes the user interface, installs a progress bar on the status bar, and uses <a href="qaxbase.html#dynamicCall">QAxBase::dynamicCall</a>() to invoke the <tt>GoHome()</tt> method of Internet Explorer to navigate to the user's home page.</p> <pre> void MainWindow::on_WebBrowser_TitleChange(const QString &title) { setWindowTitle("Qt WebBrowser - " + title); } void MainWindow::on_WebBrowser_ProgressChange(int a, int b) { if (a <= 0 || b <= 0) { pb->hide(); return; } pb->show(); pb->setRange(0, b); pb->setValue(a); } void MainWindow::on_WebBrowser_CommandStateChange(int cmd, bool on) { switch (cmd) { case 1: actionForward->setEnabled(on); break; case 2: actionBack->setEnabled(on); break; } } void MainWindow::on_WebBrowser_BeforeNavigate() { actionStop->setEnabled(true); } void MainWindow::on_WebBrowser_NavigateComplete(QString) { actionStop->setEnabled(false); }</pre> <p>Different slots handle the signals emitted by the WebBrowser object.</p> <p>Connections that don't require any coding, i.e. connecting the <tt>back</tt> action to the <tt>GoBack()</tt> slot, have already been made in Qt Designer.</p> <pre> void MainWindow::on_actionGo_triggered() { WebBrowser->dynamicCall("Navigate(const QString&)", addressEdit->text()); } void MainWindow::on_actionNewWindow_triggered() { MainWindow *window = new MainWindow; window->show(); if (addressEdit->text().isEmpty()) return; window->addressEdit->setText(addressEdit->text()); window->actionStop->setEnabled(true); window->on_actionGo_triggered(); } void MainWindow::on_actionAbout_triggered() { QMessageBox::about(this, tr("About WebBrowser"), tr("This Example has been created using the ActiveQt integration into Qt Designer.\n" "It demonstrates the use of QAxWidget to embed the Internet Explorer ActiveX\n" "control into a Qt application.")); } void MainWindow::on_actionAboutQt_triggered() { QMessageBox::aboutQt(this, tr("About Qt")); } void MainWindow::on_actionFileClose_triggered() { close(); } #include "main.moc" int main(int argc, char ** argv) { QApplication a(argc, argv); MainWindow w; #if defined(Q_OS_WINCE) w.showMaximized(); #else w.show(); #endif return a.exec(); }</pre> <p>The rest of the implementation is not related to <a href="activeqt.html#activeqt">ActiveQt</a> - the actions are handled by different slots, and the entry point function starts the application using standard Qt APIs.</p> <p>To build the example you must first build the <a href="qaxcontainer.html">QAxContainer</a> library. Then run your make tool in <tt>examples/activeqt/webbrowser</tt> and run the resulting <tt>webbrowser.exe</tt>.</p> <p /><address><hr /><div align="center"> <table width="100%" cellspacing="0" border="0"><tr class="address"> <td width="40%" align="left">Copyright © 2010 Nokia Corporation and/or its subsidiary(-ies)</td> <td width="20%" align="center"><a href="trademarks.html">Trademarks</a></td> <td width="40%" align="right"><div align="right">Qt 4.6.3</div></td> </tr></table></div></address></body> </html>