PDA

View Full Version : Problems with QString



cyberboy
12th October 2008, 16:09
Hi everybody,

I'm currently working on a project with printing functionality. The result is some kind of excel sheet looking print.

Everything is working fine, except one tiny thing drawing the text in the cell.

This is the code:


#include "tableCellPrinter.h"
#include <QDebug>
#include <QPainter>
#include <QString>

TableCell::TableCell(QColor fillColor, QString text, QObject *parent)
:QObject(parent)
{
qDebug("TableCell::TableCell(): Initialize a new cell");
qDebug() << text;
this->fillColor = fillColor;
this->text = QString (text);
}


void TableCell::draw(QPainter &painter, int offsetLeft, int offsetTop , int width, int height)
{
//start drawing the text
qDebug("TableCell:draw(): start drawing");

painter.drawText(QRect(offsetLeft, offsetTop, width, height), Qt::AlignCenter, this->text);
}

TableCell::~TableCell()
{

qDebug("destruct cell");
qDebug() << this->text;
}

And the header file:

#ifndef TABLECELLPRINTER_H
#define TABLECELLPRINTER_H

#include <QObject>
#include <QColor>
#include <QString>
#include <QRect>

class QPainter;

class TableCell : public QObject
{
Q_OBJECT
public:
TableCell(QColor fillColor, QString text, QObject *parent =0);
void draw(QPainter&, int offsetLeft, int offsetTop , int width, int height);
~TableCell();
QColor fillColor;
QString text;

};

#endif

The program shuts down when I try to call the draw method. I know the problem is something with the this->text code. But I just don't know what it is!

There are no errors and the program compiles just fine. But when I call the printing function it crashes at this point according to the debugger: QPainter::drawText

But the when I modify the drawtext code to draw a static text like "Qt" it works fine.
And the print is previewed in preview(on mac) and the destructor debug message shows the text that I did put in the cell.

Here's a little part of the debugging messages:


TablePrint::TablePrint():start printing
TablePrint::dataPrint():printing row data
TableRow::TableRow(): Start drawing the tables
rect.width:764
TableRow::setHeaderData(): Set the header data
TableColumn::TableColumn(): Define header column
TableCell::TableCell(): Initialize a new cell
"column1"
TableColumn::TableColumn(): Define header column
TableCell::TableCell(): Initialize a new cell
"column2"
TableColumn::TableColumn(): Define header column
TableCell::TableCell(): Initialize a new cell
"column3"
TableRow::setRowData(): set the row data
this->width:764
TableColumn::addCell(): add a cell to the column
TableCell::TableCell(): Initialize a new cell
"column_1_row_1_artikel"
TableColumn::addCell(): add a cell to the column
TableCell::TableCell(): Initialize a new cell
"column_2_row_1_artikel"
TableColumn::addCell(): add a cell to the column
TableCell::TableCell(): Initialize a new cell
"column_3_row_1_artikel"
TableRow::setRowData(): set the row data
this->width:764
TableColumn::addCell(): add a cell to the column
TableCell::TableCell(): Initialize a new cell
"column_1_row_2_artikel"
TableColumn::addCell(): add a cell to the column
TableCell::TableCell(): Initialize a new cell
"column_2_row_2_artikel"
TableColumn::addCell(): add a cell to the column
TableCell::TableCell(): Initialize a new cell
"column_3_row_2_artikel"
TableRow:draw(): start drawing
this->width:764
TableColumn:draw(): start drawing
Drawing area: offsetLeft:10 offsetTop:10 width:100 height:236476672
TableCell:draw(): start drawing
TableColumn:draw(): start drawing
Drawing area: offsetLeft:110 offsetTop:10 width:100 height:236342768
TableCell:draw(): start drawing
TableColumn:draw(): start drawing
Drawing area: offsetLeft:210 offsetTop:10 width:100 height:236343120
TableCell:draw(): start drawing
TableRow:draw(): start drawing
this->width:764
TableColumn:draw(): start drawing
Drawing area: offsetLeft:10 offsetTop:30 width:100 height:236476672
TableCell:draw(): start drawing
TableColumn:draw(): start drawing
Drawing area: offsetLeft:110 offsetTop:30 width:100 height:236342768
TableCell:draw(): start drawing
TableColumn:draw(): start drawing
Drawing area: offsetLeft:210 offsetTop:30 width:100 height:236343120
TableCell:draw(): start drawing
destruct table print
Destruct rows
Destruct column
BestelApplicatie(1165,0xa057afa0) malloc: *** error for object 0xe18532c: Non-aligned pointer being freed
*** set a breakpoint in malloc_error_break to debug
destruct cell
"column1"
destruct cell
"column_1_row_1_artikel"
destruct cell
"column_1_row_2_artikel"
Destruct column
BestelApplicatie(1165,0xa057afa0) malloc: *** error for object 0xe1859cc: Non-aligned pointer being freed
*** set a breakpoint in malloc_error_break to debug
destruct cell
"column2"
destruct cell
"column_2_row_1_artikel"
destruct cell
"column_2_row_2_artikel"
Destruct column
BestelApplicatie(1165,0xa057afa0) malloc: *** error for object 0xe164eec: Non-aligned pointer being freed
*** set a breakpoint in malloc_error_break to debug
destruct cell
"column3"
destruct cell
"column_3_row_1_artikel"
destruct cell
"column_3_row_2_artikel"

You can ignore the extreme height, I'm busy solving that problem too(A)


Does somebody know how to solve this?

greetings,

Cyberboy

aamer4yu
13th October 2008, 07:26
How are you calling TableCell::draw ???
May be theres some prob with the painter reference u are passing

wysota
13th October 2008, 08:18
Maybe this is going to sound stupid, but why do you inherit QObject in your TableCell class?