Results 1 to 4 of 4

Thread: ListView Dynamic Popup menu

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

    Default ListView Dynamic Popup menu

    Hi everybody,

    I have a listview. I create a menu for this listview.
    The idea from this popup menu is that the user are able to delete songs from the list OR
    add songs to a playlist.
    But it depends if the user see the playlist or are searching songs..If the user see the playlist,
    so he must be able just to "remove songs from the Playlist".
    If he are searching songs, he should be able just to "add to Playlist". My popup is dynamic.


    My situation now:
    I am not able to call the function actionTriggered() on choosing a action from contextMenu2. The function
    actionTriggered2 will always called.
    I am able to call actionTriggered2() from contextMenu without problem.

    How could i call actionTriggered() by choosesing "add to Playlist". And how could i call actionTriggered2() on choosing
    "remove from Playlist"?
    Thanks

    Qt Code:
    1. contextMenu2 = new Q3PopupMenu( this);
    2. contextMenu2 = new Q3PopupMenu( this);
    3.  
    4.  
    5.  
    6. contextMenu = new Q3PopupMenu( this);
    7. contextMenu2 = new Q3PopupMenu( this);
    8.  
    9.  
    10. connect(contextMenu2,SIGNAL(triggered(QAction *)),this,SLOT(actionTriggered(QAction *)));
    11. connect(contextMenu,SIGNAL(triggered(QAction *)),this,SLOT(actionTriggered2(QAction *)));
    12.  
    13.  
    14. void MainWindow::PlaylistMenu( Q3ListViewItem *Item, const QPoint &point, int)
    15. {
    16. contextMenu2->clear();
    17. contextMenu->clear();
    18.  
    19. if( Item )
    20. contextMenu->popup( point );
    21. Q_CHECK_PTR( contextMenu );
    22.  
    23.  
    24. //Die Playlist Funktionen sollen nur angezeigt werden, wenn auch
    25. //eine Playlist gewählt wurde und auch einen song vorhanden ist
    26. if((ui.playlistname_cb->currentText() != "") && (ui.listView->childCount() > 0))
    27. {
    28. contextMenu->insertItem( "&remove from Playlist");
    29. contextMenu->exec( QCursor::pos() );
    30. //delete contextMenu;
    31. return;
    32. }
    33. if((ui.playlistname_cb->currentText() == "") && (ui.listView->childCount() > 0))
    34. {
    35. contextMenu2->setTitle("add to Playlist");
    36. int items = ui.playlistname_cb->count();
    37. for(int i = 1; i < items; i++)
    38. {
    39. QString playlistname = ui.playlistname_cb->itemText(i);
    40. contextMenu2->addAction(playlistname);
    41. contextMenu->addMenu(contextMenu2);
    42. }
    43. contextMenu->exec( QCursor::pos() );
    44. //delete contextMenu;
    45. //delete contextMenu2;
    46. return;
    47. }
    48. }
    49.  
    50.  
    51. void MainWindow::actionTriggered(QAction *a)
    52. {
    53. QString playlistname = a->text();
    54. addToPlaylist(&playlistname);
    55. }
    56.  
    57. void MainWindow::actionTriggered2(QAction *a)
    58. {
    59. QString songname = (ui.listView->currentItem() )->text( 0 );
    60. QString artist = (ui.listView->currentItem() )->text( 1 );
    61. switch( QMessageBox::warning( this, "DSM",
    62. "Remove " + songname + " of " + artist + " from Playlist "
    63. + ui.playlistname_cb->currentText() + " ?\n\n",
    64. "Yes",
    65. "No", 0, 0, 1 ) )
    66. {
    67. case 1:
    68. break;
    69. case 0:
    70. //Song aus der aktuelle Playlist löschen
    71. removeFromPlaylist();
    72. //Aktualisieren
    73. getPlaylist();
    74. }
    75. }
    To copy to clipboard, switch view to plain text mode 
    Think DigitalGasoline

  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: ListView Dynamic Popup menu

    First of all I suggest you stopped using the Q3Support library (Q3PopupMenu - why not use QMenu?).

    I see you misunderstand the concept of actions and menus. I'd suggest something like this (this is not a complete code!):
    Qt Code:
    1. class MyClass : public ... {
    2. //...
    3. private:
    4. QAction *m_addAction;
    5. QAction *m_removeAction;
    6. };
    7.  
    8. MyClass::MyClass() {
    9. //...
    10. m_addAction = new QAction("Add to playlist", this);
    11. m_removeAction = new QAction("Remove from playlist", this);
    12. connect(m_addAction, SIGNAL(triggered()), this, SLOT(addToPlaylist()));
    13. connect(m_removeAction, SIGNAL(triggered()), this, SLOT(removeFromPlaylist()));
    14. //...
    15. }
    16.  
    17. void MyClass::playListMenu(QPoint &pt){
    18. QMenu menu;
    19. if(shouldAddToPlaylist()){
    20. menu.addAction(m_addAction);
    21. } else {
    22. menu.addAction(m_removeAction);
    23. }
    24. menu.exec(pt);
    25. }
    To copy to clipboard, switch view to plain text mode 
    You can also add both actions and simply disable or hide the one you don't want. You'll get cleaner code then. Although for the QListView I'd use QWidget::customContextMenuRequested( const QPoint & pos ) signal and QAbstractItemView::indexAt() to fetch the item under cursor, but I don't know what you're exactly doing so some other approach might be better.

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

    Default Re: ListView Dynamic Popup menu

    Hi wysota

    Thank you very much for your example..now i now how and where to use QAction and QMenu

    The only thing i was not able to do is to insert a submenu in the menu.
    If the user would like to insert song to a playlist, he should choose which playlist. I was able to fill the submenu with all playlists. But how could i make this:
    add to playlist->Playlist1
    Playlist2
    Playlist3
    If the user choose a playlist, i have to call everytime this function: "addToPlaylist()"

    Qt Code:
    1. m_addAction = new QAction("Add to playlist", this);
    2. m_removeAction = new QAction("Remove from playlist", this);
    3. connect(m_addAction, SIGNAL(triggered()), this, SLOT(addToPlaylist()));
    4. connect(m_removeAction, SIGNAL(triggered()), this, SLOT(removeFromPlaylist()));
    5. .
    6. .
    7.  
    8. QMenu menu;
    9. QMenu submenu;
    10. if((ui.playlistname_cb->currentText() != "") && (ui.listView->childCount() > 0)){
    11. menu.addAction(m_removeAction);
    12. //getPlaylist();
    13.  
    14. }
    15. if((ui.playlistname_cb->currentText() == "") && (ui.listView->childCount() > 0)){
    16. menu.addAction(m_addAction);
    17. int items = ui.playlistname_cb->count();
    18. for(int i = 1; i < items; i++)
    19. {
    20. QString playlistname = ui.playlistname_cb->itemText(i);
    21. submenu.addMenu(playlistname);
    22. //menu.addAction(submenu);
    23. }
    24.  
    25.  
    26. }
    27. menu.exec(point);
    To copy to clipboard, switch view to plain text mode 
    Think DigitalGasoline

  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: ListView Dynamic Popup menu

    Qt Code:
    1. QMenu topmenu;
    2. topmenu.addAction(action1);
    3. QMenu submenu;
    4. submenu.addAction(action2);
    5. topmenu.addMenu(&submenu);
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Can't close my popup
    By Jimmy2775 in forum Qt Programming
    Replies: 2
    Last Post: 11th July 2006, 09:10
  2. Background image on popup menu item
    By MarkoSan in forum Qt for Embedded and Mobile
    Replies: 1
    Last Post: 27th June 2006, 05:55
  3. Replies: 6
    Last Post: 14th April 2006, 05:39
  4. Tracking separators in a menu (insertSeparator)
    By PrimeCP in forum Qt Programming
    Replies: 4
    Last Post: 25th January 2006, 18:10

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.