Results 1 to 7 of 7

Thread: QAxWidget Programming: pass by reference

  1. #1
    Join Date
    Mar 2014
    Posts
    5
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default QAxWidget Programming: pass by reference

    I have been using a ActiveX widget in my C++ program using the QAxWidget class. It works fine for all the functions that return a single value and have input arguments only. However, when I try to pass a variable by reference which I will use later as a return value, it won't compile because QVariant can't be instantiated with a pointer or it has errors at runtime. The documenation for the ActiveX widget I'm using is geared for Visual Basic so I'm probably using the wrong syntax. Here is a function that works:

    ************** LIBRARY DOCUMENTATION **************
    Motor Method SetAbsMovePos

    See Also Example Code


    Visual Basic Syntax

    Function SetAbsMovePos(lChanID As Long, fAbsPos As Single) As Long


    Parameters

    lChanID - the channel identifier

    fAbsPos - the absolute position associated with the specified channel


    Returns

    MG Return Code


    Details

    This method sets the absolute position to which the channel specified by the lChanID parameter will move, the next time the MoveAbsolute method is called. The position is set to the value specified in the fAbsPos parameter and is measured from the home position, in real world units (mm or degrees).

    The lChanID parameter takes values specified by the HWCHANNEL enumeration.

    Note: On dual channel units, the system cannot set the absolute position of both channels simultaneously, and the use of CHANBOTH_ID is not allowed.

    To get the absolute position for a particular channel, see the GetAbsMovePos method.
    ************** MY FUNCTIONING IMPLEMENTATION **************
    Qt Code:
    1. QAxWidget* hMotor;
    2. hMotor = new QAxWidget();
    3. hMotor->setControl("MGMOTOR.MGMotorCtrl.1");
    4. hMotor->setProperty("HWSerialNum",QVariant(67839858));
    5. hMotor->dynamicCall("StartCtrl");
    6. double pos(10);
    7.  
    8. hMotor->dynamicCall("SetAbsMovePos(QVariant,QVariant)",chanID,QVariant(pos));
    9.  
    10. hMotor->dynamicCall("MoveAbsolute(QVariant,QVariant,QVariant)",chanID,1);
    To copy to clipboard, switch view to plain text mode 



    ************** FUNCTION I WANT TO USE **************
    Motor Method GetPosition

    See Also Example Code


    Visual Basic Syntax

    Function GetPosition(lChanID As Long, pfPosition As Single) As Long


    Parameters

    lChanID - the channel identifier

    pfPosition - the current position of the associated channel


    Returns

    MG Return Code


    Details

    This method obtains the present position for the channel(s) specified by the lChanID parameter, and returns a value in the pfPosition parameter.

    If a calibration file has been associated with the channel using the APT Config utility, the method returns the calibrated position. If no calibration file has been associated, then the returned position is uncalibrated. Refer to the helpfile in the APT Config utility for further details on using position calibration files.

    Note that the position values returned are derived from either the ‘Microstep Count’ or the Encoder Count, dependent on the positioning mode selected. See the encoder operation section for more information.

    The position of the stage associated with the specified channel is determined by its displacement from the 'Home' position.

    The lChanID parameter takes values specified by the HWCHANNEL enumeration.

    ************** MY SYNTAX ATTEMPTS AND ERROR OUTPUT **************
    Qt Code:
    1. #include <QApplication>
    2. #include <QAxWidget>
    3. #include <QDebug>
    4. #include <QThread>
    5.  
    6. int main(int argc, char *argv[])
    7. {
    8. QApplication g(argc,argv);
    9.  
    10. QAxWidget hMotor;
    11. QVariant chanID = QVariant(0);
    12. hMotor.setControl("MGMOTOR.MGMotorCtrl.1");
    13. hMotor.setProperty("HWSerialNum",QVariant(67839858));
    14. hMotor.dynamicCall("StartCtrl");
    15. hMotor.dynamicCall("EnableHWChannel(QVariant)",chanID);
    16. QThread::sleep(1); // Give it time to enable the channel
    17.  
    18. double pos(5);
    19. hMotor.dynamicCall("SetAbsMovePos(QVariant,QVariant)",chanID,QVariant(pos));
    20. hMotor.dynamicCall("MoveAbsolute(QVariant,QVariant,QVariant)",chanID,0);
    21.  
    22. double outPos;
    23. hMotor.dynamicCall("GetPosition(QVariant,QVariant)",chanID,QVariant(outPos));
    24. qWarning() << outPos;
    25. hMotor.dynamicCall("StopCtrl");
    26.  
    27.  
    28. return (0);
    29.  
    30. }
    To copy to clipboard, switch view to plain text mode 

    CONSOLE OUTPUT
    QAxBase: Error calling IDispatch member GetPosition: Type mismatch in parameter
    1
    1.39065e-309
    Press <RETURN> to close this window...

    All the functions work except the "GetPosition" one. The outPos should a double between 0 and 100, not 1.39e-309 (bad memory). This is just a simple example and I've used the other functions probably 100 times and I'm sure they work. If I try QVariant(&outPos) as the argument, I get a compile error (QVariant::QVariant(*void) is private...). Any ideas?

    Thanks in advance!

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QAxWidget Programming: pass by reference

    I am not certain how a pointer to a float can be passed as the second argument but I would try:
    Qt Code:
    1. double outPos(0.0);
    2. QVariant retCode = hMotor.dynamicCall("GetPosition(int, float&)",chanID, outPos);
    To copy to clipboard, switch view to plain text mode 
    The parameter types come from the lookup in the docs.

  3. #3
    Join Date
    Mar 2014
    Posts
    5
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Unhappy Re: QAxWidget Programming: pass by reference

    Thanks, I appreciate the help.

    I just tried your suggestion. It gives the same error as before.

    QAxBase: Error calling IDispatch member GetPosition: Type mismatch in parameter
    1
    0
    Press <RETURN> to close this window...
    Last edited by NeuroC++; 4th March 2014 at 00:29. Reason: added thanks and smiley

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QAxWidget Programming: pass by reference

    How about this?
    Qt Code:
    1. float outPos(0.0);
    2. QVariant retCode = hMotor.dynamicCall("GetPosition(int, float*)",chanID, &outPos);
    To copy to clipboard, switch view to plain text mode 
    I don't really expect this to work, just covering the bases.

  5. #5
    Join Date
    Mar 2014
    Posts
    5
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QAxWidget Programming: pass by reference

    Yeah, if I do that it gives the "QVariant::QVariant(*void) is private" compile error. I've even tried casting the pointer to "outPos" into an int that I put into a QVariant which I then passed to the function. It compiled but still gave a type mismatch error at runtime.

  6. #6
    Join Date
    Mar 2014
    Posts
    5
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QAxWidget Programming: pass by reference

    bump. Still can't solve this problem.


    Added after 1 42 minutes:


    I found a call that doesn't throw a compile or runtime error but the value doesn't change.
    Qt Code:
    1. float outPos(1.1);
    2. hMotor->dynamicCall("GetPosition(int,float&)",chanID,outPos);
    To copy to clipboard, switch view to plain text mode 

    My output is "1.1" after I call the function. Any ideas?
    Last edited by NeuroC++; 5th May 2014 at 20:27.

  7. #7
    Join Date
    Mar 2014
    Posts
    5
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QAxWidget Programming: pass by reference [SOLVED]

    It's a year later and I finally had a chance to debug this. So, without any further delay, voila!

    Qt Code:
    1. bool flag(0);
    2. QList<QVariant> args;
    3. args.push_back(QVariant(flag));
    4. motor->dynamicCall("GetHWCommsOK(QVariant&)",args);
    5. return args[0].toBool();
    To copy to clipboard, switch view to plain text mode 

    and it returns the value set by the ActiveX control. Hurray! Sorry to dig up an old thread, but I wanted the solution to be out there somewhere. I'll probably forget and have to come back here to remind myself.

Similar Threads

  1. Design: Signals => Pass by Reference
    By tescrin in forum Qt Programming
    Replies: 19
    Last Post: 7th December 2012, 22:40
  2. How to pass variables as reference from C++ to QML
    By dmendizabal in forum Qt Quick
    Replies: 1
    Last Post: 17th February 2012, 15:39
  3. Pass by reference in QtScript
    By songyu_us in forum Qt Programming
    Replies: 0
    Last Post: 14th July 2009, 18:59
  4. Pass by reference
    By vermarajeev in forum General Programming
    Replies: 6
    Last Post: 20th July 2007, 15:53
  5. QAxWidget
    By chak_med in forum Newbie
    Replies: 1
    Last Post: 10th September 2006, 21:05

Tags for this Thread

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.