Results 1 to 5 of 5

Thread: How to set the QAction's action

  1. #1
    Join Date
    Mar 2011
    Posts
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default How to set the QAction's action

    Hey guys

    I am writing a QT application and I have hit a bit of a wall.

    I have a file containing an undetermined number of data entries (blocks of 512 bytes). Each data entry contains a collection of variables as well as a data entry name, and a data entry category.

    I have created the code to look at this file and generate a QMenu. Basically the code creates sub-menus for each unique category found in the file, and then obtains the name of each data entry and places it in the correct category.

    This works fine and the menu appears as expected however the next step is where I am struggling. I have a function that requires arguments extracted from the data entry.

    So my question is this How can I add the required function calls to the QActions?

    Here is an extract of the relevant part of my code!

    Qt Code:
    1. // Create the menu structure
    2.  
    3. // *********************
    4. // ** CREATE BASE MENU
    5. QMenu *menu = new QMenu((QWidget*)ui->button);
    6. // *********************
    7. for(int i = 0; i < numberOfCategories; i++){
    8. // *******************
    9. // ** CREATE SUB MENU
    10. QMenu *submenu = new QMenu(dataEntryCategoryNameList[i], (QWidget*)menu);
    11. // *******************
    12. // ** FIND ALL DATA ENTRIES THAT ARE PART OF THE CURRENT CATEGORY
    13. for(int j = 0; j < dataEntryCount; j++){
    14. if(memcmp(dataEntryCategoryNameList[i], &dataEntryRawDataList[j][220], 30)==0){
    15. // **********************
    16. // ** ADD ACTION TO MENU
    17. char temp[30];
    18. memset(temp, 0x00, 30);
    19. memcpy(temp, &dataEntryRawDataList[j][0], 30);
    20. // OBTAIN RELEVANT COMMAND OPTIONS FOR THE CURRENT DATA ENTRY
    21. int var1 = /*value obtained from file*/
    22. int var2 = /*value obtained from file*/
    23. int var3 = /*value obtained from file*/
    24. int var4 = /*value obtained from file*/
    25. int var5 = /*value obtained from file*/
    26.  
    27. //Function I want to call for the current data entry:
    28. // myFunction(var1, var2, var3, var4, var5)
    29. QAction *action = new QAction(temp, (QObject*)submenu);
    30. submenu->addAction(action);
    31. // **********************
    32. }
    33. }
    34. menu->addMenu(submenu);
    35. }
    36. menu->exec(QCursor::pos());
    To copy to clipboard, switch view to plain text mode 

    Thanks for reading and I appreciate any help!

  2. #2
    Join Date
    Nov 2010
    Posts
    315
    Thanked 53 Times in 51 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: How to set the QAction's action

    1. store custom data somewhere for example QAction::setData
    2. if you what fetch those data when action is invoked then in slot connected to this action do:
      Qt Code:
      1. void SomeQObject::someSlot()
      2. {
      3. QAction* action = qobject_cast<QAction*>(sender());
      4. if (!action) {
      5. return;
      6. }
      7. if (!action->data().isNull) {
      8. yourDataType data = qvariant_cast<yourDataType>(action->data());
      9. ....
      10. }
      11. }
      To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Mar 2011
    Posts
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to set the QAction's action

    Nice. Cunning idea, thanks very much!

    I do have one problem with it though. Since I have been using QT creator this is the first time I have had to manually code the signal+slot process so I am not sure what to do.


    I presume I am meant to use the connect() function just after I have created the action to send a triggered() signal to my mainWindow widget, but I am a little confused by the functions arguments.

    Qt Code:
    1. // Class mainWindow...
    2.  
    3. void mainWindow::createMenu(){
    4. ...
    5. // Set action as int ID of current command
    6. int value = getValue();
    7. QAction myaction = new QAction(temp, (QObject*)subMenu);
    8. myaction->setData(QVariant(value));
    9.  
    10. connect(myAction, triggered(), (QWidget*)this, /*NO IDEA WHAT TO PUT HERE :P*/
    11. ...
    12. }
    13.  
    14.  
    15. void mainWindow::mySlot()
    16. {
    17. QAction* action = qobject_cast<QAction*>(sender());
    18. if (!action) {
    19. return;
    20. }
    21. if (!action->data().isNull) {
    22. yourDataType data = qvariant_cast<yourDataType>(action->data());
    23. char data[400];
    24. memset(data, 0x00, 400);
    25. memcpy
    26. }
    27. }
    To copy to clipboard, switch view to plain text mode 

    Am I on the right lines?

    Thanks again

  4. #4
    Join Date
    Nov 2010
    Posts
    57
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to set the QAction's action

    You probably have already figured this out:
    Qt Code:
    1. connect(myAction, SIGNAL(triggered()), this, SLOT(mySlot()));
    To copy to clipboard, switch view to plain text mode 

    The help has a good example of how to use connect.
    Last edited by pan; 18th March 2011 at 12:55. Reason: missing [code] tags

  5. #5
    Join Date
    Mar 2011
    Posts
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to set the QAction's action

    Brilliant I got it working thank you. I never realised the slot system could be so simple. The function prototype definitely looks 10000 times more intimidating than it actually is thanks for all your help!


    I actually have one last question if it is not too much trouble :P In my code I have used several new operators however I have not used any matching delete operators (memory leak?). Do they self destruct?


    Added after 9 minutes:


    Never mind I assume that YES i was causing a memory leak and fixed it by adding an array of pointers for the QAction and QMenu objects and simply ran through them at the end and deleted them

    <3
    Last edited by aatwo; 18th March 2011 at 13:49.

Similar Threads

  1. QToolBar and QAction
    By !Ci in forum Qt Programming
    Replies: 1
    Last Post: 22nd March 2008, 12:41
  2. QAction Icon
    By hgedek in forum Newbie
    Replies: 1
    Last Post: 14th September 2007, 12:30
  3. deleting qaction
    By hyling in forum Qt Programming
    Replies: 1
    Last Post: 8th December 2006, 21:48
  4. QAction
    By mickey in forum Qt Programming
    Replies: 7
    Last Post: 17th July 2006, 09:42
  5. No action checked in an exclusive action group
    By SkripT in forum Qt Programming
    Replies: 12
    Last Post: 13th February 2006, 06:19

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.