Go back to Richel Bilderbeek's homepage.

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

 

 

 

 

 

(C++) Qt QGraphicsProxyWidget example 3

 

QGraphicsProxyWidget example 3 is a QGraphicsProxyWidget example.

 

 

 

 

 

 

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: CppQGraphicsProxyWidgetExample3.pro

 

QT       += core
QT       += gui
QMAKE_CXXFLAGS += -Wextra -Werror
TARGET = CppQGraphicsProxyWidgetExample3
CONFIG   += console
CONFIG   -= app_bundle
TEMPLATE = app
SOURCES += main.cpp

 

 

 

 

 

main.cpp

 

#include <cassert>
#include <cmath>
#include <QApplication>
#include <QDesktopWidget>
#include <QGraphicsProxyWidget>
#include <QGraphicsScene>
#include <QGraphicsView>
#include <QLineEdit>
#include <QLabel>
#include <QVBoxLayout>
#include <QDialog>
#include <QGraphicsSceneMouseEvent>
#include <QPointer>

struct MyWidget : public QDialog
{
  MyWidget(QWidget *parent = 0, Qt::WindowFlags f = 0)
    : QDialog(parent,f),
      m_edit(new QLineEdit(this)),
      m_label(new QLabel(this))
  {
    assert(!this->layout());
    QVBoxLayout * const layout = new QVBoxLayout(this);
    this->setLayout(layout);
    layout->addWidget(m_label);
    layout->addWidget(m_edit);
  }
  QLineEdit * const m_edit;
  QLabel * const m_label;
};

int main(int argc, char **argv)
{
  //Create the application
  QApplication app(argc, argv);

  //Create the Qt Graphics Framework components
  QGraphicsScene scene;
  QGraphicsView view(&scene);
  view.setGeometry(0,0,600,400);
  {
    //Put the dialog in the screen center
    const QRect screen = QApplication::desktop()->screenGeometry();
    view.move( screen.center() - view.rect().center() );
  }
  view.show();

  //Create the QLineEdit instances
  const int sz = 10;
  std::vector<QGraphicsProxyWidget *> proxies;
  for (int i=0; i!=sz; ++i)
  {
    MyWidget * const mywidget = new MyWidget;
    mywidget->setGeometry(0,0,64,22);
    mywidget->m_label->setText(QString("#") + QString::number(i));
    mywidget->m_edit->setText(QString("Text ") + QString::number(i));
    //Add the QWidget and obtain its proxy
    QGraphicsProxyWidget * const proxy = scene.addWidget(mywidget,Qt::Window);
    proxies.push_back(proxy);
  }

  const double ray = 150.0; //pixels
  for (int i=0; i!=sz; ++i)
  {
    const double angle = 2.0 * M_PI * static_cast<double>(i) / static_cast<double>(sz);
    const int x = static_cast<int>(0.0 + (std::sin(angle) * ray));
    const int y = static_cast<int>(0.0 - (std::cos(angle) * ray));
    QGraphicsProxyWidget * const proxy = proxies[i];
    proxy->setRotation(angle * 360.0 / (2.0 * M_PI));
    proxy->setPos(x,y);
    //proxy->setFlag(QGraphicsItem::ItemIsMovable,true); //No need to set this flag
  }
  return app.exec();
}

 

 

 

 

 

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