Results 1 to 3 of 3

Thread: Problems with QString

  1. #1
    Join Date
    Jan 2008
    Posts
    58
    Thanks
    3
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Problems with QString

    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:
    Qt Code:
    1. #include "tableCellPrinter.h"
    2. #include <QDebug>
    3. #include <QPainter>
    4. #include <QString>
    5.  
    6. TableCell::TableCell(QColor fillColor, QString text, QObject *parent)
    7. :QObject(parent)
    8. {
    9. qDebug("TableCell::TableCell(): Initialize a new cell");
    10. qDebug() << text;
    11. this->fillColor = fillColor;
    12. this->text = QString (text);
    13. }
    14.  
    15.  
    16. void TableCell::draw(QPainter &painter, int offsetLeft, int offsetTop , int width, int height)
    17. {
    18. //start drawing the text
    19. qDebug("TableCell:draw(): start drawing");
    20.  
    21. painter.drawText(QRect(offsetLeft, offsetTop, width, height), Qt::AlignCenter, this->text);
    22. }
    23.  
    24. TableCell::~TableCell()
    25. {
    26.  
    27. qDebug("destruct cell");
    28. qDebug() << this->text;
    29. }
    To copy to clipboard, switch view to plain text mode 

    And the header file:
    Qt Code:
    1. #ifndef TABLECELLPRINTER_H
    2. #define TABLECELLPRINTER_H
    3.  
    4. #include <QObject>
    5. #include <QColor>
    6. #include <QString>
    7. #include <QRect>
    8.  
    9. class QPainter;
    10.  
    11. class TableCell : public QObject
    12. {
    13. Q_OBJECT
    14. public:
    15. TableCell(QColor fillColor, QString text, QObject *parent =0);
    16. void draw(QPainter&, int offsetLeft, int offsetTop , int width, int height);
    17. ~TableCell();
    18. QColor fillColor;
    19. QString text;
    20.  
    21. };
    22.  
    23. #endif
    To copy to clipboard, switch view to plain text mode 

    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()rinting 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
    Last edited by cyberboy; 12th October 2008 at 18:11.

  2. #2
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problems with QString

    How are you calling TableCell::draw ???
    May be theres some prob with the painter reference u are passing

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Problems with QString

    Maybe this is going to sound stupid, but why do you inherit QObject in your TableCell class?

Similar Threads

  1. Custom Model Advice Requested
    By mclark in forum Qt Programming
    Replies: 3
    Last Post: 18th September 2008, 16:26
  2. Convert from iso-8859-1 to... Something else :-)
    By Nyphel in forum Qt Programming
    Replies: 4
    Last Post: 7th March 2007, 17:59
  3. QT4 Plugins - problems, problems
    By NormanDunbar in forum Qt Programming
    Replies: 6
    Last Post: 9th May 2006, 15:39
  4. Converting QString to unsigned char
    By salston in forum Qt Programming
    Replies: 3
    Last Post: 24th April 2006, 22:10
  5. [SOLVED] Widget plugin ... how to ?
    By yellowmat in forum Newbie
    Replies: 10
    Last Post: 29th January 2006, 20:41

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.