COM/ActiveX Technology in Qt and MFC

Lingfa Yang

  1. Make an activeX control in Qt
  2. Using the control in MS Office/IE
  3. Reuse activeX control in MFC developed in Qt
  4. How to customize an activeX control in MFC environment
  5. ActiveX scripting: use of OLE interfaces in a scripting engines
  6. Reuse activeX control in Qt developed in MFC

  1. Make an activeX in Qt

    • Make a widget (draw/paint in GDI+)
      class AnalogClock : public QWidget
      
      How to draw an analog clock?
    • Generate uuid for classID, interfaceID and eventsID.
      uuidgen >> uuid.txt // ms vs common tools
      
      e4ff4700-d874-474b-9f88-c5bb263bb1f7
      e6157a63-1a73-4bbc-b8f6-85c9c8bab4aa
      efa38bb5-dcda-42fe-98a6-7767da42845a
      
    • Make an activeX control
      class ActiveQtFactory : public QAxFactory
      
  2. Using the control in MS Office/IE

    Turn on developer tab in PowerPoint 2007
    1. Right click on menu bar and choose "Customize Quick Access Toolbar..."
    2. Select first tag "Popular"
    3. Turn on "Show Developer tab in the Ribbon" (see snapshot)
    4. More controls (see snapshot)
    5. Insert a control into PowerPoint (see snapshot); Press F5 for slideshow to see clock tick
  3. Reuse activeX control in MFC developed in Qt

    1. New | MFC AppWizard (exe) | Project Name (QtAx) | Dialog based | Finish (VS6.0 QtAx.zip) || File | New | Project | Visual C++ Projects | MFC |MFC Application | QtAxVs03 | Application Type (Dialog based) | Finish | View | Resource View | Double Click on IDD_QTAXVS03_DIALOG (QtAxVs03.zip)
    2. Right Click on Resource Dialog | Insert ActiveX Control... | AnalogClockAx Class
    3. Compile and run

      Runtime needs QtGui4.dll, QtCore4.dll, QtXml4.dll, qwt5.dll ...

  4. How to customize an activeX control in MFC environment

    1. Generate a wrapper
      Hit "Add Variable" will trigger a dialog which will generate a wrapper automatically.
      Then, in QtAxDlg.h you will find
      #include "analogclockax.h"
      
      CAnalogClockAx	m_clock;
      
      and the wrapper exposes all set and get functions
      class CAnalogClockAx : public CWnd
      {
      // Attributes
      public:
      	long GetLeft();
      	void SetLeft(long);
      }	
      
    2. Initialization
      Find OnInitDialog:
      BOOL CQtAxDlg::OnInitDialog()
      {
      	// TODO: Add extra initialization here
      	m_clock.SetLeft(500); // Customize
      }
      
    3. ListTreeTab example
      QListWidge and QTreeWidget in QTabWidget as Qt's ListTreeAx control in MFC dialog.

      (Project zip in VC++6.0 | VS2003 )
      Synchronize the list view and the tree view in Qt.
      connect(listWidget, 
      	SIGNAL(currentItemChanged(QListWidgetItem *, QListWidgetItem *)),
      	this, SLOT(currentListItem(QListWidgetItem *, QListWidgetItem *)));
      connect(treeWidget, 
      	SIGNAL(currentItemChanged(QTreeWidgetItem *, QTreeWidgetItem *)),
      	this, SLOT(currentTreeItem(QTreeWidgetItem *, QTreeWidgetItem *)));
      
      Use of hash
      	
      void ListTreeTab::currentListItem(
      	QListWidgetItem *current, QListWidgetItem *previous)
      {
      	treeWidget->setCurrentItem(treeToList.key(current));
      }
      
      void ListTreeTab::currentTreeItem(
      	QTreeWidgetItem *current, QTreeWidgetItem *previous)
      {
      	listWidget->setCurrentItem(treeToList.value(current));
      }
      
    4. Bilateral talks between Qt controls and MFC controls
      QWidget's signals are partially exposed, which you can in "Add Windows Message Handler..."

      void CListWidgetDlg::OncurrentRowChangedListwidgetax1(long p_currentRow) 
      {
      	m_currentText = m_list.GetItemText();
      	this->UpdateData(FALSE); // TODIALOG
      }
      
      void CListWidgetDlg::OnChangeEditCurrent() 
      {
      	this->UpdateData(TRUE); // TOMEMBER
      	m_list.SetItemText(m_currentText);	
      }
      

      Demo: Bilateral talks between Qt controls and MFC controls


      (Project zip in VC++6.0)

      Qt:  My current item changed.
      MFC: Yes I know. I'm displaying your change.
      MFC: Look, I am in editing mode (delete, then, keyboard in).
      Qt:  Yes I know. I'm displaying your change.
      Qt:  List itself is editable (double-click, or press F2)
           listItem->setFlags(listItem->flags() | Qt::ItemIsEditable); 
      
  5. ActiveX scripting: use of OLE interfaces in a scripting engines

    (undercounstruction)
  6. Reuse activeX control in Qt developed in MFC

    (undercounstruction)

Home | Qt | C/C++ | Online Articles: Impressions on MFC vs Qt Programming | Qt/COM