Results 1 to 8 of 8

Thread: How To do an actionListener

  1. #1
    Join Date
    Jun 2008
    Posts
    32
    Qt products
    Qt4

    Default How To do an actionListener

    Hello, I need to implement an action listener but i dont know much about signals and slots.
    Let me explain what I'm trying to do:
    In my program I have 2 views. User will select from the view action in the menubar. There are 2 views. When 1 is selected, it will be checked and the other will become unchecked.Also the view will change.
    How can i implement this actionlistener??

  2. #2
    Join Date
    Jun 2008
    Location
    Saint-Petersburg
    Posts
    50
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How To do an actionListener

    Qt Code:
    1. void MyClass::pressMenu()
    2. {
    3. QAction* your_action = qobject_cast<QAction*> (sender());
    4. // Your code
    5. }
    To copy to clipboard, switch view to plain text mode 
    By this action you get your action. Then you change view according to selected action! Their actions you should to do in the slot (for example it's name is pressMenu)!
    Qt Code:
    1. class MyClass
    2. {
    3. // your variables and functions
    4. QAction* myActions[2];
    5. private slots:
    6. void pressMenu();
    7. }
    To copy to clipboard, switch view to plain text mode 
    - in header
    Qt Code:
    1. for(int i=0; i<2; ++i)
    2. {
    3. myActions[i] = new QAction(this);
    4. connect(myActions[i], SIGNAL(triggered()), this, SLOT(pressMenu()));
    5. }
    To copy to clipboard, switch view to plain text mode 
    - in source
    For example use this code!

  3. #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: How To do an actionListener

    J-P Nurmi

  4. #4
    Join Date
    Jun 2008
    Posts
    32
    Qt products
    Qt4

    Default Re: How To do an actionListener

    Thank you very much, it works=)
    but i did not understand what is this line for?
    "QAction* your_action = qobject_cast<QAction*> (sender());"
    I just put a print in the method and it prints it also without this line

  5. #5
    Join Date
    Jun 2008
    Location
    Saint-Petersburg
    Posts
    50
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How To do an actionListener

    You can select needed action from array of actions by this method!

  6. #6
    Join Date
    Jun 2008
    Location
    Saint-Petersburg
    Posts
    50
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How To do an actionListener

    For example, array considered two actions - "Open" and "New":
    Qt Code:
    1. void MyClass::pressMenu()
    2. {
    3. QAction* your_action = qobject_cast<QAction*> (sender());
    4. QString name_menu = your_action -> text();
    5. }
    To copy to clipboard, switch view to plain text mode 

    If you click in "Open", mean of name_menu is "Open", other - "New"!

  7. #7
    Join Date
    Jun 2008
    Posts
    32
    Qt products
    Qt4

    Default Re: How To do an actionListener

    Okey I get it. But I have another problem now. I have a menu at top. Like "File" menu in browser. In it, there are two actions: actionViewClassic, actionViewModern.
    These are checkable. So if one of them is checked, the other one must be unchecked. To reach this goal,I passed the other Action as a parameter to the function.However since, I created the gui with designer, if i try to change the other buttons checked function it gives me an error Because of scoping. how can I accomplish these checkablity situation ??

    Error:

    GeneratedFiles\Debug\moc_assignment1.cpp(66) : error C2248: 'QAction::QAction' : cannot access private member declared in class 'QAction'
    1> d:\qt\include\qtgui\../../src/gui/kernel/qaction.h(191) : see declaration of 'QAction::QAction'
    1> d:\qt\include\qtgui\../../src/gui/kernel/qaction.h(41) : see declaration of 'QAction'
    Last edited by bod; 27th June 2008 at 13:12.

  8. #8
    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: How To do an actionListener

    Quote Originally Posted by bod View Post
    These are checkable. So if one of them is checked, the other one must be unchecked... how can I accomplish these checkablity situation ??
    Take a look at QActionGroup as already proposed.
    J-P Nurmi

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
  •  
Qt is a trademark of The Qt Company.