How to set the operations of right click
-
No you did not do exactly what I did. Try and find out for yourself what is different. Hint: test if the connect(..) in the constructor returns true or false. And if its false (which it will be) find out why its not connecting the Signal to your Slot.
-
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;
}
@ -
it neither shows true or false i can get it why
-
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 ?
-
no my menu is not deisplying i dont know why
-
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());
}
@ -
i have to remove the line
@ this->setContextMenuPolicy(Qt::CustomContextMenu);@
any ways thanks for helping me -
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();@ -
I'm glad you got it up and running. Sorry I didn't answer, I was sick last week.
-
no problem