Results 1 to 16 of 16

Thread: QPixmap analog in console

  1. #1
    Join Date
    Aug 2009
    Posts
    122
    Thanks
    74
    Qt products
    Qt4
    Platforms
    Windows

    Question QPixmap analog in console

    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!

  2. #2
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QPixmap analog in console

    You should enable "gui" in your pro-file to use QPixmap/QImage.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  3. The following user says thank you to spirit for this useful post:

    timmu (27th August 2012)

  4. #3
    Join Date
    Aug 2009
    Posts
    122
    Thanks
    74
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QPixmap analog in console

    Thanks! How do I enable "gui" in the pro file?

  5. #4
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QPixmap analog in console

    QT += gui
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  6. The following user says thank you to spirit for this useful post:

    timmu (28th August 2012)

  7. #5
    Join Date
    Aug 2009
    Posts
    122
    Thanks
    74
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QPixmap analog in console

    I'm using QPixmap and QImage with console application.
    I have "QT += gui" in my *.pro file.
    This is what I'm including:

    Qt Code:
    1. #include <QApplication>
    2. #include <QCoreApplication>
    3. #include <QtGui>
    4. #include <QImage>
    5. #include <QPixmap>
    To copy to clipboard, switch view to plain text mode 

    Then I add this in my code

    Qt Code:
    1. QPixmap map(100,100);
    To copy to clipboard, switch view to plain text mode 

    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

  8. #6
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QPixmap analog in console

    Did you rebuild you project from scratch (make clean, qmake, make) after adding QT += gui?
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  9. #7
    Join Date
    Aug 2009
    Posts
    122
    Thanks
    74
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QPixmap analog in console

    Hi Spirit.

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

  10. #8
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QPixmap analog in console

    pro
    Qt Code:
    1. QT += core gui
    2.  
    3. TARGET = console
    4. CONFIG += console
    5. CONFIG -= app_bundle
    6.  
    7. TEMPLATE = app
    8.  
    9. SOURCES += main.cpp
    To copy to clipboard, switch view to plain text mode 

    main.cpp
    Qt Code:
    1. #include <QApplication>
    2. #include <QPixmap>
    3. #include <QDesktopWidget>
    4.  
    5. int main(int argc, char *argv[])
    6. {
    7. QApplication a(argc, argv);
    8. return QPixmap::grabWindow(a.desktop()->winId()).save("pixmap.png", "png");
    9. // return a.exec();
    10. }
    To copy to clipboard, switch view to plain text mode 

    Works fine for me.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  11. The following user says thank you to spirit for this useful post:

    timmu (28th August 2012)

  12. #9
    Join Date
    Aug 2009
    Posts
    122
    Thanks
    74
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QPixmap analog in console

    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:

    Qt Code:
    1. TEMPLATE = app
    2. TARGET =
    3. DEPENDPATH += .
    4. INCLUDEPATH += .
    5.  
    6. # Input
    7. HEADERS += functions.cpp
    8. SOURCES += functions.cpp main.cpp
    To copy to clipboard, switch view to plain text mode 

    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

    Qt Code:
    1. //I have this
    2. QCoreApplication application(argc, argv);
    3. //and not this
    4. QApplication application(argc, argv);
    To copy to clipboard, switch view to plain text mode 

    Does this matter?
    Last edited by timmu; 28th August 2012 at 09:42.

  13. #10
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QPixmap analog in console

    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.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  14. The following user says thank you to spirit for this useful post:

    timmu (28th August 2012)

  15. #11
    Join Date
    Aug 2009
    Posts
    122
    Thanks
    74
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QPixmap analog in console

    Thanks again. I created simplified version of my project. I have 2 files .
    main.cpp
    Qt Code:
    1. #include <QApplication>
    2. #include <QCoreApplication>
    3.  
    4. #include "functions.cpp"
    5.  
    6. #include <fstream>
    7. #include <iostream>
    8. #include <cstring>
    9. #include <stdlib.h>
    10.  
    11. #include <QtGui>
    12. #include <QImage>
    13. #include <QPixmap>
    14. #include <QDesktopWidget>
    15.  
    16. using namespace std;
    17. void doPrint();
    18.  
    19. int main(int argc, char *argv[])
    20. {
    21.  
    22. QCoreApplication application(argc, argv);
    23. //QApplication application(argc, argv);
    24.  
    25. doPrint();
    26.  
    27. return 0;
    28. }
    To copy to clipboard, switch view to plain text mode 


    functions.cpp
    Qt Code:
    1. #include <iostream>
    2. #include <cstring>
    3. #include <cstdlib>
    4. #include <cmath>
    5. #include <string>
    6.  
    7. #include <QtGui>
    8. #include <QImage>
    9. #include <QPixmap>
    10. #include <QApplication>
    11. #include <QCoreApplication>
    12.  
    13.  
    14. using namespace std;
    15.  
    16.  
    17. void doPrint()
    18. {
    19.  
    20. QPixmap map(100,100);
    21.  
    22. map.fill(Qt::white);
    23. QPainter p(&map);
    24. QPen pen;
    25.  
    26. p.setPen(pen);
    27. p.setPen(QColor(100,100,100));
    28. p.drawEllipse(0,0,50,50);
    29. p.drawLine(10,20,30,40);
    30.  
    31. //map.save("figure.jpg", 0, 90);
    32.  
    33. cout << "Hello\n";
    34. }
    To copy to clipboard, switch view to plain text mode 

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

  16. #12
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QPixmap analog in console

    This is really odd
    Qt Code:
    1. void doPrint()
    2. {
    3. ...
    4. QPixmap map(100,100); exit(1);//why do you need to exit right here?
    5. ...
    6. }
    To copy to clipboard, switch view to plain text mode 
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  17. #13
    Join Date
    Aug 2009
    Posts
    122
    Thanks
    74
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QPixmap analog in console

    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)

  18. #14
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QPixmap analog in console

    main should look like this
    Qt Code:
    1. ...
    2. int main(int argc, char *argv[])
    3. {
    4.  
    5. QApplication application(argc, argv);
    6.  
    7. doPrint();
    8.  
    9. return 0;
    10. }
    To copy to clipboard, switch view to plain text mode 
    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.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  19. The following user says thank you to spirit for this useful post:

    timmu (28th August 2012)

  20. #15
    Join Date
    Aug 2009
    Posts
    122
    Thanks
    74
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QPixmap analog in console

    Spirit, you rock! Thanks and greetings to Ukraine!

  21. #16
    Join Date
    Apr 2013
    Posts
    1
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Maemo/MeeGo

    Default Re: QPixmap analog in console

    Quote Originally Posted by spirit View Post
    main should look like this
    Qt Code:
    1. ...
    2. int main(int argc, char *argv[])
    3. {
    4.  
    5. QApplication application(argc, argv);
    6.  
    7. doPrint();
    8.  
    9. return 0;
    10. }
    To copy to clipboard, switch view to plain text mode 
    Eg, QApplication must be created instead of QCoreApplication, because in this case you'll get

    in the console.
    thank you very much!

Similar Threads

  1. __emul Qt analog
    By casperbear in forum Newbie
    Replies: 2
    Last Post: 4th August 2011, 17:09
  2. Does Qt have a numeric_limits<T>::max analog?
    By MattPhillips in forum Qt Programming
    Replies: 0
    Last Post: 16th January 2011, 00:21
  3. Analog .Net Remoting in Qt
    By sergey_85 in forum Qt Programming
    Replies: 1
    Last Post: 25th November 2009, 09:00
  4. fgets() analog in Qt
    By tommy in forum Qt Programming
    Replies: 1
    Last Post: 27th March 2009, 23:45
  5. Analog Gauges
    By RookieAM in forum General Programming
    Replies: 1
    Last Post: 15th February 2008, 21:53

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.