Right Click Context Menu for QTextBrowser

3k Views Asked by At

I'm trying to add a custom contextMenu for the QTextBrowser that i have in my Qt application, but it doesn't seem to work.

I'm using the steps explained in the following link defined for the Element QLineEdit, however it doesn't work.

extend-the-standard-context-menu-of-qtextedit

EDIT:

The following code i wrote in the constructor of the MainWindow:

QDockWidget *dock = new QDockWidget(tr("Text View"), this);
txtBrwsr = new QTextBrowser(this);
dock->setWidget(txtBrwsr);

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

The following is the implementation of the showContextMenu function:

void AMTMainWindow::showContextMenu(const QPoint &pt) {

    QMenu * menu = txtBrwsr->createStandardContextMenu();
    QMenu * tags;
    tags = menu->addMenu(tr("&Tag"));
    for(int i=0; i<_atagger->tagTypeVector->count(); i++) {
        QAction * taginstance;
        char * tagValue = (_atagger->tagTypeVector->at(i)).tag.toLocal8Bit().data();
        taginstance = new QAction(tr(tagValue), this);
        connect(taginstance, SIGNAL(triggered()), this, SLOT(tag(tagValue)));
        tags->addAction(taginstance);
    }
    menu->addAction(untagAct);
    menu->addAction(addtagAct);
    menu->exec(txtBrwsr->mapToGlobal(pt));
    delete menu;
}

After some debugging, i found out that the showContextMenu function is not even triggered when i press the right click which should open the context menu. So i think the main problem is in the connect function.

Any help is appreciated.

1

There are 1 best solutions below

0
On

you delete menu just after you created it here:

[...]
menu->addAction(addtagAct);
menu->exec(txtBrwsr->mapToGlobal(pt));

delete menu;