Results 1 to 9 of 9

Thread: COM object and QtVariant casting issue

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2017
    Location
    Spain
    Posts
    5
    Thanks
    4
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default COM object and QtVariant casting issue

    Hello every one,
    I'm trying to use a Windows COM object and I'm having trouble casting a returned function value. I am using Qt 5.7.0 and I got .h and .cpp file for COM object with dumpcpp tool. For simplicity I show only the kind of interest:

    Qt Code:
    1. class SCAPILIB_EXPORT IAttrList : public QAxObject
    2. {
    3. public:
    4. IAttrList(IDispatch *subobject = 0, QAxObject *parent = 0)
    5. : QAxObject((IUnknown*)subobject, parent)
    6. {
    7. internalRelease();
    8. }
    9.  
    10. /*
    11.   Method AddItem
    12.  
    13.   method AddItem
    14.  
    15.   */
    16. inline void AddItem(Attr Attribute, const QVariant& value);
    17.  
    18. /*
    19.   Method FindItemIndex
    20.  
    21.   method FindItemIndex
    22.  
    23.   */
    24. inline int FindItemIndex(int startIndex, SCAPILib::Attr Attribute);
    25.  
    26. /*
    27.   Method GetCount
    28.  
    29.   method GetCount
    30.  
    31.   */
    32. inline int GetCount();
    33.  
    34. /*
    35.   Method GetIndex
    36.  
    37.   method GetIndex
    38.  
    39.   */
    40. inline QVariant GetIndex(int index, Attr& pAttribute);
    41.  
    42. /*
    43.   Method GetItem
    44.  
    45.   method GetItem
    46.  
    47.   */
    48. inline QVariant GetItem(Attr Attribute);
    49.  
    50. /*
    51.   Method OutputToString
    52.  
    53.   method OutputToString
    54.  
    55.   */
    56. inline QString OutputToString();
    57.  
    58. /*
    59.   Method RestoreFromBlob
    60.  
    61.   method RestoreFromBlob
    62.  
    63.   */
    64. inline void RestoreFromBlob(QVariant blob);
    65.  
    66. /*
    67.   Method SaveToBlob
    68.  
    69.   method SaveToBlob
    70.  
    71.   */
    72. inline void SaveToBlob(QVariant& pBlob);
    73.  
    74. /*
    75.   Method SetIndex
    76.  
    77.   method SetIndex
    78.  
    79.   */
    80. inline void SetIndex(int index, SCAPILib::Attr Attribute, const QVariant& value);
    81.  
    82. // meta object functions
    83. static const QMetaObject staticMetaObject;
    84. virtual const QMetaObject *metaObject() const { return &staticMetaObject; }
    85. virtual void *qt_metacast(const char *);
    86. };
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. template<>
    2. struct QMetaTypeFunctionHelper<SCAPILib::IAttrList, /* Accepted */ true> {
    3. static void Destruct(void *t)
    4. {
    5. Q_UNUSED(t)
    6. static_cast<SCAPILib::IAttrList*>(t)->SCAPILib::IAttrList::~IAttrList();
    7. }
    8. static void *Construct(void *where, const void *t)
    9. {
    10. Q_ASSERT(!t);
    11. Q_UNUSED(t)
    12. return new (where) SCAPILib::IAttrList;
    13. }
    14. #ifndef QT_NO_DATASTREAM
    15. static void Save(QDataStream &stream, const void *t) { stream << *static_cast<const SCAPILib::IAttrList*>(t); }
    16. static void Load(QDataStream &stream, void *t) { stream >> *static_cast<SCAPILib::IAttrList*>(t); }
    17. #endif // QT_NO_DATASTREAM
    18. };
    To copy to clipboard, switch view to plain text mode 
    The function GetItem always returned a QVariant, usually the QVariant contain a QString, QInt or QBool, but with a determinate attribute the returned value will be a pointer to the base class(IAttrList *).

    Qt Code:
    1. inline QVariant IAttrList::GetItem(Attr Attribute)
    2. {
    3. QVariant qax_result;
    4. void *_a[] = {(void*)&qax_result, (void*)&Attribute};
    5. qt_metacall(QMetaObject::InvokeMetaMethod, 12, _a);
    6. return qax_result;
    7. }
    To copy to clipboard, switch view to plain text mode 

    I can use the function of the COM object just fine, except when it will be return a indicated class pointer. In this case de QVariant returnet type is (IUnknown *) and I not able to cast this to (IAttrLits *).

    I'm tried with:

    Qt Code:
    1. IAttrList * Attr = AttrList.GetItem(Some_Attribute).Value<IAttrList *>();
    To copy to clipboard, switch view to plain text mode 
    But compiler returned error: static assertion failed: qobject_cast requires the type to have a Q_OBJECT macro

    Which I understand is normal because, according to QT documentation, classes derived from QAxObject can not contain the Q_OBJECT macro.

    I have also tried with:

    Qt Code:
    1. IAttrList * attr = static_cast<IAttrList *>(AttrList.GetItem(Some_attribute).data());
    To copy to clipboard, switch view to plain text mode 
    and
    Qt Code:
    1. IAttrList * attr = static_cast<IAttrList *>(AttrList.GetItem(Some_attribute).value<void *>();
    To copy to clipboard, switch view to plain text mode 
    It's compile but the AttrList pointer does not seem to be valid because I get a segmentation fault to try use it.

    I have also read about the QueryInterface method and I have tried with something like:

    Qt Code:
    1. QVariant * var =AttrList.GetItem(some_attribute);
    2. HRESULT hr = AttrList.queryInterface(UuID,(void **)&var);
    To copy to clipboard, switch view to plain text mode 
    The problen is that this interface not have a UUID and I can't do the call.

    Also I have read about get a class instance with QuerySubOject but a QVariant Objects does not have it.

    I do not know how to solve it, any subgestion?. Thanks in advanced.
    Last edited by mlago; 3rd January 2017 at 09:50.

Similar Threads

  1. casting object to enum in Qt
    By alizadeh91 in forum Qt Programming
    Replies: 1
    Last Post: 18th August 2012, 15:22
  2. Two dynamically created object interaction issue
    By kornicameister in forum Qt Quick
    Replies: 2
    Last Post: 9th September 2011, 11:35
  3. QtVariant pointer from QHash list
    By rmat in forum Qt Programming
    Replies: 3
    Last Post: 9th August 2011, 10:28
  4. Qt Webkit 2.1 - SVG Issue - Object overlapping
    By Tushar in forum Qt Programming
    Replies: 0
    Last Post: 29th December 2010, 08:32
  5. object handling issue
    By prolink007 in forum Qt Programming
    Replies: 1
    Last Post: 12th April 2010, 07:42

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.