PDA

View Full Version : get Text from Popupmenu action



raphaelf
11th October 2006, 11:52
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?



Q3PopupMenu* contextMenu = new Q3PopupMenu( this);
Q3PopupMenu* contextMenu2 = new Q3PopupMenu( this);

if( Item )
contextMenu->popup( point );

Q_CHECK_PTR( contextMenu );



if((ui.playlistname_cb->currentText() == "") && (ui.listView->childCount() > 0))
{
QSqlQuery select_playlistname("select playlistname from playlistname_tbl");
while(select_playlistname.next())
{
QString playlistname = select_playlistname.value(0).toString();
contextMenu2->setTitle("add to Playlist");

QAction *action = new QAction(playlistname, this);
connect(action,SIGNAL(activated()),this,SLOT(addTo Playlist()));
contextMenu2->addAction(action );
contextMenu->addMenu(contextMenu2);
QMessageBox::information(this,
("Info"),action->text());
}
contextMenu->exec( QCursor::pos() );

return;
}

munna
11th October 2006, 12:41
use

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

raphaelf
11th October 2006, 13:34
Hi :cool:

Thanks for reply..

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

thanks

munna
11th October 2006, 14:00
In the slot you can get the text

Like this:




void YourClass::actionTriggered(QAction *a)
{
QString str = a->text()
}

raphaelf
11th October 2006, 15:08
Hi munna,

If can not get my selected text from the submenu.

Can you see the problem?



connect(ui.listView, SIGNAL( contextMenuRequested( Q3ListViewItem *, const QPoint& , int ) ),
this, SLOT( PlaylistMenu( Q3ListViewItem *, const QPoint &, int ) ) );

contextMenu = new Q3PopupMenu( this);
contextMenu2 = new Q3PopupMenu( this);
connect(contextMenu2,SIGNAL(triggered(QAction *)),this,SLOT(actionTriggered(QAction *a)));

void MainWindow::PlaylistMenu( Q3ListViewItem* Item, const QPoint & point, int )
{

if( Item )
contextMenu->popup( point );
Q_CHECK_PTR( contextMenu );


contextMenu2->clear();
contextMenu->clear();


if((ui.playlistname_cb->currentText() == "") && (ui.listView->childCount() > 0))
{
contextMenu2->setTitle("add to Playlist");
QSqlQuery select_playlistname("select playlistname from playlistname_tbl");
while(select_playlistname.next())
{
QString playlistname = select_playlistname.value(0).toString();
contextMenu2->addAction(playlistname, this, SLOT(addToPlaylist()));
contextMenu->addMenu(contextMenu2);
}
contextMenu->exec( QCursor::pos() );
//delete contextMenu;
//delete contextMenu2;
return;
}

}

void MainWindow::addToPlaylist()
{
QMessageBox::information(this, "", "addToPlaylist");


}
void MainWindow::actionTriggered(QAction *a)
{
QString str = a->text();
QMessageBox::information(this, "", str);
}

munna
11th October 2006, 15:21
connect(contextMenu2,SIGNAL(triggered(QAction *)),this,SLOT(actionTriggered(QAction *a)));


should be



connect(contextMenu2,SIGNAL(triggered(QAction *)),this,SLOT(actionTriggered(QAction *)));

raphaelf
11th October 2006, 15:24
it works :p

Thanks.

Could you explain why just * and not *a :rolleyes:

Thank you!!!