Results 1 to 4 of 4

Thread: trouble with the QAction and Signal/Slot-system

  1. #1
    Join Date
    Aug 2007
    Location
    Gothenburg, Sweden
    Posts
    9
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question trouble with the QAction and Signal/Slot-system

    Hello,
    I have a little trouble with the QAction and Signal/Slot-system. How do I tell what QAction was just activated(triggered) when I'm already "sent on" to the SLOT-function?

    Qt Code:
    1. #include <QtGui>
    2.  
    3. #include "mainwindow.h"
    4.  
    5. MainWindow::MainWindow()
    6. {
    7. setupUi(this);
    8.  
    9. connect(pushButton, SIGNAL(clicked()), this, SLOT(pushButton_clicked()));
    10.  
    11. userCounter=0;
    12. }
    13.  
    14. //Read files from users-directory (every user is represented by the filename in a file)
    15. //Add the filenames to a array of Action-pointers
    16. //Make a connection with the Action and the edit-user function(slot)
    17. void MainWindow::pushButton_clicked()
    18. {
    19. QDir dir;
    20. dir.setPath("users");
    21. QStringList userFiles = dir.entryList(); //gets the filelist from the users-dir
    22. userFiles.removeFirst(); //Removes the . entry
    23. userFiles.removeFirst(); //Removes the .. entry
    24.  
    25. //Make Actions and send em to the menu until the StringList is empty
    26. while(!userFiles.isEmpty()){
    27. userAction[userCounter] = new QAction(userFiles.takeFirst(), this);
    28. menuUser->addAction(userAction[userCounter]);
    29. connect(userAction[userCounter], SIGNAL(triggered(), this, SLOT(editUser()));
    30. userCounter++;
    31. }
    32. }
    33.  
    34. void MainWindow::editUser()
    35. {
    36. //textLabel is just a label in the mainwindow - so i can see if i reach this function
    37. textLabel->setText("One has been triggered - my problem is to tell which one");
    38. }
    To copy to clipboard, switch view to plain text mode 

    So, I click the "pushButton" and voila - the menu gets filled up.
    I choose one of the users from the menu - and voila - the textLabel is updated.

    However - I need to know, being in the editUser-function, what user to change. that is - what Action OR what menuoption was triggered - what is the text of that menu-option (a.k.a. the userfilename)?

    I have tried to work a little with sending QAction-pointers along to the editUser-function, without any working result. And even if I get that to work - how do i extract the "username" in a smart way from the QAction in question?

    Any solutions to this problem - or Work-arounds to make the same thing happen in a different way would be appreciated.

  2. #2
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: trouble with the QAction and Signal/Slot-system

    Hi,

    you can use QObject::sender() to get the sender of a slot.

    See also class QSignalMapper
    Last edited by wysota; 19th November 2007 at 18:36. Reason: Changed [quote] to [qtclass]
    A camel can go 14 days without drink,
    I can't!!!

  3. The following 2 users say thank you to mcosta for this useful post:

    doktorn (19th November 2007), vycke (11th April 2008)

  4. #3
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: trouble with the QAction and Signal/Slot-system

    QActionGroup is also one option.
    J-P Nurmi

  5. The following user says thank you to jpn for this useful post:

    doktorn (19th November 2007)

  6. #4
    Join Date
    Aug 2007
    Location
    Gothenburg, Sweden
    Posts
    9
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Talking Re: trouble with the QAction and Signal/Slot-system

    Very Nice. I went with the QActionGroup this time, but the other info was also very helpful.
    Thanks to both of you mcosta and jpn.

    I post my working code - in case somebody has the same problem. (And yes, I fertilize my code with too many comments )

    Qt Code:
    1. #include <QtGui>
    2.  
    3. #include "mainwindow.h"
    4.  
    5. MainWindow::MainWindow()
    6.  
    7. {
    8. setupUi(this);
    9. connect(pushButton, SIGNAL(clicked()), this, SLOT(pushButton_clicked()));
    10. userCounter=0; //sets users to 0 at creation of each mainwindowobject (we only have one - so its in reality a static int)
    11. }
    12.  
    13. void MainWindow::pushButton_clicked() //here a filereding from the user-dir will take place
    14. {
    15. QDir dir;
    16. dir.setPath("users");
    17. QStringList userFiles = dir.entryList(); //gets the filelist from the users-dir
    18. userFiles.removeFirst(); //Removes the . entry
    19. userFiles.removeFirst(); //Removes the .. entry
    20.  
    21. actionGroup = new QActionGroup(this); //Makes a alignmentgroup of the actions so that we can identify which one has been chosen
    22.  
    23. while(!userFiles.isEmpty()){
    24. userAction[userCounter] = new QAction(userFiles.takeFirst(), this); //adds the first userFile-entry to a userAction-entry
    25. userAction[userCounter]->setCheckable(true); //The action must be set checkable so we can check this status later on
    26. actionGroup->addAction(userAction[userCounter]); //adds the userAction-entry to the actionGroup
    27. menuUser->addAction(userAction[userCounter]); //adds the userAction-entry to the usermenu
    28. connect(userAction[userCounter], SIGNAL(triggered()), this, SLOT(editUser()));
    29. userCounter++;
    30. }
    31. }
    32. void MainWindow::editUser() //temporary function that will be replaced with the real edituser-program
    33. {
    34. QAction * tmp = actionGroup->checkedAction();
    35. if (tmp==0) textLabel->setText("Bad Data");
    36. else {
    37. textLabel->setText(tmp->text()); //try out the username (tmp) by displaying it on textLabel
    38. }
    39. }
    To copy to clipboard, switch view to plain text mode 

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.