Link - middle button click



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


  • Moderators

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


Log in to reply
 

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