Go back to Richel Bilderbeek's homepage.

Go back to Richel Bilderbeek's C++ page.

 

 

 

 

 

(C++) QtExample17

 

QtQt CreatorLubuntuUbuntu

 

This Qt example shows how to modify a QLabel to make it clickable, resulting in a traffic light, like this screenshot (png).

 

Technical facts

 

Application type(s)

Operating system(s) or programming environment(s)

IDE(s):

Project type:

C++ standard:

Compiler(s):

Libraries used:

 

 

 

 

 

Qt project file: ./CppQtExample17/CppQtExample17.pro

 

exists(../../DesktopApplication.pri) {
  include(../../DesktopApplication.pri)
}
!exists(../../DesktopApplication.pri) {
  QT += core gui
  greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
  TEMPLATE = app

  CONFIG(debug, debug|release) {
    message(Debug mode)
  }

  CONFIG(release, debug|release) {
    message(Release mode)
    DEFINES += NDEBUG NTRACE_BILDERBIKKEL
  }

  QMAKE_CXXFLAGS += -std=c++11 -Wall -Wextra

  unix {
    QMAKE_CXXFLAGS += -Werror
  }
}

exists(../../Libraries/Boost.pri) {
  include(../../Libraries/Boost.pri)
}
!exists(../../Libraries/Boost.pri) {
  win32 {
    INCLUDEPATH += \
      ../../../Projects/Libraries/boost_1_55_0
  }
}

SOURCES += main.cpp

SOURCES += \
    trafficlightdialog.cpp \
    image.cpp

HEADERS  += \
    trafficlightdialog.h \
    image.h

FORMS    += trafficlightdialog.ui

RESOURCES += resources.qrc

 

 

 

 

 

./CppQtExample17/image.h

 

#ifndef IMAGE_H
#define IMAGE_H

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Weffc++"
#pragma GCC diagnostic ignored "-Wunused-local-typedefs"
#include <QColor>
#include <QLabel>
#include <QObject>
#pragma GCC diagnostic pop

struct QColor;
struct QMouseEvent;

class Image : public QLabel
{
  Q_OBJECT

public:
    Image(const QString& filename);
    void mousePressEvent( QMouseEvent * e);
signals:
    void onClick();
};

#endif // IMAGE_H

 

 

 

 

 

./CppQtExample17/image.cpp

 

#include "image.h"

#include <cassert>
#include <iostream>

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Weffc++"
#pragma GCC diagnostic ignored "-Wunused-local-typedefs"

#include <QBitmap>
#include <QColor>
#include <QImage>
#include <QPixmap>
#include <QMouseEvent>
#pragma GCC diagnostic pop

Image::Image(const QString& filename)
{
  //MouseTracking must be set to true, to make Image clickable
  this->setMouseTracking(true);
  //Set the image
  QPixmap pixmap(filename);
  std::clog << "Using pixmap file '" << filename.toStdString() << "'\n";
  assert(!pixmap.isNull());
  //Set transparency (white, in this example)
  const QBitmap mask = pixmap.createMaskFromColor(QColor(255,255,255,0).rgb());
  pixmap.setMask(mask);
  this->setPixmap(pixmap);
}

void Image::mousePressEvent( QMouseEvent *)
{
  std::clog << "emitting onClick()\n";
  emit onClick();
}

 

 

 

 

 

./CppQtExample17/main.cpp

 

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Weffc++"
#pragma GCC diagnostic ignored "-Wunused-local-typedefs"
#include <QApplication>
#include "trafficlightdialog.h"
#pragma GCC diagnostic pop

int main(int argc, char *argv[])
{
  QApplication a(argc, argv);
  TrafficLightDialog w;
  w.show();
  return a.exec();
}

 

 

 

 

 

./CppQtExample17/trafficlightdialog.h

 

#ifndef TRAFFICLIGHTDIALOG_H
#define TRAFFICLIGHTDIALOG_H

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Weffc++"
#pragma GCC diagnostic ignored "-Wunused-local-typedefs"
#include <boost/shared_ptr.hpp>
#include <QDialog>
#pragma GCC diagnostic pop

