Results 1 to 4 of 4

Thread: Qwt and non-GUI application

  1. #1
    Join Date
    Apr 2009
    Posts
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question Qwt and non-GUI application

    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:
    Qt Code:
    1. #include <QtGui>
    2.  
    3. int main( int argc, char** argv )
    4. {
    5. QApplication app( argc, argv, false );
    6.  
    7. QImage image( 400, 300, QImage::Format_RGB32 );
    8. QFont font( "Helvetica", 24 );
    9. QPainter painter( &image );
    10.  
    11. painter.setFont( font );
    12. painter.setPen( Qt::white );
    13. painter.fillRect( image.rect(), Qt::darkBlue );
    14. painter.drawText( image.rect(), Qt::AlignCenter, "Hello, world!" );
    15. image.save( "test.png", "PNG" );
    16.  
    17. return 0;
    18. }
    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:
    Qt Code:
    1. #include <QApplication>
    2. #include <QImage>
    3. #include <QPainter>
    4.  
    5. #include <qwt_plot.h>
    6. #include <qwt_plot_curve.h>
    7. #include <qwt_plot_grid.h>
    8. #include <qwt_legend.h>
    9.  
    10. static void output( QwtPlot *plot )
    11. {
    12. const int width = 600;
    13. const int height = 400;
    14.  
    15. QImage image( width,height, QImage::Format_RGB32 );
    16. image.fill(0xFFFFFF);
    17. QPainter painter( &image );
    18. plot->print( &painter, QRect(10,10,width-20,height-20) );
    19. image.save( "test.png", "PNG" );
    20. }
    21.  
    22. static void setData( QwtPlotCurve *curve )
    23. {
    24. QVector<double> x,y;
    25. for(int i=0;i<1000;i++)
    26. { x << i;
    27. y << sin((double)i/1000*2*3.1415926);
    28. }
    29. curve->setData( x,y );
    30. }
    31.  
    32. int main( int argc, char **argv )
    33. {
    34. QApplication app( argc, argv, true );
    35.  
    36. QwtPlot plot;
    37.  
    38. grid.enableXMin(true);
    39. grid.enableYMin(true);
    40. grid.setMajPen(QPen(Qt::black, 1, Qt::DotLine));
    41. grid.setMinPen(QPen(Qt::gray, 1, Qt::DotLine));
    42.  
    43. grid.attach( &plot );
    44. plot.setCanvasBackground(QColor("linen"));
    45. plot.setAxisScale(QwtPlot::yLeft,-1,1);
    46.  
    47. plot.insertLegend(new QwtLegend, QwtPlot::BottomLegend);
    48.  
    49. QwtPlotCurve curve;
    50. curve.setPen(QPen(Qt::darkBlue,2));
    51. curve.setTitle("sin");
    52. setData( &curve );
    53. curve.attach( &plot );
    54.  
    55. app.processEvents();
    56.  
    57. output( &plot );
    58.  
    59. return 0;
    60. }
    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
    Last edited by alex-krutikov; 7th April 2009 at 13:38.

  2. #2
    Join Date
    Feb 2006
    Location
    Munich, Germany
    Posts
    3,325
    Thanked 879 Times in 827 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Qwt and non-GUI application

    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

  3. #3
    Join Date
    Nov 2006
    Location
    Netherlands
    Posts
    6
    Thanks
    1
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Qwt and non-GUI application

    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

  4. #4
    Join Date
    Feb 2006
    Location
    Munich, Germany
    Posts
    3,325
    Thanked 879 Times in 827 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Qwt and non-GUI application

    Well using Qt/Embedded instead should be a better workaround.

    Uwe

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.