Results 1 to 3 of 3

Thread: How to print a data in model?

  1. #1
    Join Date
    Apr 2009
    Posts
    75
    Thanks
    15
    Qt products
    Qt4
    Platforms
    Windows

    Default How to print a data in model?

    Hello,

    I've a one simple issue.

    I've a data in model from model-viev-delegate, for example:
    Qt Code:
    To copy to clipboard, switch view to plain text mode 
    and I would like to print this data. Is there any simple way to do this? Or have I print table "step by step" using drawText, drawRect (unfortunately it isn't convenient solve) etc...

    Thank you very much.
    Last edited by TomASS; 22nd August 2009 at 21:25. Reason: correct the title

  2. #2
    Join Date
    Jan 2006
    Location
    Innsbruck, Austria
    Posts
    62
    Thanks
    10
    Thanked 7 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to print a data in model?

    You might generate HTML code and use a QTextDocument to print it. For example:

    Qt Code:
    1. void MyClass::print()
    2. {
    3. QString html;
    4. html = "<html><body><table border=\"0\">";
    5. for(int row = 0; row < m_model->rowCount(); row++) {
    6. html += "<tr>";
    7. for(int column = 0; column < m_model->columnCount(); column++) {
    8. QString data = m_model->data(m_model->index(row, column), Qt::DisplayRole).toString();
    9. html += "<td>" + data + "</td>";
    10. }
    11. html += "</tr>";
    12. }
    13. html += "</table></body></html>";
    14.  
    15. QPrinter printer;
    16. QPrintDialog *dialog = new QPrintDialog(&printer);
    17. if(dialog->exec() == QDialog::Accepted) {
    18. QTextDocument document;
    19. document.setHtml(html);
    20. document.print(&printer);
    21. }
    22. }
    To copy to clipboard, switch view to plain text mode 

  3. The following 2 users say thank you to vfernandez for this useful post:

    numbat (24th August 2009), TomASS (24th August 2009)

  4. #3
    Join Date
    Apr 2009
    Posts
    75
    Thanks
    15
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to print a data in model?

    Thank you! It's working!

Similar Threads

  1. Custom Model Advice Requested
    By mclark in forum Qt Programming
    Replies: 3
    Last Post: 18th September 2008, 16:26
  2. Clearing data model
    By db in forum Newbie
    Replies: 6
    Last Post: 16th February 2008, 13:10
  3. QMap model data
    By gyre in forum Newbie
    Replies: 3
    Last Post: 9th December 2007, 22:19
  4. Data model
    By steg90 in forum Qt Programming
    Replies: 3
    Last Post: 17th September 2007, 12:14
  5. Informing a model its data has changed
    By saknopper in forum Newbie
    Replies: 3
    Last Post: 17th January 2007, 19:58

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.