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 )
{
QFont font
( "Helvetica",
24 );
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;
}
#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;
}
To copy to clipboard, switch view to plain text mode
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;
image.fill(0xFFFFFF);
plot
->print
( &painter,
QRect(10,
10,width
-20,height
-20) );
image.save( "test.png", "PNG" );
}
{
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 )
{
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);
curve.
setPen(QPen(Qt
::darkBlue,
2));
curve.setTitle("sin");
setData( &curve );
curve.attach( &plot );
app.processEvents();
output( &plot );
return 0;
}
#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;
}
To copy to clipboard, switch view to plain text mode
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
Bookmarks