简体   繁体   中英

Text in GraphicsView

I have added text items in a graphics scene, in Qt, by assigning id to it, but I am unable to save the items; I am using XmlstreamWriter.

Following is my code snippet for adding the text:

cadtextitem.h

#ifndef cadtextitem_H
#define cadtextitem_H

#include <QGraphicsTextItem>
#include <QPen>

QT_BEGIN_NAMESPACE
class QFocusEvent;
class QGraphicsItem;
class QGraphicsScene;
class QGraphicsSceneMouseEvent;
QT_END_NAMESPACE

class cadtextitem:public QGraphicsTextItem
{
    Q_OBJECT
public:
    enum { Type = UserType + 3 };
    cadtextitem( int, QGraphicsItem *parent=0);
    int type() const{return Type;}
    int id;
signals:
    void lostFocus(cadtextitem *item);
    void selectedChange(QGraphicsItem *item);

protected:
    QVariant itemChange(GraphicsItemChange change, const QVariant &value);
    void focusOutEvent(QFocusEvent *event);
    void mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event);
};

#endif // cadtextitem_H

cadtextitem.cpp

 #include "cadtextitem.h"

        cadtextitem::cadtextitem(int i, QGraphicsItem *parent)
            : QGraphicsTextItem(parent)
        {
            //assigns id
            id = i;

        }

        QVariant cadtextitem::itemChange(GraphicsItemChange change,
                                         const QVariant &value)
        {
            if (change == QGraphicsItem::ItemSelectedHasChanged)
                emit selectedChange(this);
            return value;
        }

        void cadtextitem::focusOutEvent(QFocusEvent *event)
        {
            setTextInteractionFlags(Qt::NoTextInteraction);
            emit lostFocus(this);
            QGraphicsTextItem::focusOutEvent(event);
        }

        void cadtextitem::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event)
        {
            if (textInteractionFlags() == Qt::NoTextInteraction)
                setTextInteractionFlags(Qt::TextEditorInteraction);
            QGraphicsTextItem::mouseDoubleClickEvent(event);
        }

Following is the code to save the items:

mainwindow.cpp

void MainWindow::on_actionSave_triggered()
{
    // save file dialog box
    QString filename = QFileDialog::getSaveFileName(this,
                                                  tr("Save File"),
                                                  QString(),
                                                  tr("file Name(*.xml)"));
    if(!filename.isEmpty())
    {
        QFile file(filename);
        if (!file.open(QIODevice::WriteOnly))
        {
            QMessageBox::critical(this, tr("Error"), tr("Could not open file"));
            return;
        }
        else
        {
            QXmlStreamWriter xmlWriter(&file);
            xmlWriter.setAutoFormatting(true);
            xmlWriter.writeStartDocument();
            xmlWriter.writeStartElement("SceneData");
            xmlWriter.writeAttribute("version", "v1.0");
            xmlWriter.writeStartElement("Entities");

            view->scene->writeStream(&xmlWriter);

            xmlWriter.writeEndElement();   //end of Entities
            xmlWriter.writeEndElement();   //end of SceneData
            QMessageBox::warning(this, "Saved",
                                 QString("Saved Scene Data to '%1'").
                                 arg(filename));
            file.close();
        }
    }

cadgraphicsscene.cpp

void CadGraphicsScene::writeStream(QXmlStreamWriter *stream)
{
    // write entities in a file
    foreach (QGraphicsItem *item, itemList)
    {
        if (items().contains(item))
        {
            if (item->type() == Point::Type)
            {
                Point *myItem = dynamic_cast<Point *>(item);
                stream->writeStartElement("Point");
                stream->writeAttribute("id", QString::number(myItem->id));
                stream->writeAttribute("x", QString::number(myItem->x()));
                stream->writeAttribute("y", QString::number(myItem->y()));
                stream->writeEndElement();  //end of Point Item
            }

            else if (item->type() == Line::Type)
            {
                Line *myItem = dynamic_cast<Line *>(item);
                stream->writeStartElement("Line");
                stream->writeAttribute("id", QString::number(myItem->id));
                stream->writeAttribute("x1", QString::number(myItem->start_p.x()));
                stream->writeAttribute("y1", QString::number(myItem->start_p.y()));
                stream->writeAttribute("x2", QString::number(myItem->end_p.x()));
                stream->writeAttribute("y2", QString::number(myItem->end_p.y()));
                stream->writeEndElement();  //end of Line Item
            }

            else if (item->type() == Circle::Type)
            {
                Circle *myItem = dynamic_cast<Circle *>(item);
                stream->writeStartElement("Circle");
                stream->writeAttribute("id", QString::number(myItem->id));
                stream->writeAttribute("cx", QString::number(myItem->center_p.x()));
                stream->writeAttribute("cy", QString::number(myItem->center_p.y()));
                stream->writeAttribute("radius", QString::number(myItem->radius));
                stream->writeEndElement();  //end of Circle Item
            }

            else if (item->type() == Ellipse::Type)
            {
                Ellipse *myItem = dynamic_cast<Ellipse *>(item);
                stream->writeStartElement("Ellipse");
                stream->writeAttribute("id", QString::number(myItem->id));
                stream->writeAttribute("cx", QString::number(myItem->p1.x()));
                stream->writeAttribute("cy", QString::number(myItem->p1.y()));
                stream->writeAttribute("majR", QString::number(myItem->majRadius));
                stream->writeAttribute("minR", QString::number(myItem->minRadius));
                stream->writeEndElement();  //end of Ellipse Item
            }


            else if (item->type() == mText::Type)
            {
                mText *myItem = dynamic_cast<mText *>(item);
                stream->writeStartElement("Ellipse");
                stream->writeAttribute("id", QString::number(myItem->id));
                stream->writeAttribute("textwidth", QString::number(myItem->textWidth()));
                stream->writeEndElement();  //end of Text Item
            }
        }
    }
}

在发布的代码中,您缺少对流编写器writeEndDocument函数的调用。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM