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. How can i print a QImage to a thermal printer
Forum Updated to NodeBB v4.3 + New Features

How can i print a QImage to a thermal printer

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 3 Posters 721 Views
  • 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.
  • M Offline
    M Offline
    Mohan Raj
    wrote on last edited by
    #1

    Here i have converted my QImage to QByteArray formal then it showing some error on printer write...anyone pls suggest me any correction or other ways to print image in thermal printer```

    int MainWindow::on_pushbutton_graphprint_clicked()
    {
        int num = 230;
        char character = char(num);
        Printer *p = new Printer();
        std::cout << "Trying to open port" << std::endl;
        bool res = p->open("/dev/ttyS0");
        std::cout << "Status: " << res << std::endl;
        if (!res) {
            std::cerr << "Error opening port, aborting" << std::endl;
            return (0);}
        quint8 bytesWritten = p->write(arr);
    
            if (bytesWritten == -1) {
                qDebug() << "Error writing data to the printer.";
                p->close();
                return 1;
            } else {
                qDebug() << "Successfully sent " << bytesWritten << " bytes to the printer.";
            }
    
    
            p->close();
    
            return 0; \
    }
    
    
    jsulmJ 2 Replies Last reply
    0
    • M Mohan Raj

      Here i have converted my QImage to QByteArray formal then it showing some error on printer write...anyone pls suggest me any correction or other ways to print image in thermal printer```

      int MainWindow::on_pushbutton_graphprint_clicked()
      {
          int num = 230;
          char character = char(num);
          Printer *p = new Printer();
          std::cout << "Trying to open port" << std::endl;
          bool res = p->open("/dev/ttyS0");
          std::cout << "Status: " << res << std::endl;
          if (!res) {
              std::cerr << "Error opening port, aborting" << std::endl;
              return (0);}
          quint8 bytesWritten = p->write(arr);
      
              if (bytesWritten == -1) {
                  qDebug() << "Error writing data to the printer.";
                  p->close();
                  return 1;
              } else {
                  qDebug() << "Successfully sent " << bytesWritten << " bytes to the printer.";
              }
      
      
              p->close();
      
              return 0; \
      }
      
      
      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @Mohan-Raj said in How can i print a QImage to a thermal printer:

      showing some error on printer write

      What error?
      Is your printer connected via serial to your machine or why do you use /dev/ttyS0 ?

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      M 1 Reply Last reply
      0
      • jsulmJ jsulm

        @Mohan-Raj said in How can i print a QImage to a thermal printer:

        showing some error on printer write

        What error?
        Is your printer connected via serial to your machine or why do you use /dev/ttyS0 ?

        M Offline
        M Offline
        Mohan Raj
        wrote on last edited by
        #3

        @jsulm hi!!! thanks for your reply.
        yes, i have connected my printer with raspberry pi and main board communication is made via serial using uart.

        jsulmJ 1 Reply Last reply
        0
        • M Mohan Raj

          @jsulm hi!!! thanks for your reply.
          yes, i have connected my printer with raspberry pi and main board communication is made via serial using uart.

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @Mohan-Raj You forgot to answer my first question.
          Also, are you able to print anything on that printer via your OS?

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          M 1 Reply Last reply
          0
          • jsulmJ jsulm

            @Mohan-Raj You forgot to answer my first question.
            Also, are you able to print anything on that printer via your OS?

            M Offline
            M Offline
            Mohan Raj
            wrote on last edited by
            #5

            @jsulm
            its showing error write function.
            i can print other text data using qt but sticking with printing image

            1 Reply Last reply
            0
            • M Mohan Raj

              Here i have converted my QImage to QByteArray formal then it showing some error on printer write...anyone pls suggest me any correction or other ways to print image in thermal printer```

              int MainWindow::on_pushbutton_graphprint_clicked()
              {
                  int num = 230;
                  char character = char(num);
                  Printer *p = new Printer();
                  std::cout << "Trying to open port" << std::endl;
                  bool res = p->open("/dev/ttyS0");
                  std::cout << "Status: " << res << std::endl;
                  if (!res) {
                      std::cerr << "Error opening port, aborting" << std::endl;
                      return (0);}
                  quint8 bytesWritten = p->write(arr);
              
                      if (bytesWritten == -1) {
                          qDebug() << "Error writing data to the printer.";
                          p->close();
                          return 1;
                      } else {
                          qDebug() << "Successfully sent " << bytesWritten << " bytes to the printer.";
                      }
              
              
                      p->close();
              
                      return 0; \
              }
              
              
              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @Mohan-Raj said in How can i print a QImage to a thermal printer:

              Printer *p = new Printer();

              What is Printer?
              You still did not post the exact error you get -why?
              Try to save your QImage to a file and see whether you can open it in an image viewer.
              You also did not say (or show any code) how you're converting the image to QByteArray.
              If you want to get support you should provide enough information...

              Also, why do you convert the QImage to byte array? You can print a QImage like shown here: https://stackoverflow.com/questions/8310681/how-to-print-image-file-from-a-printer-using-qt

              QImage img(fileName);
              QPainter painter(&printer);
              painter.drawImage(QPoint(0,0),img);
              painter.end();
              

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              M 1 Reply Last reply
              0
              • jsulmJ jsulm

                @Mohan-Raj said in How can i print a QImage to a thermal printer:

                Printer *p = new Printer();

                What is Printer?
                You still did not post the exact error you get -why?
                Try to save your QImage to a file and see whether you can open it in an image viewer.
                You also did not say (or show any code) how you're converting the image to QByteArray.
                If you want to get support you should provide enough information...

                Also, why do you convert the QImage to byte array? You can print a QImage like shown here: https://stackoverflow.com/questions/8310681/how-to-print-image-file-from-a-printer-using-qt

                QImage img(fileName);
                QPainter painter(&printer);
                painter.drawImage(QPoint(0,0),img);
                painter.end();
                
                M Offline
                M Offline
                Mohan Raj
                wrote on last edited by
                #7

                @jsulm That printer is like a library used for thermal printer using that p object i'm using write, close like functions.
                to print I can only send the image data as a Byte value only for serial communication and also i can print only monochrome format image in a thermal printer so i have converted it to bytearray values. so, i can't use any other like painter or webview operations

                below i have added how i converted to bytearray...

                 QImage graphImage(ui->customPlot_4->size(), QImage::Format_Mono);
                    ui->customPlot_4->render(&graphImage);
                    QString filePath = "/home/pi/git/graph.png";
                    if (graphImage.save(filePath)) {
                        qDebug() << "Image saved successfully.";
                    } else {
                        qDebug() << "Failed to save the image.";
                    }
                    arr = QByteArray::fromRawData((const char*)graphImage.bits(), graphImage.byteCount());
                    if(arr.isEmpty() || arr.isNull())
                            qDebug("empty");
                    else
                        qDebug("converted to QByteArray");
                
                jsulmJ C 2 Replies Last reply
                0
                • M Mohan Raj

                  @jsulm That printer is like a library used for thermal printer using that p object i'm using write, close like functions.
                  to print I can only send the image data as a Byte value only for serial communication and also i can print only monochrome format image in a thermal printer so i have converted it to bytearray values. so, i can't use any other like painter or webview operations

                  below i have added how i converted to bytearray...

                   QImage graphImage(ui->customPlot_4->size(), QImage::Format_Mono);
                      ui->customPlot_4->render(&graphImage);
                      QString filePath = "/home/pi/git/graph.png";
                      if (graphImage.save(filePath)) {
                          qDebug() << "Image saved successfully.";
                      } else {
                          qDebug() << "Failed to save the image.";
                      }
                      arr = QByteArray::fromRawData((const char*)graphImage.bits(), graphImage.byteCount());
                      if(arr.isEmpty() || arr.isNull())
                              qDebug("empty");
                      else
                          qDebug("converted to QByteArray");
                  
                  jsulmJ Offline
                  jsulmJ Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @Mohan-Raj Please read documentation (https://doc.qt.io/qt-6/qbytearray.html#fromRawData):
                  "The bytes are not copied. The QByteArray will contain the data pointer. The caller guarantees that data will not be deleted or modified as long as this QByteArray and any copies of it exist that have not been modified.".
                  Since your graphImage is a local variable it does not exist anymore when you're using the byte array. So, the image data in the byte array is invalid then.

                  "so, i can't use any other like painter or webview operations" - are you sure? This class seems to support QByteArray - is it a subclass of QPrinter or QPaintDevice?

                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  0
                  • M Mohan Raj

                    @jsulm That printer is like a library used for thermal printer using that p object i'm using write, close like functions.
                    to print I can only send the image data as a Byte value only for serial communication and also i can print only monochrome format image in a thermal printer so i have converted it to bytearray values. so, i can't use any other like painter or webview operations

                    below i have added how i converted to bytearray...

                     QImage graphImage(ui->customPlot_4->size(), QImage::Format_Mono);
                        ui->customPlot_4->render(&graphImage);
                        QString filePath = "/home/pi/git/graph.png";
                        if (graphImage.save(filePath)) {
                            qDebug() << "Image saved successfully.";
                        } else {
                            qDebug() << "Failed to save the image.";
                        }
                        arr = QByteArray::fromRawData((const char*)graphImage.bits(), graphImage.byteCount());
                        if(arr.isEmpty() || arr.isNull())
                                qDebug("empty");
                        else
                            qDebug("converted to QByteArray");
                    
                    C Offline
                    C Offline
                    ChrisW67
                    wrote on last edited by
                    #9

                    @Mohan-Raj Is there a reason for posting the same problem under two separate accounts? https://forum.qt.io/post/771275

                    Have you read the (surprisingly informative) manual? Have you sent the commands to put the printer into bit image mode, set bit density, image size, before sending the image data?

                    What arrangement of bits and scanlines does the printer expect? QImage::Format_Mono: "The image is stored using 1-bit per pixel. Bytes are packed with the most significant bit (MSB) first." The format described in the manual is not arranged the same way.

                    What version of Qt are you using? QImage::byteCount() was marked, "This function is obsolete. It is provided to keep old source code working. We strongly advise against using it in new code."

                    1 Reply Last reply
                    2

                    • Login

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