Printing a label



  • I have a picture that is shown in my label and would like to print the label.

    QPrinter printer;
    printer.setPrinterName("Sony UP-D897");
    QPainter painter(&printer);
    ui->label_first->render(&painter);

    which gives me a blank page, and there is not much information online regarding printing labels. How would I be able to print this picture?


  • Qt Champions 2016

    hi
    Why not try to draw it directly?
    painter.drawPixmap(xxx)
    That said, render should work, but it draw it self to this device
    and not sure how x,y translate. besides you might want to scale it any way
    as printers have much higher DPI so it might become small.

    anyway, here is a function that take screenshot, scale it and print it

    void MainWindow::printPage() {
      QPixmap pix = QPixmap::grabWindow(QApplication::desktop()->winId());
      QPrinter printer(QPrinter::HighResolution);
      printer.setOrientation(QPrinter::Landscape);
      QPainter painter;
      painter.begin(&printer);
      double xscale = printer.pageRect().width() / double(pix.width());
      double yscale = printer.pageRect().height() / double(pix.height());
      double scale = qMin(xscale, yscale);
      painter.translate(printer.paperRect().x() + printer.pageRect().width() / 2,
                        printer.paperRect().y() + printer.pageRect().height() / 2);
      painter.scale(scale, scale);
      painter.translate(-width() / 2, -height() / 2);
      painter.drawPixmap(0, 0, pix);
      painter.end();
    }
    


  • @mrjj Hey thanks for the reply,

    I tried that as well and played around with the x and y but yet it still gave me a blank print out. Is there any other way other than using QPainter? Because for the textedit widget all you have to do is ui->textedit->print(&printer) and prints accordingly and works but when I try a label all it has is render.



  • @mrjj
    Heres the code:

      QPrinter printer;
      printer.setPrinterName("Sony UP-D897");
      QPainter painter;
      painter.begin(&printer);
      double xscale = printer.pageRect().width() / double(outPixmap.width());
      double yscale = printer.pageRect().height() / double(outPixmap.height());
      double scale = qMin(xscale, yscale);
      painter.translate(printer.paperRect().x() + printer.pageRect().width() / 2,
                        printer.paperRect().y() + printer.pageRect().height() / 2);
      painter.scale(scale, scale);
      painter.translate(-width() / 2, -height() / 2);
      painter.drawPixmap(0, 0, outPixmap);
      painter.end();
    ui->label_first->render(&painter);

  • Qt Champions 2016

    hi
    well textedit has a print function
    but a normal display widgets do not have .

    Its the normal way to draw on a painter connected to painter.

    Are u sure outPixmap contains anything?

    did u try the sample unchanged so it grabs screenshot?
    did that print?

    I think something else is up. code looks good. works here for a screenshot.



  • @mrjj
    Yea it does, just ran my code with a different label to see if outPixmap contained an image and it did.
    I tried the screenshot method and it worked.
    Which comes to a dilemma since my outPixmap did contain an image of QPixmap class since it displayed to a label before I ran it through the QPrinter but printed out an empty screen.
    But when I did the screenshot method then it printed something.

    Let me show you some more code:

    in my .h file:
    QPixmap outPixmap = QPixmap();

    .cpp file:
    ui->label_second->setPixmap(outPixmap);
    QPrinter printer;
    printer.setPrinterName("Sony UP-D897");
    QPainter painter;
    painter.begin(&printer);
    double xscale = printer.pageRect().width() / double(outPixmap.width());
    double yscale = printer.pageRect().height() / double(outPixmap.height());
    double scale = qMin(xscale, yscale);
    painter.translate(printer.paperRect().x() + printer.pageRect().width() / 2,
    printer.paperRect().y() + printer.pageRect().height() / 2);
    painter.scale(scale, scale);
    painter.translate(-width() / 2, -height() / 2);
    painter.drawPixmap(0, 0, outPixmap);
    painter.end();

    In an earlier function I take a picture of a livestream -> store it into database -> then show it on a label through outByteArray = query.value(0).toByteArray();
    outPixmap.loadFromData( outByteArray );

    which is where I got ui->label_second->setPixmap(outPixmap); and it displayed the image on the label before I ran it through the printer.


  • Qt Champions 2016

    Hi
    Ok, so we agree that outPixmap does contain an image?
    That is 100% sure.?
    U show it , just before u print it every time.

    it should work then, if it print with screen shot.

    How big is the picture?

    the sample should scale image up so it should not be
    a case of image simply too small to really see.

    Im kinda out of ideas as only issue seems if outPixmap is not really valid.

    I can see that you new it
    QPixmap outPixmap = QPixmap();

    where do u make it contain anything?
    (i know u used to load it)
    but in this code shown, where do u draw anything?



  • @mrjj Here is the function it stores the outPixmap and yes outPixmap contains an image when I run the print function. The only issue might be that I scaled the image accordingly to the size of the label then stored it into my database, other than that I dont see any issues.

    void xrayImaging::processCapturedImage(int requestId, const QImage &img)
    {
    pRegister.connOpen();
    QSqlQuery query = QSqlQuery(pRegister.db);
    int w=ui->label_first->width();
    int h=ui->label_first->height();
    QImage scaledImage = img.scaled(w,h,Qt::KeepAspectRatio,Qt::SmoothTransformation);

    QBuffer inbuffer(&inByteArray);
    inbuffer.open(QIODevice::WriteOnly);
    scaledImage.save(&inbuffer,"PNG");
    
    if(patientid1 == ""){
        if(!query.exec("insert into patientTable (Prefix,FirstName,Middle,LastName,Suffix,DateOfBirth,Gender,Physician,Description,Anatomy,Accession,DateTime) values ('','','','','','','','','','','','')"))
           QMessageBox::critical(this,tr("Patient Database"),query.lastError().text());
        if(!query.exec("select PatientId,LastName from patientTable order by PatientId desc limit 1"))
           QMessageBox::critical(this,tr("Patient Database"),query.lastError().text());
        if(query.next()){
         patientid1 = query.value(0).toString();
         lname = query.value(1).toString();
           }
        query.prepare("INSERT INTO imageTable (PatientId,LastName,Image) VALUES('"+patientid1+"','"+lname+"',:imageData)");
        query.bindValue(":imageData", inByteArray);
        if( !query.exec() )
            qDebug() << "Error inserting Image into table:\n" << query.lastError();
        if( !query.exec( "SELECT Image from imageTable where PatientId = '"+patientid1+"' order by ImageId DESC limit 3"))
            qDebug() << "Error getting image from table:\n" << query.lastError();
        if(query.next()){
            outByteArray = query.value(0).toByteArray();
            outPixmap.loadFromData( outByteArray );
              }
        if(query.next()){
            outByteArray= query.value(0).toByteArray();
         outPixmap2.loadFromData( outByteArray );
        }
        if(query.next()){
            outByteArray= query.value(0).toByteArray();
            outPixmap3.loadFromData( outByteArray );
        }
    }
    

    else {
    query.prepare("select LastName from patientTable where PatientId = '"+patientid1+"'");
    if (query.exec()){
    while(query.next()){
    lname = query.value(0).toString();
    }
    }
    query.prepare("INSERT INTO imageTable (PatientId,LastName,Image) VALUES('"+patientid1+"','"+lname+"',:imageData)");
    query.bindValue(":imageData", inByteArray);
    if( !query.exec() )
    qDebug() << "Error inserting Image into table:\n" << query.lastError();
    if( !query.exec( "SELECT Image from imageTable where PatientId = '"+patientid1+"' order by ImageId DESC limit 3"))
    qDebug() << "Error getting image from table:\n" << query.lastError();

    if(query.next()){
     outByteArray = query.value(0).toByteArray();
     outPixmap.loadFromData( outByteArray );
       }
    if(query.next()){
    outByteArray= query.value(0).toByteArray();
    outPixmap2.loadFromData( outByteArray );
    }
    if(query.next()){
        outByteArray= query.value(0).toByteArray();
        outPixmap3.loadFromData( outByteArray );
    }
    

    }

    pRegister.connClose();
    ui->label_first->setPixmap(outPixmap);
    ui->label_second->setPixmap(outPixmap2);
    ui->label_third->setPixmap(outPixmap3);
    

    }


  • Qt Champions 2016

    well the sample print code also scales it up.
    I wonder if something could go wrong there.
    Since i trust u triple check image is fine so
    maybe we kill it with scale.

    Since screenshot worked, we can safely say
    pixmap ok
    printer ok.
    so error must be in the print function?

    Have you checked the values etc?

    Im out of ideas as we seemed to have checked all and judging by the code, you are not beginner and
    hence it must be something odd. agree?


  • Qt Champions 2016

    hi
    the code used this->width for translate.
    maybe that did something in your case
    can you try

    void printv2(QPixmap &pix) {
    
      QPrinter printer(QPrinter::HighResolution);
      printer.setOrientation(QPrinter::Landscape);
      printer.setOutputFormat(QPrinter::PdfFormat);
      printer.setPaperSize(QPrinter::A4);
      printer.setOutputFileName("e:/test.pdf"); <<<<<<<<<<<<<<<<<<<<< CHANGE ME
    
      QPainter painter;
      painter.begin(&printer);
    
      double xscale = printer.pageRect().width() / double(pix.width());
      double yscale = printer.pageRect().height() / double(pix.height());
      double scale = qMin(xscale, yscale);
      painter.translate(printer.paperRect().x() + printer.pageRect().width() / 2,
                        printer.paperRect().y() + printer.pageRect().height() / 2);
      painter.scale(scale, scale);
      painter.translate(-pix.width() / 2, -pix.height() / 2);
      painter.drawPixmap(0, 0, pix);
      painter.end();
    
    }
    

    and see if pdf file shows your image?



  • @mrjj said:

    ouble xscale = printer.pageRect().width() / double(pix.width());
    double yscale = printer.pageRect().height() / double(pix.height());
    double scale = qMin(xscale, yscale);
    painter.translate(printer.paperRect().x() + printer.pageRect().width() / 2,
    printer.paperRect().y() + printer.pageRect().height() / 2);
    painter.scale(scale, scale);
    painter.translate(-pix.width() / 2, -pix.height() / 2);
    painter.drawPixmap(0, 0, pix);
    painter.end();

    Bingo! That was the odd problem. Thank you very much for the support!


  • Qt Champions 2016

    @Danielpopo
    super. sorry forgot it was made for screen shot so
    didnt think about it :)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.