Results 1 to 7 of 7

Thread: get Text from Popupmenu action

  1. #1
    Join Date
    Jan 2006
    Posts
    273
    Thanks
    42
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default get Text from Popupmenu action

    Hi everyboby!

    I have a Q3PopupMenu for my ListView created.The idea from this Popupmenu is that the user can select a song and
    add to a playlist(submenu)
    I have a Menu and a Submenu. I generate actions with values from my Database (see code).
    For example: playlist1, playlist2, playlist3(actions).
    So my actions will everytime generated when the user start my popupmenu. I have a dynamic submenu with dynamic actions.
    All my actions call the same function "addToPlaylist()".Thats ok.
    Now i must get the selected action text from the submenu.
    With QAction->text() i can read the action text. But how could i get the selected text of my popupmenu?Because my action names is everytime the same.
    I need this text to be able to insert value on a database.


    Have somebody a idea?

    Qt Code:
    1. Q3PopupMenu* contextMenu = new Q3PopupMenu( this);
    2. Q3PopupMenu* contextMenu2 = new Q3PopupMenu( this);
    3.  
    4. if( Item )
    5. contextMenu->popup( point );
    6.  
    7. Q_CHECK_PTR( contextMenu );
    8.  
    9.  
    10.  
    11. if((ui.playlistname_cb->currentText() == "") && (ui.listView->childCount() > 0))
    12. {
    13. QSqlQuery select_playlistname("select playlistname from playlistname_tbl");
    14. while(select_playlistname.next())
    15. {
    16. QString playlistname = select_playlistname.value(0).toString();
    17. contextMenu2->setTitle("add to Playlist");
    18.  
    19. QAction *action = new QAction(playlistname, this);
    20. connect(action,SIGNAL(activated()),this,SLOT(addToPlaylist()));
    21. contextMenu2->addAction(action );
    22. contextMenu->addMenu(contextMenu2);
    23. QMessageBox::information(this,
    24. ("Info"),action->text());
    25. }
    26. contextMenu->exec( QCursor::pos() );
    27.  
    28. return;
    29. }
    To copy to clipboard, switch view to plain text mode 
    Think DigitalGasoline

  2. #2
    Join Date
    Jan 2006
    Posts
    667
    Thanks
    10
    Thanked 80 Times in 74 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: get Text from Popupmenu action

    use

    connect(contectMenu2,SIGNAL(triggered(QAction *)),this,SLOT(actionTriggered(QAction *)))

  3. #3
    Join Date
    Jan 2006
    Posts
    273
    Thanks
    42
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: get Text from Popupmenu action

    Hi

    Thanks for reply..

    But how could i get the selected item Text of my Submenu?I need this text.

    thanks
    Think DigitalGasoline

  4. #4
    Join Date
    Jan 2006
    Posts
    667
    Thanks
    10
    Thanked 80 Times in 74 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: get Text from Popupmenu action

    In the slot you can get the text

    Like this:

    Qt Code:
    1. void YourClass::actionTriggered(QAction *a)
    2. {
    3. QString str = a->text()
    4. }
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Jan 2006
    Posts
    273
    Thanks
    42
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: get Text from Popupmenu action

    Hi munna,

    If can not get my selected text from the submenu.

    Can you see the problem?

    Qt Code:
    1. connect(ui.listView, SIGNAL( contextMenuRequested( Q3ListViewItem *, const QPoint& , int ) ),
    2. this, SLOT( PlaylistMenu( Q3ListViewItem *, const QPoint &, int ) ) );
    3.  
    4. contextMenu = new Q3PopupMenu( this);
    5. contextMenu2 = new Q3PopupMenu( this);
    6. connect(contextMenu2,SIGNAL(triggered(QAction *)),this,SLOT(actionTriggered(QAction *a)));
    7.  
    8. void MainWindow::PlaylistMenu( Q3ListViewItem* Item, const QPoint & point, int )
    9. {
    10.  
    11. if( Item )
    12. contextMenu->popup( point );
    13. Q_CHECK_PTR( contextMenu );
    14.  
    15.  
    16. contextMenu2->clear();
    17. contextMenu->clear();
    18.  
    19.  
    20. if((ui.playlistname_cb->currentText() == "") && (ui.listView->childCount() > 0))
    21. {
    22. contextMenu2->setTitle("add to Playlist");
    23. QSqlQuery select_playlistname("select playlistname from playlistname_tbl");
    24. while(select_playlistname.next())
    25. {
    26. QString playlistname = select_playlistname.value(0).toString();
    27. contextMenu2->addAction(playlistname, this, SLOT(addToPlaylist()));
    28. contextMenu->addMenu(contextMenu2);
    29. }
    30. contextMenu->exec( QCursor::pos() );
    31. //delete contextMenu;
    32. //delete contextMenu2;
    33. return;
    34. }
    35.  
    36. }
    37.  
    38. void MainWindow::addToPlaylist()
    39. {
    40. QMessageBox::information(this, "", "addToPlaylist");
    41.  
    42.  
    43. }
    44. void MainWindow::actionTriggered(QAction *a)
    45. {
    46. QString str = a->text();
    47. QMessageBox::information(this, "", str);
    48. }
    To copy to clipboard, switch view to plain text mode 
    Think DigitalGasoline

  6. #6
    Join Date
    Jan 2006
    Posts
    667
    Thanks
    10
    Thanked 80 Times in 74 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: get Text from Popupmenu action

    Qt Code:
    1. connect(contextMenu2,SIGNAL(triggered(QAction *)),this,SLOT(actionTriggered(QAction *a)));
    To copy to clipboard, switch view to plain text mode 

    should be

    Qt Code:
    1. connect(contextMenu2,SIGNAL(triggered(QAction *)),this,SLOT(actionTriggered(QAction *)));
    To copy to clipboard, switch view to plain text mode 

  7. The following user says thank you to munna for this useful post:

    raphaelf (11th October 2006)

  8. #7
    Join Date
    Jan 2006
    Posts
    273
    Thanks
    42
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: get Text from Popupmenu action

    it works

    Thanks.

    Could you explain why just * and not *a

    Thank you!!!
    Think DigitalGasoline

Similar Threads

  1. Unhandled exception in qatomic
    By NewGuy in forum Qt Programming
    Replies: 14
    Last Post: 23rd July 2013, 09:49
  2. visible text of textedit
    By regix in forum Qt Programming
    Replies: 3
    Last Post: 26th June 2006, 09:02
  3. widget for text AND buttons
    By nongentesimus in forum Newbie
    Replies: 2
    Last Post: 16th June 2006, 13:43
  4. QTextEdit API questions (plain text)
    By Gaspar in forum Qt Programming
    Replies: 4
    Last Post: 16th May 2006, 06:03
  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.