PDA

View Full Version : QPixmap analog in console



timmu
27th August 2012, 16:34
I'm writing a console application that needs to produce a graphical output. This output is two sets of values plotted against each other and a short sentence of text. I would like to use QPixmap (and QImage) for this to produce a jpeg, however, these do not work with a console application. What would be the solution? Is there some way to still use QPixmap or do I need an alternative? Thanks!

spirit
27th August 2012, 16:37
You should enable "gui" in your pro-file to use QPixmap/QImage.

timmu
27th August 2012, 16:45
Thanks! How do I enable "gui" in the pro file?

spirit
27th August 2012, 16:47
QT += gui
:)

timmu
28th August 2012, 09:44
I'm using QPixmap and QImage with console application.
I have "QT += gui" in my *.pro file.
This is what I'm including:



#include <QApplication>
#include <QCoreApplication>
#include <QtGui>
#include <QImage>
#include <QPixmap>


Then I add this in my code



QPixmap map(100,100);


The code compiles fine but it terminates during runtime with this message: "segmentation fault".

It seems I don't know how to do graphics with console. This worked fine with a gui program. Any help in trouble-shooting is most apreciated.

Thanks

spirit
28th August 2012, 09:56
Did you rebuild you project from scratch (make clean, qmake, make) after adding QT += gui?

timmu
28th August 2012, 10:10
Hi Spirit.

I do:
1. "qmake -project"
2. open *.pro file and and add Qt += gui
3. (optionally "make clean")
4. qmake
5. make

spirit
28th August 2012, 10:13
pro


QT += core gui

TARGET = console
CONFIG += console
CONFIG -= app_bundle

TEMPLATE = app

SOURCES += main.cpp


main.cpp


#include <QApplication>
#include <QPixmap>
#include <QDesktopWidget>

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
return QPixmap::grabWindow(a.desktop()->winId()).save("pixmap.png", "png");
// return a.exec();
}


Works fine for me.

timmu
28th August 2012, 10:38
Thanks. I've added your lines to my *.pro file all together and one at a time but I still get the segmentation fault.

My *.pro file as it comes out of qmake -project, qmake, make:



TEMPLATE = app
TARGET =
DEPENDPATH += .
INCLUDEPATH += .

# Input
HEADERS += functions.cpp
SOURCES += functions.cpp main.cpp


How to modify this correctly. Should I add QT += gui or QT += core gui?
Do I still need these:

CONFIG += console
CONFIG -= app_bundle

Thanks!

By the way in my main() I have



//I have this
QCoreApplication application(argc, argv);
//and not this
QApplication application(argc, argv);


Does this matter?

spirit
28th August 2012, 10:41
It would be easier if you could attach your project.


CONFIG -= app_bundle

Is only for MacOS.


CONFIG += console

Tells that your app is the console app.

timmu
28th August 2012, 10:58
Thanks again. I created simplified version of my project. I have 2 files .
main.cpp


#include <QApplication>
#include <QCoreApplication>

#include "functions.cpp"

#include <fstream>
#include <iostream>
#include <cstring>
#include <stdlib.h>

#include <QtGui>
#include <QImage>
#include <QPixmap>
#include <QDesktopWidget>

using namespace std;
void doPrint();

int main(int argc, char *argv[])
{

QCoreApplication application(argc, argv);
//QApplication application(argc, argv);

doPrint();

return 0;
}



functions.cpp


#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <string>

#include <QtGui>
#include <QImage>
#include <QPixmap>
#include <QApplication>
#include <QCoreApplication>


using namespace std;


void doPrint()
{

QPixmap map(100,100);

map.fill(Qt::white);
QPainter p(&map);
QPen pen;

p.setPen(pen);
p.setPen(QColor(100,100,100));
p.drawEllipse(0,0,50,50);
p.drawLine(10,20,30,40);

//map.save("figure.jpg", 0, 90);

cout << "Hello\n";
}



Do you know why this compiles but won't run? I'm using Linux. Thanks

spirit
28th August 2012, 11:01
This is really odd


void doPrint()
{
...
QPixmap map(100,100); exit(1);//why do you need to exit right here?
...
}

timmu
28th August 2012, 11:03
Sorry. I didn't really have exit(1) there. I took it out but you responded before I managed to do it. It's not really there. And it still won't run. (Please see above for correct code)

spirit
28th August 2012, 11:07
main should look like this


...
int main(int argc, char *argv[])
{

QApplication application(argc, argv);

doPrint();

return 0;
}

Eg, QApplication must be created instead of QCoreApplication, because in this case you'll get


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

in the console.

timmu
28th August 2012, 11:18
Spirit, you rock! Thanks and greetings to Ukraine!

Flass
19th February 2016, 07:16
main should look like this


...
int main(int argc, char *argv[])
{

QApplication application(argc, argv);

doPrint();

return 0;
}

Eg, QApplication must be created instead of QCoreApplication, because in this case you'll get

in the console.

thank you very much!