PDA

View Full Version : QLineEdit contextMenu



^NyAw^
17th May 2007, 10:53
Hi,

I have a class inherited from QLineEdit and I want to show a custom context Menu.



MyLineEdit::MyLineEdit(QWidget *parent) : QLineEdit(parent)
{
ui.setupUi(this);
this->setContextMenuPolicy(Qt::CustomContextMenu);
}

void MyLineEdit::contextMenuEvent(QContextMenuEvent *event)
{
//Never enters here
QMenu* pqMenu = new QMenu();
pqMenu->addAction(tr("Firts"));
pqMenu->addAction(tr("Second"));
pqMenu->exec(event->globalPos());
delete (pqMenu);
}


In the header file I have:


protected:
void contextMenuEvent(QContextMenuEvent *event);


Anyone knows what is the problem?

Thanks,

^NyAw^
17th May 2007, 10:58
Hi

Sorry, I don't undersand why, but I have recompiled all the project and now it works.
;)

marcel
17th May 2007, 11:01
use instead QLineEdit::createStandardContextMenu().
Also, make sure contextMenuPolicy is not set to Qt::NoContextMenu.

regards

marcel
17th May 2007, 11:03
Hi

Sorry, I don't undersand why, but I have recompiled all the project and now it works.
;)

Yes, but using the solution I posted is more efficient because you create the menu only once, not every time you show it.

You solution is good if tou want to customize the menu according to some runt-time options ( maybe add or remove items from it ), etc.

^NyAw^
17th May 2007, 11:15
Hi,

Ok, will try it. Thanks for your idea