OpenUrl from a QGraphicsView [Solved]



  • I have used Creater to create a QGraphicsView where I place my Logo. What I want to do is to have the web browser opened and the url "www.cnn.com" opened when the user clocks on this logo. Below is the code I use to load the Logo image from disk to the QGraphicsView. From research it looks like I need to override the mouseUp event or something like that but I am having problems trying to figure out how to over ride this when I have created the QGraphicsView in creator.

    @ QPixmap pixmap;

    imageLoaded = pixmap.load(QString(":/new/prefix1/my_logo1.png"));
    
    QGraphicsScene *gs = new QGraphicsScene;
    QRect rect = ui->graphicsLogoView->geometry();
    gs->addPixmap(pixmap.scaledToWidth(rect.width()-10));
    
    
    ui->graphicsLogoView->setScene(gs);
    ui->graphicsLogoView->show();
    ui->graphicsLogoView->setStyleSheet("background: transparent");
    

    @



  • You can use standard QPushButton for this. Add it in designer. And then:
    @pushButton->setStyleSheet("QPushButton {border: none; padding: 0px;}");
    pushButton->setIcon(QIcon(":/new/prefix1/my_logo1.png"));@

    Then, in designer, create slot clicked() for this button and in this slot write:
    @QDesktopServices::openUrl(QUrl("www.cnn.com", QUrl::TolerantMode));@

    PS: you also need to add:
    @#include <QDesktopServices>@



  • To do this in QGraphicsView you can subclass QGraphicsObject and QGraphicsPixmapItem and declare a clicked() signal.

    Override the mousePressEvent() and check to see if it was the left mouse button which was pressed. If so emit your clicked signal.

    Then in your mainwindow or somewhere else convenient connect to your custom item's clicked() signal to a slot and from there call QDesktopServices::openUrl().



  • Thanks, I switched to using a button and worked fine. Thanks.


Log in to reply
 

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