(C++) QGraphicsProxyWidget eexample 4

February 24, 2017 · View on GitHub

 

 

 

 

 

(C++) Qt QGraphicsProxyWidget eexample 4

 

QGraphicsProxyWidget example 4 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 Qt: version 4.8.3 (32 bit)
  • STL STL: GNU ISO C++ Library, version 4.7.2

 

 

 

 

 

Qt project file: CppQGraphicsProxyWidgetExample4.pro

 


QT       += core QT       += gui QMAKE_CXXFLAGS += -Wextra -Werror TARGET = CppQGraphicsProxyWidgetExample4 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 MyItem : public QWidget {   MyItem(QWidget *parent = 0, Qt::WindowFlags f = 0)     : QWidget(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; }; struct MyView : public QGraphicsView {   MyView(QWidget *parent = 0)     : QGraphicsView(parent),       m_scene(new QGraphicsScene(this))   {     this->setScene(m_scene);     //Create the QLineEdit instances     const int sz = 10;     std::vector<QGraphicsProxyWidget *> proxies;     for (int i=0; i!=sz; ++i)     {       MyItem * const mywidget = new MyItem;       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 = m_scene->addWidget(mywidget);       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     }   }   QGraphicsScene * const m_scene; }; int main(int argc, char **argv) {   //Create the application   QApplication app(argc, argv);   MyView view;   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();   return app.exec(); }