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. Link - middle button click
QtWS25 Last Chance

Link - middle button click

Scheduled Pinned Locked Moved Qt WebKit
2 Posts 2 Posters 2.4k 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.
  • T Offline
    T Offline
    tomko222
    wrote on last edited by
    #1

    I am trying to write a simple tab browser and everything is working ok but I have problem in implementing opening link in tab by clicking on it with the middle mouse button. I don't know in what way I should check if link was clicked by left button or middle button?

    What I have already tried:
    I have set
    @
    webView->page()->setLinkDelegationPolicy(QWebPage::DelegateAllLinks);
    @
    Then I used linkClicked signal
    @
    connect(webView->page(), SIGNAL(linkClicked(QUrl)), this, SLOT(linkClickedSlot(QUrl)));
    @
    but in this slot I don't know how to check what button was clicked. So I thought to reimplement mousePressEvent in QWebView:
    @
    void WebView::mousePressEvent(QMouseEvent *event)
    {
    //checking what mouse button was clicked (left, middle, right)
    if(event->button() == Qt::LeftButton)
    {
    lastClickedMouseButton = 0;//left 0
    }else if(event->button() == Qt::MidButton)
    {
    lastClickedMouseButton = 1;//middle 1
    }else if(event->button() == Qt::RightButton)
    {
    lastClickedMouseButton = 2;//right 2
    }
    }
    @
    and then check lastClickedMouseButton value in linkClickedSlot
    @
    void Tab::linkClickedSlot(QUrl url)
    {
    if(webView->lastClickedMouseButton == 0)//left button
    {
    qDebug("left button clicked");
    webView->setUrl(url);//opening clicked link in this page

    }else if(webView->lastClickedMouseButton == 1)//middle button
    {
        qDebug("middle button clicked");
        //creating new tab with clicked link
        //will be added later
    }else if(webView->lastClickedMouseButton == 2)//right button
    {
        qDebug("right button clicked");
    }else
    {
        qDebug("unknown button clicked");
    }
    

    }
    @
    And it is more-less working - when I click once it is not working but it is working when I click fast few times... How should I check if the left or middle button was clicked in the correct way?

    1 Reply Last reply
    0
    • raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

      no since QtWebkit drops the mousebutton/mouseevent information at some point and doesn't pass them back through the Qt API.
      So your way with storing the last pressed mousebutton is the only solution i would say.

      But instead connecting to the linkedClicked signal i recommend reimplementing of QWebPage::acceptNavigationRequest():
      @
      bool MyWebPage::acceptNavigationRequest(QWebFrame * frame, const QNetworkRequest & request, QWebPage::NavigationType type)
      {
      if( type == QWebPage::NavigationTypeLinkClicked && lastClickedMouseButton == 1 ) //middle mouse button was pressed
      {
      emit openUrlInNewTab( request->url() ); //open new tab with url
      return false;
      }
      else
      return QWebPage::acceptNavigationRequest(frame, request, type);
      }
      @

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      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