PDA

View Full Version : Printing problemQt3.3.5



vermarajeev
30th January 2007, 12:29
Hi guys,
This is a problem related to printing.
I have a QTextEdit in RichText format. I have some information on the textedit with different formats like font, color, size etc.

According to my assumption whatever I type in the textEdit, the same should be printed on the printer page. But if I put some text at one end and some text at other end, the line gets wrapped to next line. Why is this unusual behaviour happening????

I'm attaching a test.bmp file for clearer picture.

thanks in advance

vermarajeev
31st January 2007, 04:31
Guys please help!!!!!

I know people in this forum who are expert in Qt and I think this is not going to be tough for them to answer...

Waiting for a reply

jacek
31st January 2007, 17:32
if I put some text at one end and some text at other end, the line gets wrapped to next line.
How do you put that text at the end of a line? Do you alter some tag attributes or just add white spaces?

vermarajeev
1st February 2007, 04:35
How do you put that text at the end of a line? Do you alter some tag attributes or just add white spaces?


How do you put that text at the end of a line? Do you alter some tag attributes or just add white spaces?

Hi jacek, thanks for your reply

Yes I just add white space by pressing spacebar on my keyboard and put the text at the end. The other interesting thing is that when I increase the point size of the font to say 18, the line doesnt get wrapped to next line. If I give small point size to say 9 then the line gets wrapped.

As I know (WYSIWYG) "What you see is what you get", Whatever I type on TextEdit the same should be seen while printing also but I dont understand why the line is getting wrapped. The user wont be happy with this unusual behaviour

I have also set the textEdit width and height to that of the printer. Here is a sample piece of code..


QPainter p;
p.begin( &printer );
QPaintDeviceMetrics metrics( &printer );
double dpmm = (double)metrics.width() / (double)metrics.widthMM(); //this is dots per mm

double aspect = ( (double)metrics.height() / (double)metrics.heightMM() ) //this is aspect ratio
// Create a ratio which basically shows the size of the printer
// canvas in comparison to the 72dpi postscript canvas.
double ratio = PRINTERDPI/PostscriptDPI;

QPoint margin = mappings.marginPoint();
int textEditHeight = 0;
if(myTextEdit) //myTextEdit is the QTextEdit declared as a member variable
textEditHeight = myTextEdit->height() * ratio ;

