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).