Code Examples
The following simple examples demonstrate how easy to integrate and use NCReport in any Qt application. The codes are Qt4.6 - Qt5.8 compatible. The project file settings are based on Qt .pro file syntax.
Project File
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets printsupport
TARGET = MySimpleDemo
TEMPLATE = app
SOURCES += main.cpp
win32:CONFIG(release, debug|release) : LIBS += -L$$PWD/../ncreport/lib/ -lNCReport2
else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/../ncreport/lib/ -lNCReportDebug2
INCLUDEPATH += $$PWD/../ncreport/includes
Run report to the preview window 1.
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
NCReport *report = new NCReport();
report->setReportSource( NCReportSource::File ); // set report source type
report->setReportFile("myreport.ncr"); //set the report filename fullpath or relative to dir
report->runReportToPreview(); // run to preview output
// error handling
if( report->hasError())
{
QMessageBox msgBox;
msgBox.setText(QObject::tr("Report error: ") + report->lastErrorMsg());
msgBox.exec();
}
else
{
// show preview
NCReportPreviewWindow *pv = new NCReportPreviewWindow(); // create preview window
pv->setOutput( (NCReportPreviewOutput*)report->output() ); // add output to the window
pv->setReport(report);
pv->setWindowModality(Qt::ApplicationModal ); // set modality
pv->setAttribute( Qt::WA_DeleteOnClose ); // set attrib
pv->exec(); // run like modal dialog
}
delete report;
}
Run report to the preview window 2.
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
NCReport *report = new NCReport();
report->setReportFile("myreport.ncr"); //set the report filename fullpath or relative to dir
report->runReportToShowPreview(); // run and show to preview output
// error handling
if( report->hasError())
{
QMessageBox msgBox;
msgBox.setText(QObject::tr("Report error: ") + report->lastErrorMsg());
msgBox.exec();
}
delete report;
}
Run report to PDF
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
NCReport *report = new NCReport();
report->setReportFile("myreport.ncr"); //set the report filename fullpath or relative to dir
report->runReportToPDF("c:/temp/myreportoutput.pdf")
// error handling
if( report->hasError())
{
QMessageBox msgBox;
msgBox.setText(QObject::tr("Report error: ") + report->lastErrorMsg());
msgBox.exec();
}
delete report;
}