How to set the operations of right click
-
i am developing a browser i want to know how to set the operation of right click of link when we right click a link by default it shows some options how to edit hese options also how to do this for images and other elementts i searched a lot but cant find the anwser
-
You can do this in your QWebView derived class:
@
void WebViewDerivedClass::showCustomContextMenu(const QPoint& a_pos)
{
QMenu* contextMenu = this->page()->createStandardContextMenu();
contextMenu->addAction(m_myCustomAction1);
contextMenu->addAction(m_myCustomAction2);
m_rightClickPos = a_pos;
QPoint globalPosition = mapToGlobal(a_pos);
contextMenu->exec(globalPosition);
delete contextMenu;
}
@
and in Constructor:
@
WebViewDerivedClass::WebViewDerivedClass(QWidget* parent) : QWebView(parent)
{
this->setContextMenuPolicy(Qt::CustomContextMenu);
connect(this, SIGNAL(customContextMenuRequested(const QPoint&)), this, SLOT(showCustomContextMenu(const QPoint&)));
m_myCustomAction1 = new QAction(tr("Do Something"), this);
m_myCustomAction2 = new QAction(tr("Do Something Else"), this);
connect(m_myCustomAction1, SIGNAL (triggered()), this, SLOT(doSomething()));
connect(m_myCustomAction2, SIGNAL (triggered()), this, SLOT(doSomethingElse()));
}
@If you need more specialised behaviour you can install an eventFilter for your WebView and handle the different events. Or maybe you can check what kind of object is at the position of the right-click and depending on this add different actions to the standard context menu.
-
First of all, please use punctuation and try to watch your spelling. Minor spelling mistakes are ok, but your posts are really really hard for the eye. If you want people to help you at least make the effort to write something understandable.
Now to your question. So your problem is that all the functions you want to execute from the context menu are implemented in another class, is that correct ? If so first you should ask yourself if this isn't a problem of your initial design. If there is no way to move this functions to your WebView derived class, you could pass a pointer to the class that has the functions you need to your WebView class and store that as a private member. Ideally this class could be the parent of your WebView, then you won't even need an additional member variable.
-
i did what you said but it still shows the same menu that is by default
@webview::webview(QWidget *parent) :
QWebView(parent)
{
this->setContextMenuPolicy(Qt::CustomContextMenu);
QObject::connect(this , SIGNAL(customContextMenuRequested(QPoint)) , this , SLOT(showmenu(QPoint)));
}
void webview::showmenu(QPoint point)
{
QMenu * menu = this->page()->createStandardContextMenu();
QAction action("hey" ,this);
QPoint globalpos = mapToGlobal(point);
menu->exec(globalpos);
delete menu;
}
@ -
-
here is the new code:
@webview::webview(QWidget *parent) :
QWebView(parent)
{
this->setContextMenuPolicy(Qt::CustomContextMenu);
if(connect(this , SIGNAL(customContextMenuRequested(const QPoint&)) , this , SLOT(showmenu(const QPoint&))) == true)
{
QLabel label("true");
label.show();
}
else
{
QLabel label("false");
label.show();
}
}
void webview::showmenu(const QPoint &point)
{
QMenu * menu = this->page()->createStandardContextMenu();
QAction action("hey" ,this);
QPoint globalpos = mapToGlobal(point);
menu->exec(globalpos);
delete menu;
}
@ -
ahh, no I meant like this:
@
bool test = connect(this , SIGNAL(customContextMenuRequested(const QPoint&)) , this , SLOT(showmenu(const QPoint&));
@
its easier this way. I assume you know how to use a debugger ?But as I see you have found the mistake (having to use the correct parameter const QPoint&). Is your custom context menu displayed now ?
-
This is working. So unless you start making an effort to solve your problems and start giving more precise desciptions of your problems this is my last respond to this thread. I'm done playing "20 questions" with you.
WebViewDerivedClass.h
@
#include <QWebView>
#include <QPoint>class WebViewDerivedClass : public QWebView
{
Q_OBJECT
public:
WebViewDerivedClass(QWidget* pParent = 0);
~WebViewDerivedClass();public slots:
void showCustomContextMenu(const QPoint& a_pos);private:
QAction* m_myCustomAction1;
QAction* m_myCustomAction2;
};
@
WebViewDerivedClass.cpp
@
#include "WebViewDerivedClass.h"
#include <QAction>
#include <QMenu>WebViewDerivedClass::WebViewDerivedClass(QWidget* parent) : QWebView(parent)
{
this->setContextMenuPolicy(Qt::CustomContextMenu);
connect(this, SIGNAL(customContextMenuRequested(const QPoint&)), this, SLOT(showCustomContextMenu(const QPoint&)));
m_myCustomAction1 = new QAction(tr("Do Something"), this);
m_myCustomAction2 = new QAction(tr("Do Something Else"), this);
connect(m_myCustomAction1, SIGNAL (triggered()), this, SLOT(doSomething()));
connect(m_myCustomAction2, SIGNAL (triggered()), this, SLOT(doSomethingElse()));
}WebViewDerivedClass::~WebViewDerivedClass()
{
delete myCustomAction1;
delete myCustomAction2;
}void WebViewDerivedClass::showCustomContextMenu(const QPoint& a_pos)
{
QMenu* contextMenu = this->page()->createStandardContextMenu();
contextMenu->addAction(m_myCustomAction1);
contextMenu->addAction(m_myCustomAction2);
QPoint globalPosition = mapToGlobal(a_pos);
contextMenu->exec(globalPosition);
delete contextMenu;
}
@ -
sorry it dont works
from many days i was busy in finding the soltuin by asking expirementing and solving here is the solution re implement the protected function webview::contextMenuEvent(QContextMenuEvent * event)
then get page of webview and then get frame and then hittestcontent of the pos and get the element and then get the tagname in QString
@void webview::contextMenuEvent(QContextMenuEvent * event)
{
QMenu * menu = new QMenu();
menu->setVisible(true);
QString tagname = this->page()->mainFrame()->hitTestContent(event->pos()).element().tagName().toLower();
if(tagname == "img")
{
QAction * img = new QAction("img" , this);
menu->addAction(img);
}
if(tagname == "a")
{
QAction * link = new QAction("link" , this);
menu->addAction(link);
}
if(tagname == "input")
{
QAction * input = new QAction("input" , this);
menu->addAction(input);
}
if(tagname == "textarea")
{
QAction * textarea = new QAction("text area" , this);
menu->addAction(textarea);
}
menu->exec(event->globalPos());
}
@ -
note the tag name of a link is
means blank
link statement was not working i tried to found out the link tag name
using
@QMessageBox::information(tr("the tag name" , tr("the tage name is %!").arg(tagname))@
remember tag name is
@ QString tagname = this->page()->mainFrame()->hitTestContent(event->pos()).element().tagName().toLower();@