Skip to content

Instantly share code, notes, and snippets.

@cestrand
Created August 16, 2015 10:32
Show Gist options
  • Save cestrand/258bf18da2f32384d955 to your computer and use it in GitHub Desktop.
Save cestrand/258bf18da2f32384d955 to your computer and use it in GitHub Desktop.
Add clickable image banner in QToolBar
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent), ui(new Ui::MainWindow)
{
// somewhere after ui->setupUi(this);
ui->mainToolBar->addWidget(spacer);
ui->mainToolBar->addWidget(new MyClickableBanner(this));
// ...
}
#include "myclickablebanner.h"
MyClickableBanner::MyClickableBanner(QWidget *parent) :
QLabel(parent)
{
pixmap = new QPixmap(":/images/banner.png");
url = new QUrl("http://google.com");
setPixmap(*pixmap);
}
bool MyClickableBanner::event(QEvent *e)
{
if(e->type() == QEvent::Enter) {
QApplication::setOverrideCursor(QCursor(Qt::PointingHandCursor));
}
else if(e->type() == QEvent::Leave) {
QApplication::restoreOverrideCursor();
}
else if(e->type() == QEvent::MouseButtonPress) {
QDesktopServices::openUrl(*url);
}
QLabel::event(e);
}
#ifndef MYCLICKABLEBANNER_H
#define MYCLICKABLEBANNER_H
#include <QLabel>
#include <QUrl>
#include <QEvent>
#include <QApplication>
#include <QDesktopServices>
class MyClickableBanner : public QLabel
{
Q_OBJECT
public:
explicit MyClickableBanner(QWidget *parent = 0);
private:
QPixmap* pixmap;
QUrl* url;
bool cursorOverridden = false;
protected:
bool event(QEvent *e);
};
#endif // MYCLICKABLEBANNER_H
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment