(C++) How to implement a clickable QWidget?
January 11, 2018 · View on GitHub
(C++)
How to implement a clickable QWidget?
FAQ for if you want to use QWidget to display clickable images.
The trick is to override 'mousePressEvent' and use the QWidget's geometry() member function:
void Dialog::mousePressEvent(QMouseEvent * event) { if (ui->widget->geometry().contains( event->x(), event->y())) { //Do something } }
The full code can be viewed and downloaded below.
The sprites used in this example are from a game of mine, called Maziak.
Project and source code
Operating system: Ubuntu 10.04 LTS Lucid Lynx
IDE: Qt Creator 2.0.0
Project type: Qt4 GUI Application
Libraries used:
- Qt: version 4.7.0 (32 bit)
- View a screenshot of 'CppQtClickableQWidget' (png)
- Download the C++ Qt Creator project 'CppQtClickableQWidget' (zip)
CppQtClickableQWidget.pro
#------------------------------------------------- # # Project created by QtCreator 2010-07-31T12:04:49 # #------------------------------------------------- QT += core gui TARGET = CppQtClickableQWidget TEMPLATE = app SOURCES += main.cpp\ dialog.cpp HEADERS += dialog.h FORMS += dialog.ui RESOURCES += \ resources.qrc
dialog.h
#ifndef DIALOG_H #define DIALOG_H #include <QDialog> struct QMouseEvent; namespace Ui { class Dialog; } class Dialog : public QDialog { Q_OBJECT public: explicit Dialog(QWidget *parent = 0); ~Dialog(); protected: void changeEvent(QEvent *e); private: Ui::Dialog *ui; void mousePressEvent(QMouseEvent* event); void paintEvent(QPaintEvent * event); bool m_show_1st; }; #endif // DIALOG_H
dialog.cpp
#include <QMouseEvent> #include <QPainter> #include "dialog.h" #include "ui_dialog.h" Dialog::Dialog(QWidget *parent) : QDialog(parent), ui(new Ui::Dialog), m_show_1st(true) { ui->setupUi(this); } Dialog::~Dialog() { delete ui; } void Dialog::changeEvent(QEvent *e) { QDialog::changeEvent(e); switch (e->type()) { case QEvent::LanguageChange: ui->retranslateUi(this); break; default: break; } } void Dialog::paintEvent(QPaintEvent*) { QPainter painter(this); QPixmap pixmap(m_show_1st ? ":/images/PlayerWon1.png" : ":/images/PlayerScared.png"); painter.drawPixmap(ui->widget->geometry(),pixmap); } void Dialog::mousePressEvent(QMouseEvent * event) { if (ui->widget->geometry().contains( event->x(), event->y())) { m_show_1st = !m_show_1st; repaint(); } }
main.cpp
#include <QtGui/QApplication> #include "dialog.h" int main(int argc, char *argv[]) { QApplication a(argc, argv); Dialog w; w.show(); return a.exec(); }