PDA

View Full Version : extend the standard context menu of qtextedit



cai
18th October 2010, 10:02
Hi all,
when i extend the standard context menu of qtextedit, i found all the actions is disable except the "paste" and "clear". why?


QMenu *editContextMenu = ui->textEdit->createStandardContextMenu();
editContextMenu->addAction(clear);
ui->textEdit->setContextMenuPolicy(Qt::CustomContextMenu);
...
editContextMenu->exec(QCursor::pos());

wysota
18th October 2010, 10:55
Some actions are enabled only in some contexts and not in others. Try selecting some text and then bring up the context menu.

cai
18th October 2010, 16:36
Hi wysota,
I have input something in the textedit, and then select them by mouse(because the "select all" action is disable), but the cut and copy actions are still disable.
If i don't extend the standard context menu, everything works well.

wysota
18th October 2010, 16:50
Wait a moment, where did you put the code snippet you pasted in the first post?

cai
18th October 2010, 18:03
Wait a moment, where did you put the code snippet you pasted in the first post?

Hi wysota,
The example is like this:


MainWindow::MainWindow()
{
...
createContextMenu()
connect(ui->textEdit,SIGNAL(customContextMenuRequest(const QPoint&)),this,SLOT(showContextMenu()));
...
}

void MainWindow::createContextMenu()
{
editContextMenu = ui->textEdit->createStandardContextMenu();
editContextMenu->addAction(clear);
ui->textEdit->setContextMenuPolicy(Qt::CustomContextMenu);
}

void MainWindow::showContextMenu()
{
if(editContextMenu)
editContextMenu ->exec(QCursor::pos());
}


Thanks for your reply:)

wysota
18th October 2010, 18:30
It should be:

MainWindow::MainWindow()
{
ui->textEdit->setContextMenuPolicy(Qt::CustomContextMenu);
connect(ui->textEdit,SIGNAL(customContextMenuRequest(const QPoint&)),
this,SLOT(showContextMenu(const QPoint &)));
}

void MainWindow::showContextMenu(const QPoint &pt)
{
QMenu *menu = ui->textEdit->createStandardContextMenu();
menu->addAction(clear);
menu->exec(ui->textEdit->mapToGlobal(pt));
delete menu;
}

cai
19th October 2010, 02:04
It should be:

MainWindow::MainWindow()
{
ui->textEdit->setContextMenuPolicy(Qt::CustomContextMenu);
connect(ui->textEdit,SIGNAL(customContextMenuRequest(const QPoint&)),
this,SLOT(showContextMenu(const QPoint &)));
}

void MainWindow::showContextMenu(const QPoint &pt)
{
QMenu *menu = ui->textEdit->createStandardContextMenu();
menu->addAction(clear);
menu->exec(ui->textEdit->mapToGlobal(pt));
delete menu;
}

Hi wysota,
Yes, it works well. But i still don't know why? Can you tell me?
thanks.

wysota
19th October 2010, 09:05
You were "pre-creating" the context menu and trying to reuse it with each context menu request but the menu and its actions are created based on the current state of the editor (the actions are not updated), so they didn't reflect any changes (such as selecting text) that happened after you called createStandardContextMenu().

cai
19th October 2010, 11:52
You were "pre-creating" the context menu and trying to reuse it with each context menu request but the menu and its actions are created based on the current state of the editor (the actions are not updated), so they didn't reflect any changes (such as selecting text) that happened after you called createStandardContextMenu().

I get it. Thanks for your help!

oberlus
10th November 2011, 12:16
Seems customContextMenuRequest has now been changed to customContextMenuRequested