Click here to Skip to main content
15,885,309 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I am using QIMAGE class of QT for constructing the image and using the COURIER font .

Printer will support 32 characters for line and i am calculating the IMAGE Height using formula total characters in file/ no of characters per line .

But the problem is it is giving the empty spaces after printing of the file.

Please help me how to resolve the issue.

Thank you.

Ashok






test.txt is the input file.i want all its text to correctly fit on the image.
Below is the piece of code i am using.once you execute this,you will get test2.png file.but this file has some extra spaces.

C++
#include <QtGui>
#include <QDebug>
#include <QImage>
#include  <QApplication>
#include <QPainter>
#define MAX_NUM_CHAR 30
//test.txt is any text file,this is input file
#define PATH &quot;/home/narendar/test/QtTest/qtitr/test/naru/test.txt
 int main(int argc,char** argv){
 #define IMAGE_WIDTH 384
 #define FONT_HEIGHT 39

     QApplication app(argc,argv);
     QFile file (PATH);
     int  numLines;
     if ( file.open( QIODevice::ReadOnly ) )
     {
         QTextStream out(&amp;file);
         out.setFieldWidth(100);
         out.setFieldAlignment(QTextStream::AlignCenter);
         QString str = out.readAll();
         int  strLen = str.size();
         // Get number of lines
         numLines =  strLen / MAX_NUM_CHAR ;
         // Get image height
         int  imageHeight;
         imageHeight = FONT_HEIGHT * numLines ;
         // Construct the image with the obtained dimension
          QImage img( 384, imageHeight, QImage::Format_Mono );
          //  QImage img(100,100,QImage::Format_Mono);</pre>
img.fill(1);
         QPainter painter(&img);
         painter.drawText(QRect(0,0,IMAGE_WIDTH,imageHeight),Qt::AlignLeft | Qt::TextWrapAnywhere | Qt::TextIncludeTrailingSpaces,
             str);
    if(img.save("/home/narendar/test/QtTest/qtitr/test/naru/test2.png")){
         qDebug()<<"image saved"<<endl;
     }
         }
     app.exec();
 }
Posted
Updated 9-Aug-11 0:11am
v4
Comments
Richard MacCutchan 9-Aug-11 3:55am    
Without seeing your code it is impossible to guess what you are doing wrong.
Richard MacCutchan 9-Aug-11 6:16am    
This looks like an issue with the QT package and your calculations regarding text, font and image sizes. I would guess that you need to spend some time with your debugger, checking that these values are correct.
ashok1202 9-Aug-11 6:27am    
Hi,
Do you find any wrong logical calculation for the algorithm ?It works fine for small files but problem mainly comes from large file.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900