Go back to Richel Bilderbeek's homepage.

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

 

 

 

 

 

(C++) QtDisplayPosItem

 

QtQt CreatorLubuntu

 

QtDisplayPosItem is a Qt QGraphicsItem class that displays its position.

Technical facts

 

 

 

 

 

 

./CppQtDisplayPosItem/CppQtDisplayPosItem.pri

 

INCLUDEPATH += \
    ../../Classes/CppQtDisplayPosItem

SOURCES += \
    ../../Classes/CppQtDisplayPosItem/qtdisplaypositem.cpp

HEADERS  += \
    ../../Classes/CppQtDisplayPosItem/qtdisplaypositem.h

OTHER_FILES += \
    ../../Classes/CppQtDisplayPosItem/Licence.txt

 

 

 

 

 

./CppQtDisplayPosItem/qtdisplaypositem.h

 

//---------------------------------------------------------------------------
/*
QtDisplayPosItem, QGraphicsItem that displays its position
Copyright (C) 2012-2015 Richel Bilderbeek

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program.If not, see <http://www.gnu.org/licenses/>.
*/
//---------------------------------------------------------------------------
//From http://www.richelbilderbeek.nl/CppQtDisplayPosItem.htm
//---------------------------------------------------------------------------
#ifndef QTDISPLAYPOSITEM_H
#define QTDISPLAYPOSITEM_H

#include <string>
#include <vector>

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Weffc++"
#pragma GCC diagnostic ignored "-Wunused-local-typedefs"
#pragma GCC diagnostic ignored "-Wunused-but-set-parameter"
#include <boost/signals2.hpp>
#include <QGraphicsSimpleTextItem>
#pragma GCC diagnostic pop

namespace ribi {

///A text item that displays its coordinats
struct QtDisplayPosItem : public QGraphicsSimpleTextItem
{
  typedef QtDisplayPosItem This_t;

  QtDisplayPosItem(QGraphicsItem *parent = 0);

  ///Obtain the version of this class
  static std::string GetVersion() noexcept;

  ///Obtain the version history of this class
  static std::vector<std::string> GetVersionHistory() noexcept;

  ///Signal to request a scene update, because this item has moved/changed
  boost::signals2::signal<void ()> m_signal_request_scene_update;

protected:
  void mouseMoveEvent(QGraphicsSceneMouseEvent *event);
  void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
};

} //~namespace ribi

#endif // QTDISPLAYPOSITEM_H

 

 

 

 

 

./CppQtDisplayPosItem/qtdisplaypositem.cpp

 

//---------------------------------------------------------------------------
/*
QtDisplayPosItem, QGraphicsItem that displays its position
Copyright (C) 2012-2015 Richel Bilderbeek

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program.If not, see <http://www.gnu.org/licenses/>.
*/
//---------------------------------------------------------------------------
//From http://www.richelbilderbeek.nl/CppQtDisplayPosItem.htm
//---------------------------------------------------------------------------
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Weffc++"
#pragma GCC diagnostic ignored "-Wunused-local-typedefs"
#pragma GCC diagnostic ignored "-Wunused-but-set-parameter"
#include "qtdisplaypositem.h"

#include <cassert>
#include <sstream>
//#include <QGraphicsScene>

#pragma GCC diagnostic pop

ribi::QtDisplayPosItem::QtDisplayPosItem(QGraphicsItem *parent)
: QGraphicsSimpleTextItem(parent),
   m_signal_request_scene_update{}
{
  this->setFlags(
      QGraphicsItem::ItemIsFocusable
    | QGraphicsItem::ItemIsMovable
    | QGraphicsItem::ItemIsSelectable);
  this->update();
}

std::string ribi::QtDisplayPosItem::GetVersion() noexcept
{
  return "1.1";
}

std::vector<std::string> ribi::QtDisplayPosItem::GetVersionHistory() noexcept
{
  std::vector<std::string> v;
  v.push_back("2012-12-19: version 1.0: initial version");
  v.push_back("2013-07-25: version 1.1: transition to Qt5");
  return v;
}


#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-but-set-parameter"
void ribi::QtDisplayPosItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
  m_signal_request_scene_update();
  QGraphicsSimpleTextItem::mouseMoveEvent(event);
}
#pragma GCC diagnostic pop

void ribi::QtDisplayPosItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
  std::stringstream s;
  s << "(" << static_cast<int>(this->pos().x()) << "," << static_cast<int>(this->pos().y()) << ")";
  this->setText(s.str().c_str());
  QGraphicsSimpleTextItem::paint(painter,option,widget);
}

 

 

 

 

 

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