Developers

Examples & demo code

These short examples show how easy it is to integrate NCReport into any Qt application.

The examples work with Qt 6 and Qt 5. The evaluation packages and the source package include more complete demo applications.

Project file

Link the NCReport library and add its include directory. Adjust the paths to where you installed NCReport. Since version 2.27 the debug build of the library is named NCReportd (previously NCReportDebug), and Zint is a separate shared library (zint / zintd).

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 a report to the modern preview dialog

Since version 2.32 the modern preview dialog renders pages exactly like the PDF and printer output. The call is modal: it returns when the dialog is closed.

main.cpp
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    NCReport *report = new NCReport();
    report->setReportFile("myreport.ncr"); // report file name, full path or relative to the working directory
    report->runReportToModernPreview(nullptr); // modal preview dialog

    // error handling
    if (report->hasError())
    {
        QMessageBox msgBox;
        msgBox.setText(QObject::tr("Report error: ") + report->lastErrorMsg());
        msgBox.exec();
    }
    delete report;
}

Run a report to the preview window

Render the report, then show the result in an NCReportPreviewWindow that you control yourself.

main.cpp
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 and show the preview in one call

The shortest way to display a report: run it and open the classic preview window in a single call.

main.cpp
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 a report to PDF

Generate a PDF file without any user interface, for example in a background job.

main.cpp
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;
}

Try NCReport free

Fully working evaluation packages for Windows, Linux, macOS, iOS and Raspberry Pi. The only limit is an evaluation label on printed pages.