Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QSvgGenerator how to create groups and name objects
Forum Updated to NodeBB v4.3 + New Features

QSvgGenerator how to create groups and name objects

Scheduled Pinned Locked Moved Unsolved General and Desktop
1 Posts 1 Posters 127 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • GilboonetG Offline
    GilboonetG Offline
    Gilboonet
    wrote on last edited by Gilboonet
    #1

    Hello, I'm currently using QSvgGenerator to generate the a svg file, which is the final goal of my application
    Capture d'écran 2024-12-17 145516.png
    On the left is what I get with QSvgGenerator, the rendering is ok but I didn't find how to order the objects so that they are grouped convieniently to be processed (machine cut/fold/trace). It it possible to add groups with QSvgGenerator ? Maybe is there something to do with painter object ? I was able to gather lines of same kind into a sole path, but wasn't able to gather the texts (numbers) that not only created one group for each number, but also a blank group.

    On the right, is what I have with my first version of the application, each page has only one path for each kind (cut, fold M, fold V, trace) and paths are named and groups are nested as needed, using external code to create the svg. I could use it again, but before I would like to know if there is a way to do the same using QSvgGenerator or maybe doing it indirectly by first using the Svg class ?

    my export to svg code is straight forward, I'm using lines from may QGraphicsScene.

    void MainWindow::exporte () {
        piecesMAJ();
    
        qreal s = 5.0f / 1.76f;
        QSvgGenerator SG;
        SG.setSize(QSize(210*s, 297*s));
        SG.setViewBox(QRect(0, 0, 210*s, 297*s));
        SG.setTitle("EXPORT SVG");
        //SG.setDescription(tr("An SVG drawing created by the SVG Generator "
        //                            "Example provided with Qt."));
    
        QPainter painter;
        QBuffer buffer;
        SG.setOutputDevice(&buffer);
    
        QPen penC = QPen(QBrush(Qt::red), 0.2, Qt::SolidLine);
        QPen penM = QPen(QBrush(Qt::darkRed), 0.1, Qt::DashLine);
        QPen pen0 = QPen(QBrush(Qt::black), 0.1, Qt::NoPen);
        QPen penV = QPen(QBrush(Qt::green), 0.1, Qt::DashDotDotLine);
        QPen penN = QPen(QBrush(Qt::blue), 0.2, Qt::SolidLine);
    
        painter.begin(&SG);
        QFont tf = QFont("Bitstream Vera Sans", 7*s);
        tf.setLetterSpacing(QFont::AbsoluteSpacing, -2);
        QFontMetrics fm(tf);
        painter.setFont(tf);
    
        QTransform tS;
        tS.scale(s, s);
    
        QPointF centre, b;
        Nums num1;
        for (auto && n : dep.pieces) {
            // ligne de coupe
            QList<QLineF>lCoupe, lPliM, lPliV;
    
            for (auto && l : n.lignes) {
                QLineF *ligne = new QLineF(l.p1 + n.bord->pos(), l.p2+n.bord->pos());
                *ligne = tS.map(*ligne);
                if (l.nb == 1) {
                    lCoupe << *ligne;
                    num1 = Nums(l.id1, l.id2);
                    QString chNumero = QString::number(dep.nums[dep.nums.indexOf(num1)].num);
                    centre =  ligne->center();
                    painter.save();
                    painter.translate(centre);
                    painter.rotate(180- ligne->angle());
                    b = QPointF(- fm.boundingRect(chNumero).width()/2, -1.8f);
                    painter.setPen(penN);
                    painter.drawText(b, chNumero);
                    painter.restore();
                } else {
                    if (l.cop < 0)
                        lPliV << *ligne;
                    else if (l.cop > 0)
                        lPliM << *ligne;
                    else
                        delete(ligne);
                }
            }
    
            if (!lCoupe.isEmpty()) {
                painter.setPen(penC);
                painter.drawPath(construitChemin(lCoupe));
            }
    
            if (!lCoupe.isEmpty()) {
                painter.setPen(penM);
                painter.drawPath(construitChemin(lPliM));
            }
    
            if (!lCoupe.isEmpty()) {
                painter.setPen(penV);
                painter.drawPath(construitChemin(lPliV));
            }
        }
    
        painter.end();
    
        QFileDialog::saveFileContent(buffer.data(), "myExport.svg");
    }
    
    QPainterPath MainWindow::construitChemin(QList<QLineF> lignes) {
        QPainterPath chemin;
        for (auto && l : lignes) {
            chemin.moveTo(l.p1().x(), l.p1().y());
            chemin.lineTo(l.p2().x(), l.p2().y());
        }
        return chemin;
    }
    
    1 Reply Last reply
    0

    • Login

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Get Qt Extensions
    • Unsolved