October 29, 2011

justdad justdad
Lab Rat
20 posts

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.

  1.     QPixmap pixmap;
  2.      
  3.     imageLoaded = pixmap.load(QString(":/new/prefix1/my_logo1.png"));
  4.  
  5.     QGraphicsScene *gs = new QGraphicsScene;
  6.     QRect rect = ui->graphicsLogoView->geometry();
  7.     gs->addPixmap(pixmap.scaledToWidth(rect.width()-10));
  8.  
  9.  
  10.     ui->graphicsLogoView->setScene(gs);
  11.     ui->graphicsLogoView->show();
  12.     ui->graphicsLogoView->setStyleSheet("background: transparent");

3 replies

October 30, 2011

RazrFalcon RazrFalcon
Lab Rat
114 posts

You can use standard QPushButton for this. Add it in designer. And then:

  1. pushButton->setStyleSheet("QPushButton {border: none; padding: 0px;}");
  2. pushButton->setIcon(QIcon(":/new/prefix1/my_logo1.png"));

Then, in designer, create slot clicked() for this button and in this slot write:

  1. QDesktopServices::openUrl(QUrl("www.cnn.com", QUrl::TolerantMode));

PS: you also need to add:

  1. #include <QDesktopServices>

 Signature 

QT != Qt
Gentoo + KDE

October 31, 2011

ZapB ZapB
Robot Herder
1355 posts

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().

 Signature 

Nokia Certified Qt Specialist
Interested in hearing about Qt related work

November 13, 2011

justdad justdad
Lab Rat
20 posts

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

 
  ‹‹ Security issue with compiled application      direct access to FAT sectors using Qt ››

You must log in to post a reply. Not a member yet? Register here!