Base-----|---------| | | Station Station | | | | | | Device Device Device
C++ (Qt)#include <QObject>#include <QVariant>#include <QIcon>#include <QList>#include <QString> #include <QGraphicsScene> class CommonElement: public QObject{ Q_OBJECT public: explicit CommonElement(QObject*parent = 0); virtual ~CommonElement(); virtual CommonElement*appendChild() = 0; virtual QVariant getIcon() = 0; virtual QVariant getHeaderData(int ) = 0; virtual void function() = 0; virtual int amountColumn() = 0; public: QString str; QGraphicsScene *scene;};
C++ (Qt)Base::Base(QObject *parent): CommonElement(parent){ str = "Base"; // scene = new QGraphicsScene(this); ИЗ-ЗА ЭТОГО ПАДАЕТ ПРОГРАММА// scene->addText(str); !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!} Base::~Base(){} CommonElement* Base::appendChild(){ Station *station = new Station(this); return station;} QVariant Base::getIcon(){ static QIcon icon("C:/Qt/Qt5.2.1/Tools/QtCreator/bin/build-MyMTLYNX-Desktop_Qt_5_2_1_MinGW_32bit-Debug/image/database.png"); return QVariant::fromValue<QIcon>(icon);} QVariant Base::getHeaderData(int section){ switch(section) { case 0: return tr("Name"); case 1: return tr("Count Device"); case 2: return tr("Workset"); } return QVariant();} int Base::amountColumn(){ return 3;} void Base::function(){ qDebug()<<"Base";}
C++ (Qt)Station::Station(CommonElement *parent): CommonElement(parent){ str = "MyStantion"; //scene = new QGraphicsScene(this); //scene->addText(str);} Station::~Station(){} CommonElement* Station::appendChild(){ Device *device = new Device(this); return device;} QVariant Station::getIcon(){ static QIcon icon("C:/Qt/Qt5.2.1/Tools/QtCreator/bin/build-MyMTLYNX-Desktop_Qt_5_2_1_MinGW_32bit-Debug/image/station.png"); return QVariant::fromValue<QIcon>(icon);} QVariant Station::getHeaderData(int section){ switch(section) { case 0: return tr("Connection"); case 1: return tr("Serial Number"); case 2: return tr("Network Number"); case 3: return tr("Type Device"); } return QVariant();} int Station::amountColumn(){ return 4;} void Station::function(){ qDebug()<<"Station->str = " + str;}
C++ (Qt)Device::Device(QObject *parent): CommonElement(parent){ str = "MyDevice"; //scene = new QGraphicsScene(this); //scene->addText(str);} Device::~Device(){} CommonElement* Device::appendChild(){ return NULL;} QVariant Device::getIcon(){ static QIcon icon("C:/Qt/Qt5.2.1/Tools/QtCreator/bin/build-MyMTLYNX-Desktop_Qt_5_2_1_MinGW_32bit-Debug/image/device.png"); return QVariant::fromValue<QIcon>(icon);} QVariant Device::getHeaderData(int section){ switch(section) { case 0: return tr("Name"); case 1: return tr("Description"); } return QVariant();} int Device::amountColumn(){ return 2;} void Device::function(){ qDebug()<<"Device->str = " + str;}
QVariant Device::getIcon(){ static QIcon icon("C:/Qt/Qt5.2.1/Tools/QtCreator/bin/build-MyMTLYNX-Desktop_Qt_5_2_1_MinGW_32bit-Debug/image/device.png"); return QVariant::fromValue<QIcon>(icon);}
QGraphicsObject и QGraphicsScene
C++ (Qt)class CommonElement;class CommonGraphicsObject : public QGraphicsObject{ Q_OBJECT public: explicit CommonGraphicsObject(CommonElement *element,QGraphicsObject *parent = 0); virtual ~CommonGraphicsObject(); virtual QRectF boundingRect() const = 0; virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) = 0; virtual CommonElement *getModelElement() = 0; public: CommonElement *commonelement;};
C++ (Qt)DeviceGraphicsObject::DeviceGraphicsObject(Device *element,QGraphicsObject * parent): CommonGraphicsObject(element,parent){ //device = element; commonelement = element;} DeviceGraphicsObject::~DeviceGraphicsObject(){ } QRectF DeviceGraphicsObject::boundingRect() const{ return QRectF(0,0,30,20);} void DeviceGraphicsObject::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget){ QPen pen(Qt::black); painter->setBrush(Qt::yellow); painter->setPen(pen); painter->drawRect(0,0,30,20);} CommonElement *DeviceGraphicsObject::getModelElement(){ return commonelement;}
C++ (Qt)StationGraphicsObject::StationGraphicsObject(Station *element, QGraphicsObject * parent): CommonGraphicsObject(element,parent){ commonelement = element;} StationGraphicsObject::~StationGraphicsObject(){ } QRectF StationGraphicsObject::boundingRect() const{ return QRectF(0,0,30,20);} void StationGraphicsObject::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget){ QPen pen(Qt::black); painter->setBrush(Qt::green); painter->setPen(pen); painter->drawRect(0,0,30,20);} CommonElement *StationGraphicsObject::getModelElement(){ return commonelement;}
C++ (Qt)void MainWindow::clickedTreeView(QModelIndex index){ if(index.isValid()) { currentModelIndex = index; currentElement = reinterpret_cast<CommonElement*>(currentModelIndex.internalPointer()); listmodel->setRoot(currentModelIndex); setPathLabel(currentElement); listview->loadHeaderLine(currentElement); //загрузка отображения при клике graphicsScene->clear(); if(!currentElement->children().isEmpty()){ qDebug()<<"No Empty"; for(int i = 0; i < currentElement->children().size(); i++){ CommonElement *ce = (CommonElement*)(currentElement->children().at(i)); CommonGraphicsObject *cgo = ce->getGraphicsObject(); cgo->setPos(10+(cgo->boundingRect().width() + 5)*i,10); //!!!!!но почему-то программа вылетает при //!!!!! установки координат и graphicsScene->addItem(cgo); //!!!!!при добавлении объекта на сцену } graphicsView->setScene(graphicsScene); } }}
scene->clear();