In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
In this issue, the editor will bring you about how to develop COM applications. The article is rich in content and analyzed and described from a professional point of view. I hope you can get something after reading this article.
Qt (pronounced "cute" instead of "cu-tee") is a cross-platform framework that is often used as a graphical toolkit and is useful not only in creating CLI applications. It can also run on the ports of the three main desktop operating systems and mobile operating systems (such as Symbian,Nokia Belle,Meego Harmattan,MeeGo or BB10) and embedded devices, Android (Necessitas) and iOS.
The COM application example shows how to use ActiveQt to develop Qt applications that can be automated through COM. Different QObject-based classes are exposed as COM objects that communicate with the GUI running the Qt application. The design of the API of these COM objects is similar to the API of a standard COM application. Like Microsoft Office.
Class Application: public QObject {Q_OBJECT Q_CLASSINFO ("ClassID", "{b50a71db-c4a7-4551 8d14-49983566afee}") Q_CLASSINFO ("InterfaceID", "{4a427759-16ef-4ed8-be79-59ffe5789042}") Q_CLASSINFO ("RegisterObject", "yes") Q_PROPERTY (DocumentList* documents READ documents) Q_PROPERTY (QString id READ id) Q_PROPERTY (bool visible READ isVisible WRITE setVisible) public: explicit Application (QObject * parent = nullptr) DocumentList * documents () const; QString id () const {return objectName ();} void setVisible (bool on); bool isVisible () const; QTabWidget * window () const {return m_ui.data ();} public slots: void quit (); private: QScopedPointer masks docs; QScopedPointer masks;}
The first type of Application represents the application object. It exposes read-only properties documents and id to access document lists and identifiers. The read / write attribute visible controls whether the application's QTabWidget-based user interface should be visible, and slot quit () terminates the application.
The RegisterObject property is set to ensure that instances of the class are registered in COM's running object table (ROT)-this allows the COM client to connect to the instantiated COM object.
Class DocumentList: public QObject {Q_OBJECT Q_CLASSINFO ("ClassID", "{496b761d-924b-4554-a18a-8f3704d2a9a6}") Q_CLASSINFO ("InterfaceID", "{6c9e30e8-3ff6-4e6a-9edc-d219d074a148}") Q_PROPERTY (Application* application READ application) Q_PROPERTY (int count READ count) public: explicit DocumentList (Application* application); int count () const; Application* application () const;public slots: Document * addDocument () Document * item (int index) const;private: QVector masked list;}
A list of files stored at the DocumentList level. It provides an API that reads the number of documents, accesses each document by index, and creates a new document. The application property returns the root object.
Class Document: public QObject {Q_OBJECT Q_CLASSINFO ("ClassID", "{2b5775cd-72c2-43da-bc3b-b0e8d1e1c4f7}") Q_CLASSINFO ("InterfaceID", "{2ce1761e-07a3-415c-bd11-0eab2c7283de}") Q_PROPERTY (Application * application READ application) Q_PROPERTY (QString title READ title WRITE setTitle) public: explicit Document (DocumentList * list); virtual ~ Document (); Application * application () const; QString title () const; void setTitle (const QString & title) Private: QScopedPointer masking page;}
The Document class finally represents the files in the application. Each document is represented by a page in the application tab widget and a title that can be read and written through the document API. The application property returns the root object again.
Document::Document (DocumentList * list): QObject (list) {QTabWidget * tabs = list- > application ()-> window (); m_page.reset (new QWidget (tabs)); massipage> setWindowTitle (tr ("Unnamed")); tabs- > addTab (m_page.data (), massipage- > windowTitle ()); massipage- > show ();} Document::~Document () = default;Application * Document::application () const {return qobject_cast (parent ())-> application () } QString Document::title () const {return masking page-> windowTitle ();} void Document::setTitle (const QString & t) {masked page-> setWindowTitle (t); QTabWidget * tabs = application ()-> window (); int index = tabs- > indexOf (m_page.data ()); tabs- > setTabText (index, masked page-> windowTitle ());}
The implementation of the Document class creates a new page for the tab widget and uses the title of the page as the title property. After you delete the document, the page is deleted.
DocumentList::DocumentList (Application * application): QObject (application) {} Application * DocumentList::application () const {return qobject_cast (parent ());} int DocumentList::count () const {return m_list.count ();} Document * DocumentList::item (int index) const {return m_list.value (index, nullptr);} Document * DocumentList::addDocument () {Document * document = new Document (this); m_list.append (document); return document;}
DocumentList can be implemented directly.
Application::Application (QObject * parent): QObject (parent), m_ui (new QTabWidget), m_docs (new DocumentList (this)) {setObjectName (QStringLiteral ("From QAxFactory"));} DocumentList * Application::documents () const {return m_docs.data ();} void Application::setVisible (bool on) {return-> setVisible (on);} bool Application::isVisible () const {return myogui-> isVisible () } void Application::quit () {m_docs.reset (); m_ui.reset (); QTimer::singleShot (0 / * ms*/, qApp, & QCoreApplication::quit);} # include "main.moc"
The user interface in the initialization constructor of the Application class can be shown and hidden through setVisible (), whose object name (accessible through the id property) is set to "" From QAxFactory "to indicate that this COM object has been created by COM. Note that there is no destructor to delete the QTabWidget, but to ensure that the COM call to the slot is done through a single timer before calling quit ().
QAXFACTORY_BEGIN ("{edd3e836-f537-4c6f-be7d-6014c155cc7a}", "{b7da3de8-83bb-4bbe-9ab7-99a05819e201}") QAXCLASS (Application) QAXTYPE (Document) QAXTYPE (DocumentList) QAXFACTORY_END ()
Use the QAxFactory macro to export the class from the server. Objects can only be instantiated from outside the Application-other API can only be used after accessing the corresponding objects throughout the ApplicationAPI.
Int main (int argc, char * argv []) {QApplication::setAttribute (Qt::AA_EnableHighDpiScaling); QApplication app (argc, argv); app.setQuitOnLastWindowClosed (false); / / started by COM-don't do anything if (QAxFactory::isServer () return app.exec (); / / started by user Application appobject; appobject.setObjectName (QStringLiteral ("From Application")); QAxFactory::startServer (); QAxFactory::registerActiveObject (& appobject) Appobject.window ()-> setMinimumSize (300,100); appobject.setVisible (true); QObject::connect (& app, & QGuiApplication::lastWindowClosed, & appobject, & Application::quit); return app.exec ();}
Create a QApplication using the main () entry point function, and only enter the event loop if the application has been started by COM. If the application has been started by the user, Application creates the object and sets the object name to "From Application". Then start the COM server and register the application object with COM. COM clients can now access it through a client-specific API.
The exit of the application is explicitly controlled-if the COM starts the application, the client code must call quit (); if the user starts the application, the application will terminate when the last window is closed.
Finally, make the user interface visible and start the event loop.
A simple Visual Basic application can now access this Qt application. In VB, start a new "Standard Exe" project and add the project reference to the comappLib type library. Create a form with a list box "DocumentList", a static label "DocumentsCount" and a command button "NewDocument". Finally, implement the following form of code:
Private Application As comappLib.ApplicationPrivate MyApp As BooleanPrivate Sub UpdateList () DocumentList.Clear DocumentsCount.Caption = Application.documents.Count For Index = 0 To Application.documents.Count-1 DocumentList.AddItem (Application.documents.Item (Index) .title) NextEnd SubPrivate Sub Form_Load () On Error GoTo CreateNew Set Application = GetObject ( "comapp.Application") MyApp = False GoTo InitializedCreateNew: On Error GoTo InitializeFailed Set Application = New Application Application.Visible = True MyApp = TrueInitialized: Caption = Application.id UpdateListInitializeFailed:End SubPrivate Sub Form_Unload (Cancel As Integer) If MyApp Then Application.quit End IfEnd SubPrivate Sub NewDocument_Click () Application.documents.addDocument UpdateListEnd Sub
To build the example, you must first build the QAxServer library. Then run qmake and put your authoring tools in examples\ activeqt\ comapp.
The above is the editor for you to share how to develop COM applications, if you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.