Results 1 to 12 of 12

Thread: Convert from QAction to QWidget

  1. #1
    Join Date
    Jan 2011
    Posts
    6
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Convert from QAction to QWidget

    Hi,

    i'm getting the following error message while compiling:
    Qt Code:
    1. error C2664: 'QTest::mouseClick' : cannot convert parameter 1 from 'QAction *' to 'QWidget *'
    To copy to clipboard, switch view to plain text mode 

    This is my coding:
    Qt Code:
    1. QMenuBar* mb = pWidget->findChild<QMenuBar *>("menubar");
    2. QAction* fileAction = mb->findChild<QAction *>("File");
    3. QAction* newProject = fileAction->findChild<QAction *>("Create New Project...");
    4. QTest::mouseClick( newProject, Qt::LeftButton );
    To copy to clipboard, switch view to plain text mode 

    Two questions:
    1) I know that mouseClick expects a QWidget, but in the tutorial the use e.g. QLineEdit so i think this should work with QAction, too?!
    2) Is there another possibility to get the children of an object? I think it's weird to get the children from a top-object this way..

    kind regards

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Convert from QAction to QWidget

    Quote Originally Posted by duck182 View Post
    but in the tutorial the use e.g. QLineEdit so i think this should work with QAction, too?!
    No, because QLineEdit inherits QWidget and QAction does not.

    Call QAction::trigger() instead of simulating a click.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Jan 2011
    Posts
    6
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Convert from QAction to QWidget

    So it should look like this?
    Qt Code:
    1. newProject->trigger();
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Convert from QAction to QWidget

    Qt Code:
    1. if(newProject)
    2. newProject->trigger();
    3. else
    4. qFatal("Action newProject not found");
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Jan 2011
    Posts
    6
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Convert from QAction to QWidget

    Hi,

    mh okay I did not get the correct objects..

    But I'm not able to get QAction::trigger() working..
    The QMenuBar contains a QMenu. The QMenu contains a QAction. But this QAction has no objectName.
    So how do I get the correct pointer to the QAction-item?

    Qt Code:
    1. QMenuBar* mb = pWidget->findChild<QMenuBar *>("menubar"); // works
    2. QMenu* fileMenu = mb->findChild<QMenu *>("menuMenu"); // works
    3.  
    4. // Now i would like to get the pointer to the QAction.
    5. QAction* newProject = fileMenu ->findChild<QAction *>("Create New Project..."); // this won't work because the QAction-object has no name
    To copy to clipboard, switch view to plain text mode 

    The goal is to simulate a mouseclick on this QAction.

  6. #6
    Join Date
    Jan 2009
    Location
    Germany
    Posts
    131
    Thanks
    11
    Thanked 16 Times in 16 Posts
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Convert from QAction to QWidget

    You can set the objectname with QObject::setObjectName()

  7. #7
    Join Date
    Jan 2011
    Posts
    6
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Convert from QAction to QWidget

    I'm testing a complete application so I'm not able to give object names to these QActions.

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Convert from QAction to QWidget

    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. #9
    Join Date
    Jan 2011
    Posts
    6
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Convert from QAction to QWidget

    Okay, thank you so far.

    Now i get all actions and the submenu-objects correctly.
    One questions remains
    How can I call a specific QAction? At the moment I'm iterating the QList<QAction *> to get the ations.. If there are more than one I have to select one (which is obviously possible by comparing the text of these actions). But how can I choose one without iterating?

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Convert from QAction to QWidget

    If your action is the fifth added to the widget, you can call:
    Qt Code:
    1. menu->actions().at(4);
    To copy to clipboard, switch view to plain text mode 
    etc.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  11. The following user says thank you to wysota for this useful post:

    duck182 (26th January 2011)

  12. #11
    Join Date
    Jan 2011
    Posts
    6
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Convert from QAction to QWidget

    Oh okay, but not via the text-property or sth equal?

  13. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Convert from QAction to QWidget

    You can iterate over available actions and check their text if you want.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Replies: 2
    Last Post: 16th September 2010, 13:06
  2. Convert QWidget to QVariant How to?
    By sergey_85 in forum Qt Programming
    Replies: 3
    Last Post: 15th December 2009, 07:21
  3. convert QVariant to QAction
    By sepehr in forum Qt Programming
    Replies: 3
    Last Post: 5th April 2009, 11:03
  4. Convert Windows "HWND" to QWidget* pointer?
    By gerome69 in forum Qt Programming
    Replies: 4
    Last Post: 20th September 2006, 13:03
  5. Replies: 1
    Last Post: 2nd May 2006, 21:11

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.