Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Print QStandardItemModel
Qt 6.11 is out! See what's new in the release blog

Print QStandardItemModel

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 2 Posters 1.4k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    neda
    wrote on last edited by
    #1

    I want to print rows of "QStandardItemModel". I use this code, It works but table is not beautiful. I don't know how to adjust row height and I don't know how to set column widths based on the width of cells in the first row of the table.

    demo

    QPrinter printer;
        printer.setPageSize(QPrinter::A4);
        printer.setFullPage(true);
    
        QPrintDialog *dlg = new QPrintDialog(&printer,0);
        if(dlg->exec() == QDialog::Accepted) {
    
            QPainter painter;
            if (painter.begin(&printer)) {
    			
                painter.translate(0,0);
    			
    			int position = 0;
                int rowCount=newMyModel->rowCount(QModelIndex());
    
                for(int r=0;r<rowCount;r++)
    			{
                    if( position > painter.window().height()-100 ) 
    				{
                        printer.newPage();
    
                        position = 0;
                        painter.resetTransform();
                        painter.translate(0, 0);
                    }
    
                    QString html="<table style='page-break-after:always' border='1' width='100%' cellpadding =10  style='border-width: 1px;border-style: solid;border-color: #9e9e9e;width:100%'>";
    
                        index=newMyModel->index(r, 0);
    
                        QVariant prop1=index.data(MyModel::prop1);
                        QVariant prop2=index.data(MyModel::prop2);
    
    					'''
    					'''
    					'''
    					
                        html.append(
                        "<tr style='background-color:red'>"
                        "<td style='border-width: 1px;padding:10; border-style: solid; border-color: #9e9e9e;width:16%'>"+prop1.toString()+" </td>"
                        "<td style='border-width: 1px;padding:10; border-style: solid; border-color: #9e9e9e;width:16%'>"+prop2.toString()+" </td>"
                        ...
    					...
    					...);
    
                    }
    
                    html.append("</table>");
    
                    QRect rect = painter.boundingRect(painter.window(),
                                                      Qt::AlignJustify | Qt::TextWordWrap,
                                                      html);
                    QTextDocument doc;                
                    doc.setHtml(html);
                    doc.drawContents(&painter, rect);
    
                    painter.drawRect(rect);
                    painter.translate(0, rect.height());
                    position += rect.height();
                }
                painter.end();
            }
        }
    
    1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by
      #2

      Any reason why you cannot use a QTableView or build your own view?

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      N 1 Reply Last reply
      0
      • VRoninV VRonin

        Any reason why you cannot use a QTableView or build your own view?

        N Offline
        N Offline
        neda
        wrote on last edited by neda
        #3

        @VRonin

        I use this code but it does not work (Print blank page).

        QPrinter printer;
            printer.setPageSize(QPrinter::A4);
            printer.setFullPage(true);
        
            QPrintDialog *dlg = new QPrintDialog(&printer,0);
            if(dlg->exec() == QDialog::Accepted) {
        
                QTableView* pTableView = new QTableView;
                pTableView->setModel(newMyModel);
        
                int width = 0;
                int height = 0;
                int columns = newMyModel->columnCount();
                int rows = newMyModel->rowCount();
        
                pTableView->resizeColumnsToContents();
        
                for( int i = 0; i < columns; ++i ) {
                    width += pTableView->columnWidth(i);
                }
        
                for( int i = 0; i < rows; ++i ) {
                    height += pTableView->rowHeight(i);
                }
        
                pTableView->setFixedSize(width, height);
                pTableView->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
                pTableView->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
        
                 pTableView->render(&printer);
        
        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved