PDA

View Full Version : Qwt and non-GUI application



alex-krutikov
7th April 2009, 11:52
Hi,

I would like to use Qwt in a command line application to print some plots on the images. Then the images will be used in PHP web site on Apache Linux server.

Is it possible at all?

The simple test:


#include <QtGui>

int main( int argc, char** argv )
{
QApplication app( argc, argv, false );

QImage image( 400, 300, QImage::Format_RGB32 );
QFont font( "Helvetica", 24 );
QPainter painter( &image );

painter.setFont( font );
painter.setPen( Qt::white );
painter.fillRect( image.rect(), Qt::darkBlue );
painter.drawText( image.rect(), Qt::AlignCenter, "Hello, world!" );
image.save( "test.png", "PNG" );

return 0;
}

It works. No problem. The false in the QApplication app( argc, argv, false ); makes it possible to run this program on non-gui Linux server.

The Qwt test:


#include <QApplication>
#include <QImage>
#include <QPainter>

#include <qwt_plot.h>
#include <qwt_plot_curve.h>
#include <qwt_plot_grid.h>
#include <qwt_legend.h>

static void output( QwtPlot *plot )
{
const int width = 600;
const int height = 400;

QImage image( width,height, QImage::Format_RGB32 );
image.fill(0xFFFFFF);
QPainter painter( &image );
plot->print( &painter, QRect(10,10,width-20,height-20) );
image.save( "test.png", "PNG" );
}

static void setData( QwtPlotCurve *curve )
{
QVector<double> x,y;
for(int i=0;i<1000;i++)
{ x << i;
y << sin((double)i/1000*2*3.1415926);
}
curve->setData( x,y );
}

int main( int argc, char **argv )
{
QApplication app( argc, argv, true );

QwtPlot plot;

QwtPlotGrid grid;
grid.enableXMin(true);
grid.enableYMin(true);
grid.setMajPen(QPen(Qt::black, 1, Qt::DotLine));
grid.setMinPen(QPen(Qt::gray, 1, Qt::DotLine));

grid.attach( &plot );
plot.setCanvasBackground(QColor("linen"));
plot.setAxisScale(QwtPlot::yLeft,-1,1);

plot.insertLegend(new QwtLegend, QwtPlot::BottomLegend);

QwtPlotCurve curve;
curve.setPen(QPen(Qt::darkBlue,2));
curve.setTitle("sin");
setData( &curve );
curve.attach( &plot );

app.processEvents();

output( &plot );

return 0;
}

It works on windows or GUI Linux. But in this case I can't use QApplication app( argc, argv, false ); because QwtPlot inherits QWidget and the runtime error appears:

QWidget: Cannot create a QWidget when no GUI is being used

This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.


Is it possible to use Qwt in non-gui application for printing on images?

Thank you,
-Alex

Uwe
10th April 2009, 11:06
No, because many of the attributes are stored in the widgets and the layout engine uses the size hints. But it should be possible to create an invisible plot widget.

Uwe

xburgerhout
21st April 2009, 07:21
Unfortunately, you can't use QWidget if you are on a Linux/Unix system without a running X server. I _think_ it's related to how Qt handles fonts on Linux/Unix (fontserver?).

There is a workaround, though not a pretty one. You can start a vnc server or a use a virtual framebuffer like Xvfb.

I needed to generate some graphs in a cron job and ran into the same problem. I now use a wrapper script that starts a Xvnc server before calling my application.

Xander

Uwe
22nd April 2009, 06:15
Well using Qt/Embedded instead should be a better workaround.

Uwe