0

I'm building an app using Qt Quick Controls and I also need some C++ in there (for printing and reading a file). I dug up this, so I've modified it to my needs:

bonuri.h

#ifndef BONURI_H
#define BONURI_H

#include <QObject>
#include <QTemporaryFile>
#include <QIODevice>
#include <QPainter>
#include <QtSvg/QSvgRenderer>
#include <QFile>
#include <QTextStream>
#include <QtPrintSupport/QPrintPreviewDialog>
#include <QtPrintSupport/QPrinter>

class Bonuri : public QObject {
    Q_OBJECT

public:
    explicit Bonuri(QObject *parent = 0);
public slots:
    void printSVG(const QString& in);
    QString list();
    void actualPrint(QPrinter* p, const QString& in);
};

#endif // BONURI_H

bonuri.cpp

#include "bonuri.h"

Bonuri::Bonuri(QObject *parent) :
    QObject(parent)
{
}

void Bonuri::printSVG(const QString& in){
    QPrinter printer(QPrinter::HighResolution);
    printer.setPaperSize(QPrinter::A5);
    QPrintPreviewDialog printDialog(&printer);
    connect(&printDialog, SIGNAL(paintRequested(QPrinter*)), SLOT(actualPrint(QPrinter*, in)));
    printDialog.exec();
}

void actualPrint(QPrinter* p, const QString& in){
    QTemporaryFile file;
    if (file.open()){
           QTextStream out(&file);
           out << in;
           QSvgRenderer renderer(file.fileName());
           QPainter myPainter(p);
           myPainter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing | QPainter::SmoothPixmapTransform);
           renderer.render(&myPainter);
    }
}

QString list(){
    QFile file("Bonuri.json");

    QString totalLine;
    if(file.open(QIODevice::ReadOnly | QIODevice::Text)){
        QTextStream in(&file);
        while (!in.atEnd()){
            QString line = in.readLine();
            totalLine += line;
        }
    }
    return totalLine;
}

The problem is that I'm not that big of a C++ expert and wasn't able do debug this.

The problem: moc_bonuri.cpp

undefined reference to 'Bonuri::list()'; undefined reference to 'Bonuri::actualPrint(QPrinter*, QString const&)'

One more thing: could you help me optimize the code (I'm new to C++ and really have no idea why this is the way it is, I just used the example).

Community
  • 1
  • 1
user2563892
  • 553
  • 1
  • 7
  • 16

1 Answers1

0

Should be:

void Bonuri::actualPrint(QPrinter* p, const QString& in){
    //QTemporaryFile file;//now it is a class member
    if (file.open()){
           QTextStream out(&file);
           out << in;
           QSvgRenderer renderer(file.fileName());
           QPainter myPainter(p);
           myPainter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing | QPainter::SmoothPixmapTransform);
           renderer.render(&myPainter);
    }
}

QString Bonuri::list(){
    QFile file("Bonuri.json");

    QString totalLine;
    if(file.open(QIODevice::ReadOnly | QIODevice::Text)){
        QTextStream in(&file);
        while (!in.atEnd()){
            QString line = in.readLine();
            totalLine += line;
        }
    }
    return totalLine;
}

Another problem:

connect(&printDialog, SIGNAL(paintRequested(QPrinter*)), SLOT(actualPrint(QPrinter*, in)));

You can't use more arguments than signal has, you can use less but not more. So it should be at least:

connect(&printDialog, SIGNAL(paintRequested(QPrinter*)), SLOT(actualPrint(QPrinter*)));

But in this case you should totally rewrite your method.

class Bonuri : public QObject {
    Q_OBJECT
private:
   QTemporaryFile file;//...
Jablonski
  • 18,083
  • 2
  • 46
  • 47
  • The problem is that QTemporaryFile gets deleted when it is out of scope, so what can I do? – user2563892 Nov 04 '14 at 17:41
  • @user2563892 create QTemporaryFile as a class member. – Jablonski Nov 04 '14 at 17:42
  • Thanks, I'm a noob at C++, don't get the wrong impression. That's why I'm using QML, too bad there isn't a PrintDialog in QtQuick. – user2563892 Nov 04 '14 at 17:47
  • Btw, how do I handle it later, do I just rewrite it? – user2563892 Nov 04 '14 at 17:48
  • @user2563892 see this in doc: http://qt-project.org/doc/qt-4.8/qtemporaryfile.html#autoRemove Also maybe simple QFile will be more useful for you (just delete this file when you don't need this) http://qt-project.org/doc/qt-5/qfile.html – Jablonski Nov 04 '14 at 17:54
  • everything is fine with the temporary file, but for some reason it says Premature end of document. I think (judging by other people's xml reading problems) it's because of unescaped characters. What do I do? – user2563892 Nov 04 '14 at 18:31
  • @user2563892 Now it is some specific things with xml reading and writing, so unfortunately I can't help you here. One thing that I can suggest you is to search in web `Premature end of document qt`. There are some topics on forum with this question. Maybe something will be helpful for you. – Jablonski Nov 04 '14 at 18:38