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.



  • thnaks for the reply
    so currently i have not created a derived QWebView class but i think now i need to do so
    ok so i have to create my own class derived from QWebView then instaed i have to use the new class
    when making a web view page



  • hey sorry but this i quiet difficult as i cant acces the functions of toher classes for which i am doing this i know i can use the class a parameter but it is reall complicated a butter way will be preffered



  • 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.



  • thanks man i am going to do this sorry for my english



  • i tired you solution above but it does not works can you tell whati should do now



  • "it doesn't work" is not a very helpful piece of information. Can you please elaborate on your problems a little more.
    I use the exact same code in one of my applications and its working fine.



  • 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;
    }
    @



  • 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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.