// print all the pages, looping by column first
bool firstPage = true;
for( int y = minimumPageNumber.y(); y <= maximumPageNumber.y(); ++y )
for( int x = minimumPageNumber.x(); x <= maximumPageNumber.x(); ++x )
{
// do a newPage() before printing the current page
// (except for the first page)
if( firstPage )
{
firstPage = false;
}
else
{
printer.newPage();
}
changePage( x, y );
//check if header is available to be printed
if(myTextEdit)
{
int fontSz = (myTextEdit->fontInfo().pointSize());
fontSz *= printerMap.getScaling(); //printerMap is mappings to printer where I scale
//the font
if( fontSz < 6 )
fontSz = 6;

QFont headerFont = QFont( myTextEdit->family(), fontSz );
p.setFont( headerFont );

QSimpleRichText richText( myTextEdit->text(),
headerFont,
myTextEdit->context(),
myTextEdit->styleSheet(),
myTextEdit->mimeSourceFactory(),
myTextEdit->height() );
richText.setWidth( &p, myTextEdit->width() );
richText.draw( &p, margin.x(), margin.y(),headerView, colorGroup() );
}

This is just a sample..... I have taken care of everything while printing but also the textEdit text gets wrapped... Please help me with this issue, I'll be thankful to you.

jacek
1st February 2007, 21:41
Yes I just add white space by pressing spacebar on my keyboard and put the text at the end.
In that case QTextEdit width must be exactly the same as paper width.


QSimpleRichText richText( myTextEdit->text(),
headerFont,
myTextEdit->context(),
myTextEdit->styleSheet(),
myTextEdit->mimeSourceFactory(),
myTextEdit->height() );
richText.setWidth( &p, myTextEdit->width() );
IMO the problem is here. Printer pixels might have different size than screen pixels. Try multiplying myTextEdit->width() by (printer dpi / screen dpi).

vermarajeev
2nd February 2007, 04:41
In that case QTextEdit width must be exactly the same as paper width.
IMO the problem is here. Printer pixels might have different size than screen pixels. Try multiplying myTextEdit->width() by (printer dpi / screen dpi).

Hi jacek,

I just tried out this

QPaintDeviceMetrics metrics( &printer );
QRect headerView = getHeaderRect(metrics);

QSimpleRichText richText( myTextedit->text(), headerFont, myTextedit->context(),
myTextedit->styleSheet(),
myTextedit->mimeSourceFactory(),
headerView.height() );
richText.setWidth( &p, headerView.width() );
richText.draw( &p, margin.x(), margin.y(), headerView, colorGroup() );


//defination
QRect ChemCanvas::getHeaderRect(QPaintDeviceMetrics metrics)
{
QPoint margin = mappings.marginPoint();
double ratio = mappings.dotsPerMm() * MM_PER_IN / PostscriptDPI;
double headerHt;
headerHt = (double)myTextedit->height();
headerHt *= ratio;

QRect view( margin.x(), margin.y(), metrics.width()-2*margin.x() ,
headerHt);
return view;
}

This is just a part of the code that I have written, I'm taking care of mapping printer width and height to that of the QTextEdit width and height.... but the result is same. The text gets wrapped to next line...

Waiting eagerly for your suggestions

jacek
2nd February 2007, 17:22
double ratio = mappings.dotsPerMm() * MM_PER_IN / PostscriptDPI;
double headerHt;
headerHt = (double)myTextedit->height();
headerHt *= ratio;
Shouldn't it be "headerHt /= ratio;"?

What is the value of "ratio"? Also check whether the resulting width isn't greater than page width.

vermarajeev
6th February 2007, 05:12
Shouldn't it be "headerHt /= ratio;"?

What is the value of "ratio"?

Hi jacek,
thanks for your reply

"The ratio basically shows the size of the printer canvas in comparison to the 72dpi postscript canvas."

The ratio's value is 8.3294 on my system. The headerHt's value in pixel is 45.00 which I map it to printer's height, so I have multiplied headerHt with ratio and the value headerHt is 374.826 after multiplying with ratio.


Also check whether the resulting width isn't greater than page width.
I have created a dialog called page setup where the user can change the page orientation, size etc to anything he/she wants. Now to reflect that on the Drawing area(QFrame) I have created a class called mappings which maps the user entered page orientation or size to that of the drawing area(QFrame).

When I print the contents on the printer, first I'm getting page size and orientation of the printer, which I map that to the drawing area(QFrame). Those values I update in mappings class using setPageSize and setOrientation methods. then I make the drawing area(QFrame) as big as printer's size... print the entire information on the printer then resize the drawing area back to its normal size ( which is same as it was before printing ). This basically takes care of myTextEdit's width to be same as printer's width.

Please let me know If i'm not clear.
Waiting for a reply.

jacek
6th February 2007, 21:19
You could draw a rectangle which width and height are equal to a half of corresponding page dimension to verify that you set the size correctly.

If everything is OK here, then I would check margins and font metrics.

vermarajeev
7th February 2007, 04:30
You could draw a rectangle which width and height are equal to a half of corresponding page dimension to verify that you set the size correctly.

If everything is OK here, then I would check margins and font metrics.

Hi jacek, lets make things easier,

There is a sample program provided in QT Assistant. This will help you to unserstand what my problem is???? I'm facing the same problem with my application

Just run Qt/3.3.5/examples/textedit. Enter some text at the lefmost position, then enter some other text at rightmost position by hitting spacebar. Just print the contents on a pdf to check if it shows you the same thing. On my system the line is getting wrapped.

Hope this will get you an exact idea about my problem

jacek
7th February 2007, 20:22
Just run Qt/3.3.5/examples/textedit.
You could have told me about this example earlier. ;)

After playing with it a bit, I think that the best solution is to make sure that logical width of text edit is constant and equal to page width. Unfortunately for some reason it's not enough, either the problem is in some rounding-off errors or font metrics are not the same on the screen and on the paper (which might mean that you have to use different font size).