PDA

View Full Version : Contextmenu in Qtreeview



LordQt
16th February 2009, 14:40
Hello friends,

I have create an actions like this


myAction = new QAction(QIcon(":/IMAGES/error.png"),tr("&TestAction"), this);
connect(myAction, SIGNAL(triggered()), this, SLOT(openFileInTable( const QModelIndex & )));

Before creating my action I make this:



trackFolder->setContextMenuPolicy(Qt::CustomContextMenu);
connect(trackFolder, SIGNAL(customContextMenuRequested(const QPoint &)),
this, SLOT(showContextMenu(const QPoint &)));

My contextmenu function is(trackfolder is a Qtreeview) :


void Window::showContextMenu(const QPoint &position)
{

QList<QAction *> actions;
if (trackFolder->indexAt(position).isValid()) {
actions.append(myAction);
}
if (actions.count() > 0)
QMenu::exec(actions, trackFolder->mapToGlobal(position));
}

So when I rightclick an item it shows me correctly the item, but the given function does not enter. But when I replace

....SLOT(openFileInTable( const QModelIndex & )));
with the normal quit function like this

....SLOT(quit()));

the Application quits normally.

What do I wrong. My Slot in the contextmenu that must be run is declared like this:


void openFileInTable(const QModelIndex & index);


How can I run my slot in the contextmenu???

spud
16th February 2009, 16:39
You cannot connect
void triggered() with
void openFileInTable( const QModelIndex & ) because the functions have different signatures. You have to scrap the "const QModelIndex &" argument.

One solution would be to apply the selected action on the selected item(s) in the model.