[Development] ActiveQt Events

Henry Skoglund fromqt at tungware.se
Tue Jun 27 13:56:00 CEST 2017


On 2017-06-27 10:54, neel patel wrote:
> Hi All,
> 
> As I am using "QAxWidget". I took the "webbrowser" example as reference.
> As I am using "QTabWidget" as mainwindow widget and adding "QAxWidget" 
> as tab widget. Below is the code for reference.
> 
> 
>      m_tabWidget = new QTabWidget(mainwindow);
> 
>      m_mainGridLayout = new QGridLayout(m_tabWidget);
>      m_mainGridLayout->setContentsMargins(0, 0, 0, 0);
> 
>      m_MainTab = new QWidget(m_tabWidget);
>      m_tabGridLayout = new QGridLayout(m_MainTab);
>      m_tabGridLayout->setContentsMargins(0, 0, 0, 0);
> 
>      m_activeX = new WebAxWidget(m_MainTab);
>      
> m_activeX->setControl(QStringLiteral("{8856F961-340A-11D0-A96B-00C04FD705A2}"));
>      m_activeX->setObjectName(QStringLiteral("WebBrowser"));
>      m_activeX->setFocusPolicy(Qt::StrongFocus);
> 
>      m_tabGridLayout->addWidget(m_activeX, 0, 0, 1, 1);
>      m_tabWidget->addTab(m_MainTab, QString());
> 
>      mainwindow->setCentralWidget(m_tabWidget);
> 
> class WebAxWidget : public QAxWidget
> {
> public:
> 
>      WebAxWidget(QWidget* parent = 0, Qt::WindowFlags f = 0)
>          : QAxWidget(parent, f)
>      {
>      }
> }
> 
> 
> 
> I want to receive events like NewWindow3, NewWindow2, BeforeNavigate2 
> with Qt. As such there is no signal available in "QAxWidget". How can we 
> receive those signals from windows COM interface ?
> 
> Let me know reference or pointers so that i can implement and suggest in 
> above code.
> 
> Thanks in Advance.

Hi, you can receive those events through the generic QAxBase::signal, 
see http://doc.qt.io/qt-5/qaxbase.html#signal
In it, you'll see the example code for receiving "BeforeNavigate2", and 
all the other event flavors you retrieve the same way.

So first, add the slot declaration to your MainWindow class:
...
private slots:
     void slot(const QString& name,int argc, void* argv);
...

then connect your m_activeX object to that signal, here is some sample 
code added after your " mainwindow->setCentralWidget(m_tabWidget);":

...
     connect(m_activeX,SIGNAL(signal(const 
QString&,int,void*)),this,SLOT(slot(const QString&,int,void*)));
//    connect(m_activeX,&QAxWidget::signal,this,&MainWindow::slot);  // 
this is better syntax but alas will *not* compile :-(

// setup a sample navigation
     QString url = "http://doc.qt.io/qt-5/qaxbase.html";
     m_activeX->dynamicCall("Navigate(const QString&)",url);
}

// here's how to receive all of them
// you'll have to check the name for the correct one, I'll give you 2 
for free
// just add the others you need...
// note: the example in QAxBase::signal is too old to compile correctly, 
(changed below)

// need this for the VARIANTARG chaps
#include "windows.h"

void MainWindow::slot(const QString &name, int argc, void *argv)
{
     VARIANTARG *params = (VARIANTARG*)argv;
     if (name.startsWith("BeforeNavigate2("))
     {
     // 
"BeforeNavigate2(IDispatch*,QVariant&,QVariant&,QVariant&,QVariant&,QVariant&,bool&)"
         IDispatch *pDisp = params[argc-1].pdispVal;
         VARIANTARG URL = *params[argc-2].pvarVal;
         VARIANTARG Flags = *params[argc-3].pvarVal;
         VARIANTARG TargetFrameName = *params[argc-4].pvarVal;
         VARIANTARG PostData = *params[argc-5].pvarVal;
         VARIANTARG Headers = *params[argc-6].pvarVal;
         VARIANT_BOOL *Cancel = params[argc-7].pboolVal;

         QString url = QString::fromStdWString(URL.bstrVal);
         qDebug() << "BeforeNavigate2, ur" << url;
     }

     if (name.startsWith("NewWindow3("))
     {
     // NewWindow3(IDispatch**,bool&,uint,QString,QString)"
         IDispatch *pDisp = params[argc-1].pdispVal;
         VARIANT_BOOL* Cancel = params[argc-2].pboolVal;
         uint Flags = params[argc-3].uintVal;
         BSTR URLContext = params[argc-4].bstrVal;
         BSTR URL = params[argc-5].bstrVal;

         QString urlContext = QString::fromStdWString(URLContext);
         QString url = QString::fromStdWString(URL);
         qDebug() << "NewWindow3, opening" << url << "from" << urlContext;
     }
}
...

Good luck! Rgrds Henry




More information about the Development mailing list