struct Image;

namespace Ui {
  class TrafficLightDialog;
}

class TrafficLightDialog : public QDialog
{
  Q_OBJECT

public:
  explicit TrafficLightDialog(QWidget *parent = 0);
  TrafficLightDialog(const TrafficLightDialog&) = delete;
  TrafficLightDialog& operator=(const TrafficLightDialog&) = delete;
  ~TrafficLightDialog();

private:
  Ui::TrafficLightDialog *ui;
  //Images on the screen
  boost::shared_ptr<Image> m_red;
  boost::shared_ptr<Image> m_orange;
  boost::shared_ptr<Image> m_green;
  //Read-only off-screen images
  boost::shared_ptr<const Image> m_red_on;
  boost::shared_ptr<const Image> m_orange_on;
  boost::shared_ptr<const Image> m_green_on;
  boost::shared_ptr<const Image> m_red_off;
  boost::shared_ptr<const Image> m_orange_off;
  boost::shared_ptr<const Image> m_green_off;
private slots:
  void onRedClicked();
  void onOrangeClicked();
  void onGreenClicked();
};

#endif // TRAFFICLIGHTDIALOG_H

 

 

 

 

 

./CppQtExample17/trafficlightdialog.cpp

 

#include "trafficlightdialog.h"

#include <iostream>

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Weffc++"
#pragma GCC diagnostic ignored "-Wunused-local-typedefs"
#include <QVBoxLayout>
#include "ui_trafficlightdialog.h"
#include "image.h"
#pragma GCC diagnostic pop

TrafficLightDialog::TrafficLightDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::TrafficLightDialog),
    m_red(new Image(":/images/RedOff64x64.png")),
    m_orange(new Image(":/images/OrangeOff64x64.png")),
    m_green(new Image(":/images/GreenOff64x64.png")),
    m_red_on(new Image(":/images/Red64x64.png")),
    m_orange_on(new Image(":/images/Orange64x64.png")),
    m_green_on(new Image(":/images/Green64x64.png")),
    m_red_off(new Image(":/images/RedOff64x64.png")),
    m_orange_off(new Image(":/images/OrangeOff64x64.png")),
    m_green_off(new Image(":/images/GreenOff64x64.png"))
    //m_red_on(false),
    //m_orange_on(false),
    //m_green_on(false)

{
  ui->setupUi(this);

  m_red->setAlignment(Qt::AlignHCenter);
  m_orange->setAlignment(Qt::AlignHCenter);
  m_green->setAlignment(Qt::AlignHCenter);
  QVBoxLayout * layout = new QVBoxLayout(this);

  layout->addWidget(m_red.get());
  layout->addWidget(m_orange.get());
  layout->addWidget(m_green.get());

  m_red->connect(m_red.get(),SIGNAL(onClick()),this,SLOT(onRedClicked()));
  m_red->connect(m_orange.get(),SIGNAL(onClick()),this,SLOT(onOrangeClicked()));
  m_red->connect(m_green.get(),SIGNAL(onClick()),this,SLOT(onGreenClicked()));

}

TrafficLightDialog::~TrafficLightDialog()
{
  delete ui;
}

void TrafficLightDialog::onRedClicked()
{
  std::clog << "Red clicked\n";
  m_red->setPixmap(*m_red_on->pixmap());
  m_orange->setPixmap(*m_orange_off->pixmap());
  m_green->setPixmap(*m_green_off->pixmap());
}

void TrafficLightDialog::onOrangeClicked()
{
  std::clog << "Orange clicked\n";
  m_red->setPixmap(*m_red_off->pixmap());
  m_orange->setPixmap(*m_orange_on->pixmap());
  m_green->setPixmap(*m_green_off->pixmap());
}

void TrafficLightDialog::onGreenClicked()
{
  std::clog << "Green clicked\n";
  m_red->setPixmap(*m_red_off->pixmap());
  m_orange->setPixmap(*m_orange_off->pixmap());
  m_green->setPixmap(*m_green_on->pixmap());
}

 

 

 

 

 

Go back to Richel Bilderbeek's C++ page.

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict

This page has been created by the tool CodeToHtml