Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt WebKit
  4. How to set the operations of right click
QtWS25 Last Chance

How to set the operations of right click

Scheduled Pinned Locked Moved Qt WebKit
20 Posts 2 Posters 10.9k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    developer
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • K Offline
      K Offline
      KA51O
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • D Offline
        D Offline
        developer
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        • D Offline
          D Offline
          developer
          wrote on last edited by
          #4

          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

          1 Reply Last reply
          0
          • K Offline
            K Offline
            KA51O
            wrote on last edited by
            #5

            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.

            1 Reply Last reply
            0
            • D Offline
              D Offline
              developer
              wrote on last edited by
              #6

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

              1 Reply Last reply
              0
              • D Offline
                D Offline
                developer
                wrote on last edited by
                #7

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

                1 Reply Last reply
                0
                • K Offline
                  K Offline
                  KA51O
                  wrote on last edited by
                  #8

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

                  1 Reply Last reply
                  0
                  • D Offline
                    D Offline
                    developer
                    wrote on last edited by
                    #9

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

                    1 Reply Last reply
                    0
                    • K Offline
                      K Offline
                      KA51O
                      wrote on last edited by
                      #10

                      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.

                      1 Reply Last reply
                      0
                      • D Offline
                        D Offline
                        developer
                        wrote on last edited by
                        #11

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

                        1 Reply Last reply
                        0
                        • D Offline
                          D Offline
                          developer
                          wrote on last edited by
                          #12

                          it neither shows true or false i can get it why

                          1 Reply Last reply
                          0
                          • K Offline
                            K Offline
                            KA51O
                            wrote on last edited by
                            #13

                            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 ?

                            1 Reply Last reply
                            0
                            • D Offline
                              D Offline
                              developer
                              wrote on last edited by
                              #14

                              no my menu is not deisplying i dont know why

                              1 Reply Last reply
                              0
                              • K Offline
                                K Offline
                                KA51O
                                wrote on last edited by
                                #15

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

                                1 Reply Last reply
                                0
                                • D Offline
                                  D Offline
                                  developer
                                  wrote on last edited by
                                  #16

                                  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());
                                  }
                                  @

                                  1 Reply Last reply
                                  0
                                  • D Offline
                                    D Offline
                                    developer
                                    wrote on last edited by
                                    #17

                                    i have to remove the line
                                    @ this->setContextMenuPolicy(Qt::CustomContextMenu);@
                                    any ways thanks for helping me

                                    1 Reply Last reply
                                    0
                                    • D Offline
                                      D Offline
                                      developer
                                      wrote on last edited by
                                      #18

                                      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();@

                                      1 Reply Last reply
                                      0
                                      • K Offline
                                        K Offline
                                        KA51O
                                        wrote on last edited by
                                        #19

                                        I'm glad you got it up and running. Sorry I didn't answer, I was sick last week.

                                        1 Reply Last reply
                                        0
                                        • D Offline
                                          D Offline
                                          developer
                                          wrote on last edited by
                                          #20

                                          no problem

                                          1 Reply Last reply
                                          0

                                          • Login

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • Users
                                          • Groups
                                          • Search
                                          • Get Qt Extensions
                                          • Unsolved