QT application programming: write activex control in QtCreate to run on the web page (stand-alone exe form)

1. Environmental introduction

Operating system:  win10 64 bit

QT version:  5.12.6

Visual Studio IDE version:   2017

Two, ActiveX control introduction

ActiveX controls are part of Microsoft's ActiveX technology.

ActiveX controls are program objects that can be reused on computers in applications and networks. The main technology to create it is Microsoft's ActiveX technology, which is mainly the Component Object Model (COM).

ActiveX controls can be downloaded into web pages as small programs, and can also be used in general Windows and Macintosh application environments. Generally speaking, ActiveX control replaces the original OCX, which is similar in concept and function to JAVA applet. ActiveX controls can be developed by different languages ​​that can recognize Microsoft's COM technology. It is a component or a self-contained software package that can be developed or used in the same or distributed computing environment.

There are two forms of ActiveX control operation:

(1) The exe format that can be run independently
(2) The plug-in dll format is   
here: 
https://blog.csdn.net/xiaolong1126626497/article/details/112556866

This article is about the exe form that can be run independently.

QT's help document has a detailed introduction to the use of ActiveX:

Three, build a development environment

QT program to generate ActiveX plug-in requires MSVC compiler.

VS2017 and QT5.12.6 environment construction can refer to here:  https://blog.csdn.net/xiaolong1126626497/article/details/112402861

Four, new construction

Open QtCreate to create a simple test project.

Drag a calendar control on the UI interface to facilitate testing.

Five, configure the project to generate ActiveX plug-ins

Next, open the xxx.pro project file and add the following code:

TEMPLATE = app
QT  += axserver
VERSION = 1.1

 If you are generating a dll file, you need to configure it like this:

TEMPLATE = lib
QT += axserver
CONFIG  += dll
DEF_FILE = PlayerPlugin.def   #这个文件可以从VS里得到,VS建工程会自动生成
RC_FILE  = playerplugin.rc
#VERSION = 2


然后声明ID:   main.cpp可以不要,不需要main函数。
QAXFACTORY_DEFAULT(CPlayerPlugin,
                   "{A5071BCA-52E6-4685-9FBA-7554003FC42D}",
                   "{854705F4-D8DF-4768-8A02-407BD22DA47F}",
                   "{B80C8AF2-7BF7-4839-8AC8-3DAED7AE8A8D}",
                   "{1829EFE3-6E75-4B68-9287-A9D64F9115C7}",
                   "{5BC124D6-9FF9-438A-AD13-30426B91607E}")

Next, add the Q_CLASSINFO() macro to the newly created class to provide additional information for the class. To implement QT's COM or ActiveX, every class created that you want to export to COM must inherit from QObject or QWidget or their subclasses. The ones inherited from QObject and subclasses are COM plug-ins, and those inherited from QWidget and subclasses are ActiveX controls.

Q _CLASSINFO defines some information of COM components, where ClassID and InterfaceID are necessary. When events in COM need to be used, EventsID needs to be added.

The 128-bit character string in Q _CLASSINFO can be generated using the GUID.exe tool in the Tools menu of Visual Studio as long as the uniqueness is guaranteed.

#ifndef ACTIVE_TEST_H
#define ACTIVE_TEST_H

#include <QWidget>

QT_BEGIN_NAMESPACE
namespace Ui { class Active_Test; }
QT_END_NAMESPACE

class Active_Test : public QWidget
{
    Q_OBJECT

    Q_CLASSINFO("ClassID",     "{CB1DB4A6-D16D-4D20-AE0D-67F1DCF542C5}")
    Q_CLASSINFO("InterfaceID", "{C03C4B03-B376-4B95-9349-3A2D547D5905}")
    Q_CLASSINFO("EventsID",    "{59A44941-3EF7-443E-AFA1-0B2F7129B7A4}")

public:
    Active_Test(QWidget *parent = nullptr);
    ~Active_Test();

private:
    Ui::Active_Test *ui;
};
#endif // ACTIVE_TEST_H

 The above steps are just to create a COM class that can be exported, but if you want the client to apply to create this class object, the class must be created by the outside world. To achieve this, an instance of QAxFactory must be exported. This example provides a method for registering/selling COM services to the system, telling the COM system which classes and types it exports for it to create; the simplest implementation method is to use the macro structure provided by QAxFactory in main.cpp.

Because the currently generated ActiveX plug-in is an exe file that runs independently, an event loop is needed, and the following code needs to be added to main.cpp.

#include "active_test.h"
#include <QApplication>
#include <QMessageBox>
#include <QApplication>
#include <QAxFactory>

QAXFACTORY_BEGIN(
      "{41B3F63E-2E83-4BC5-A75B-9C08B5BF9BB7}", // type library ID
      "{CEF1EDF1-1BEE-48B5-8C7F-43FE9FA42E0C}"  // application ID
)QAXCLASS(Active_Test)
QAXFACTORY_END()

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    if(QAxFactory::isServer())
    {
        Active_Test w;
        w.show();
        return a.exec();//离开了{}这个作用域w窗口就没了,可能看到的是一闪而过的画面
    }
    else
    {
        QMessageBox::information(nullptr,"提示","服务启动失败",QMessageBox::Ok,QMessageBox::Ok);
    }
    return a.exec();
}

ActiveX control released in the form of exe does not require registration. Add the -activex parameter at startup to tell it to start as a COM service (the default is to start with an executable program), then the client can use the service it provides normally . It can also be registered to the system, so you don't need to start with -activex every time .

To facilitate testing, you can add -activex to the QtCreate configuration parameter command line

Next, open the registry to view the registered ID:

 

Six, publish the application and run the test

After the program is successfully compiled, go to the directory generated by the application program and look for dependent libraries for xxx.exe.

D:\linux-share-dir\QT\build-activex_test-Desktop_Qt_5_12_6_MSVC2017_32bit-Release\release>cd /d D:\linux-share-dir\QT\build-activex_test-Desktop_Qt_5_12_6_MSVC2017_32bit-Release\release

D:\linux-share-dir\QT\build-activex_test-Desktop_Qt_5_12_6_MSVC2017_32bit-Release\release>windeployqt.exe activex_test.exe 

 

After packaging, create a new xxx.html file in any directory, write the test code, and complete the call.

Example:

<html>
<head>
<title>Active_Test</title>
</head>
<body>
<object id="Active_Test" width="80%" height="80%"
	classid="clsid:CB1DB4A6-D16D-4D20-AE0D-67F1DCF542C5">
</object>
</body>
</html>

The classid should be changed according to the classid in your program.

After writing, select the file with the mouse, right-click to select the open method, and select IE to open.

At this point, the program has run successfully in the browser.

 

 

 

 

 

 

Guess you like

Origin blog.csdn.net/xiaolong1126626497/article/details/112550412