objekty
File myitem.h
#ifndef MYITEM_H #define MYITEM_H #include <QString> class MyItem { public: virtual QString info (); }; #endif // MYITEM_H
File myitem.cpp
#include "myitem.h" QString MyItem::info () { return "my item"; }
File myrectangle.h
#include "myitem.h" ... , public MyItem
#ifndef MYRECTANGLE_H #define MYRECTANGLE_H #include <QGraphicsRectItem> #include "myitem.h" class MyRectangle : public QGraphicsRectItem, public MyItem { public: virtual QString info (); }; #endif // MYRECTANGLE_H
File myrectangle.cpp
#include "myrectangle.h" QString MyRectangle::info () { return "my rectangle " + QString::number ( rect().x() ) + "," + QString::number ( rect().y() ) + " " + QString::number ( rect().width() ) + " x " + QString::number ( rect().height() ); }
File myellipse.h
#include "myitem.h" ... , public MyItem
#ifndef MYELLIPSE_H #define MYELLIPSE_H #include <QGraphicsRectItem> #include "myitem.h" class MyEllipse : public QGraphicsRectItem, public MyItem { public: virtual QString info (); }; #endif // MYELLIPSE_H
File myellipse.cpp
#include "myellipse.h" QString MyEllipse::info () { return "my ellipse " + QString::number ( rect().x() ) + "," + QString::number ( rect().y() ) + " " + QString::number ( rect().width() ) + " x " + QString::number ( rect().height() ); }
File mywindow.cpp
#include "myrectangle.h" #include "myellipse.h"
upravit ve funkci setup
MyRectangle * r = new MyRectangle; MyEllipse * e = new MyEllipse;
pridat do funkce showBranch
MyItem * m = dynamic_cast < MyItem * > (item); if (m != NULL) s = m->info ();
a cely soubor
#include "mainwindow.h" #include "ui_mainwindow.h" #include <QGraphicsRectItem> #include <QGraphicsEllipseItem> #include "myrectangle.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); setup (); } MainWindow::~MainWindow() { delete ui; } void MainWindow::setup () { QGraphicsScene * scene = new QGraphicsScene (); ui->graphicsView->setScene (scene); scene->addLine(10, 10, 100, 100); scene->addRect(100, 10, 20, 20); scene->addEllipse(140, 20, 40, 70); MyRectangle * r = new MyRectangle; r->setPen(QColor ("red")); r->setBrush(QColor ("yellow")); r->setRect (100, 100, 100, 100); r->setFlags(QGraphicsItem::ItemIsMovable); scene->addItem (r); for (int i = 1; i <= 2; i++) { QGraphicsEllipseItem * e = new QGraphicsEllipseItem; e->setPen(QColor ("orange")); e->setBrush(QColor ("green")); e->setParentItem (r); e->setRect (70 + 40*i, 120, 20, 20); } showScene (scene); } void MainWindow::showScene (QGraphicsScene * scene) { QList < QGraphicsItem * > list = scene->items (); int cnt = list.count (); for (int i = 0 ; i < cnt; i++) { QGraphicsItem * p = list.value (i); if (p->parentItem() == NULL) showBranch (NULL, p); } } void MainWindow::showBranch (QTreeWidgetItem * above, QGraphicsItem * item) { QTreeWidgetItem * t = new QTreeWidgetItem; QString s = "something"; QGraphicsRectItem * r = dynamic_cast < QGraphicsRectItem * > (item); if (r != NULL) s = "rectangle " + QString::number ( r->rect().x() ) + ", " + QString::number ( r->rect().y() ); MyRectangle * m = dynamic_cast < MyRectangle * > (item); if (m != NULL) s = m->info (); t->setText (0, s); if (above == NULL) ui->treeWidget->addTopLevelItem(t); else above->addChild (t); QList < QGraphicsItem * > list = item->childItems(); int cnt = list.count (); for (int i = 0 ; i < cnt; i++) showBranch (t, list[i]); }
objekty.txt · Last modified: 2014/10/16 11:05 by 127.0.0.1