Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED] Why QPrinter need QtGUI

[SOLVED] Why QPrinter need QtGUI

Scheduled Pinned Locked Moved General and Desktop
18 Posts 4 Posters 10.0k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    Pitchou
    wrote on last edited by
    #1

    Hi everyone
    I would to know why I must have a X11 server connection to use the QPrinter Class. In Qt 4.3, I wrote a programm which use Qprinter without the GUI and it works well.
    But I need to compile it on a new platform which has Qt 4.6 and it doesn't work because of the GUI.

    DO you have an answer ?
    Thanks

    1 Reply Last reply
    0
    • B Offline
      B Offline
      bjanuario
      wrote on last edited by
      #2

      Did u mention on .pro file that your project is a console ?
      If so can you reproduce the error ?

      1 Reply Last reply
      0
      • P Offline
        P Offline
        Pitchou
        wrote on last edited by
        #3

        Hi. yes, my .pro file has the console config.

        @QT += core

        TARGET = ptest
        CONFIG += console
        CONFIG -= app_bundle

        TEMPLATE = app@

        To use QPrinter (with Qt > 4.4), it needs QPaintDevice. This one needs QApplication which needs a server X connexion. That is I have deduced by testing only QPrinter in a test programm like this :

        @#include <QtCore/QCoreApplication>
        #include <Qt/qprinter.h>
        #include <iostream>

        using namespace std;

        int main(int argc, char *argv[])
        {
        QCoreApplication a(argc, argv);
        cout << "step 1" << endl;
        QPrinter pt(QPrinter::HighResolution);
        cout << "step 2" << endl;
        }@

        The programm abord before the step 2 :

        bq. step 1
        QPrinter: Must construct a QApplication before a QPaintDevice
        Aborted

        I don't know how to fix it because I can't change Qt version on the server and I think there isn't a "QPrinter like" class which do the same thing without needing server X connexion...

        1 Reply Last reply
        0
        • B Offline
          B Offline
          bjanuario
          wrote on last edited by
          #4

          QPrint is now a QWidget class, so you are not able to print without gui. But you can do in a old fashion c++ way. Here some sample:
          @#include <iostream.h>
          #include <fstream.h>
          #include <conio.h>

          void main(void)
          {
          clrscr();
          char filename[13];
          char ch;
          cout<<"\nEnter the text file name:";
          cin.getline(filename,13);
          cout<<"\n";
          ifstream fin(filename, ios::in);
          if(!fin)
          {
          cerr<<"\n Cannot open file !\n";
          getch();
          return;
          }
          char const * const PrinterName = "usb001:"; // Identify the printer port.
          ofstream printer(PrinterName); // Open the printer stream.
          if(!printer) // Ensure the printer stream opened ok.
          {
          cerr << "\a\n\tERROR: Unable to open " << PrinterName << endl;
          cout << "\n\tPress the [ENTER] key to return ";
          getch();
          return;
          }

          while (! fin.eof())
          {
              fin.get(ch);
              printer << (ch);
          }
          
          printer << endl << ends << flush;
          printer.close(); // Finish the print job by closing the printer stream.
          fin.close();
          
          // Send a carriage return and a formfeed to the printer so that: a) this
          // print job is ejected from the printer, and b) the next print job starts
          // at the top left corner of the page.
          
          fin.close(); // Close file./
          cout << "\n\n\t\t\t\Printing...";
          cout << "\n\n\ Please press the [ENTER] key to return";
          getch();
          
          return;
          

          }@

          1 Reply Last reply
          0
          • A Offline
            A Offline
            AcerExtensa
            wrote on last edited by
            #5

            You can't print without GUI using any of known printing library... GhostScript also needs X libraries... You can use framebuffers to do it in console mode, but you will need GUI libs in any way....

            P.S.: Only Dot Printers do not need GUI to print ;)

            God is Real unless explicitly declared as Integer.

            1 Reply Last reply
            0
            • B Offline
              B Offline
              bjanuario
              wrote on last edited by
              #6

              [quote author="AcerExtensa" date="1370616218"]You can't print without GUI using any of known printing library... GhostScript also needs X libraries... You can use framebuffers to do it in console mode, but you will need GUI libs in any way....

              P.S.: Only Dot Printers do not need GUI to print ;)[/quote]

              U can print in Linux in every kind of printer without gui! U just need to have proper driver on kernel if not paralel or serial printer, is nothing to do about if is a dot printer or laser ... it's the port and driver issues.

              1 Reply Last reply
              0
              • A Offline
                A Offline
                AcerExtensa
                wrote on last edited by
                #7

                @
                [quote author="bjanuario" date="1370619483"]
                U can print in Linux in every kind of printer without gui! U just need to have proper driver on kernel if not paralel or serial printer, is nothing to do about if is a dot printer or laser ... it's the port and driver issues.[/quote]
                @

                No, you can't :) all console printers uses framebuffers.... sure you can send to printer some kind of scratch and let print it, but we a talking not just physical printing... you can't generate print-page without X!

                God is Real unless explicitly declared as Integer.

                1 Reply Last reply
                0
                • B Offline
                  B Offline
                  bjanuario
                  wrote on last edited by
                  #8

                  If u use graphics, for sure this will be kind tricky, but some printers support decode chars that then print graphics even without X, for text is straight on. I can assure u because we use this on linux ;) On windows i dunno if is possible ...

                  [quote author="AcerExtensa" date="1370639879"]@
                  [quote author="bjanuario" date="1370619483"]
                  U can print in Linux in every kind of printer without gui! U just need to have proper driver on kernel if not paralel or serial printer, is nothing to do about if is a dot printer or laser ... it's the port and driver issues.[/quote]
                  @

                  No, you can't :) all console printers uses framebuffers.... sure you can send to printer some kind of scratch and let print it, but we a talking not just physical printing... you can't generate print-page without X! [/quote]

                  1 Reply Last reply
                  0
                  • A Offline
                    A Offline
                    AcerExtensa
                    wrote on last edited by
                    #9

                    Tell us what do you use and how do you do it! Maybe you have something what peoples with PostScript printers all other the world are looking for...

                    God is Real unless explicitly declared as Integer.

                    1 Reply Last reply
                    0
                    • P Offline
                      P Offline
                      Pitchou
                      wrote on last edited by
                      #10

                      So, if I undestand what you said... Before Qt4.4, it was possible to use QPrinter to generate a PDF file but now, with Qt I can't print a PDF file with QPrinter if my Linux doesn't have a GUI ? ...

                      Do you know another way to print a QImage in a PDF file, without using GUI ?

                      1 Reply Last reply
                      0
                      • A Offline
                        A Offline
                        AcerExtensa
                        wrote on last edited by
                        #11

                        QImage and QPrinter inherits QPaintDevice, and QPaintDevice is an GUI Class. So you need QtGUI library if you would like to use this two classes.

                        God is Real unless explicitly declared as Integer.

                        1 Reply Last reply
                        0
                        • P Offline
                          P Offline
                          Pitchou
                          wrote on last edited by
                          #12

                          I have QtGUI library installed on the OS which is OpenSuse 11. But it's a server OS and it doesn't have server X connexion ...

                          So, I'm happy :/ My app can't work on the server.

                          Thanks for your answer.

                          1 Reply Last reply
                          0
                          • A Offline
                            A Offline
                            AcerExtensa
                            wrote on last edited by
                            #13

                            you can still use virtual framebuffer like Xvfb

                            God is Real unless explicitly declared as Integer.

                            1 Reply Last reply
                            0
                            • P Offline
                              P Offline
                              Pitchou
                              wrote on last edited by
                              #14

                              Thanks AcerExtensa ! XVFB was a good idea and now, the app works well ! :D
                              This thread can be closed

                              1 Reply Last reply
                              0
                              • A Offline
                                A Offline
                                AcerExtensa
                                wrote on last edited by
                                #15

                                Please add prefix "[SOLVED]" to the left of the topic subject. Thanks!

                                God is Real unless explicitly declared as Integer.

                                1 Reply Last reply
                                0
                                • F Offline
                                  F Offline
                                  fernando.lopes
                                  wrote on last edited by
                                  #16

                                  Hi Pitchou.
                                  How did you use XVFB to create the QPrinter?

                                  I need to make a PDF printer in a console application and I get the same error you got.

                                  1 Reply Last reply
                                  0
                                  • P Offline
                                    P Offline
                                    Pitchou
                                    wrote on last edited by
                                    #17

                                    Hi Fernando, I changed my way. I do not use XVFB anymore because I found a solution to use QPriner without serverX.

                                    My error was to use QApplication, which require a serverX. I found a trick !

                                    When you use QApplication, I think you use it like this :
                                    @QApplication a(argc, argv,);@

                                    Change it to have something like this : @QApplication a(argc, argv, false);@

                                    The false attribute will force the program to work without a serverX and you can use QPrinter without any problem

                                    1 Reply Last reply
                                    0
                                    • F Offline
                                      F Offline
                                      fernando.lopes
                                      wrote on last edited by
                                      #18

                                      Thank you Pitchou!
                                      It is working now! :D

                                      1 Reply Last reply
                                      0

                                      • Login

                                      • Login or register to search.
                                      • First post
                                        Last post
                                      0
                                      • Categories
                                      • Recent
                                      • Tags
                                      • Popular
                                      • Users
                                      • Groups
                                      • Search
                                      • Get Qt Extensions
                                      • Unsolved