i have a Qt code writed in C++ . i want translate it to python code, but i have a problem in 4 code line:
all code is:

void TableView::print(QPainter* painter, const QRect& area)
{
    const int rows = model()->rowCount();
    const int cols = model()->columnCount();

    // calculate the total width/height table would need without scaling
    double totalWidth = 0.0;
    for (int c = 0; c < cols; ++c)
    {
        totalWidth += columnWidth(c);
    }
    double totalHeight = 0.0;
    for (int r = 0; r < rows; ++r)
    {
        totalHeight += rowHeight(r);
    }

    // calculate proper scale factors
    const double scaleX = area.width() / totalWidth;
    const double scaleY = area.height() / totalHeight;
    painter->scale(scaleX, scaleY);

    // paint cells
    for (int r = 0; r < rows; ++r)
    {
        for (int c = 0; c < cols; ++c)
        {
            QModelIndex idx = model()->index(r, c);
            QStyleOptionViewItem option = viewOptions();
            option.rect = visualRect(idx);
            itemDelegate()->paint(painter, option, idx);
        }
    }
}

// printer usage
QPainter painter(&printer);
tableView->print(&painter, printer.pageRect());

// test on pixmap
QPixmap pixmap(320, 240);
QPainter painter(&pixmap);
tableView->print(&painter, pixmap.rect());
pixmap.save("table.png", "PNG");

but problem is here:

        for (int r = 0; r < rows; ++r)
        {
            for (int c = 0; c < cols; ++c)
            {
                QModelIndex idx = model()->index(r, c);
                QStyleOptionViewItem option = viewOptions();
                option.rect = visualRect(idx);
                itemDelegate()->paint(painter, option, idx);
            }

please help me . thank you so much :)

Here is a typical example using PyQT:

'''pqt_tableview2.py

use PyQT's QTableView and QAbstractTableModel
to present tabular data
a rather simplified experiment
tested with PyQT 4.8 and Python 3.2
'''

from PyQt4.QtCore import *
from PyQt4.QtGui import *

class MyWindow(QWidget):
    def __init__(self, data_list, header, *args):
        QWidget.__init__(self, *args)
        # setGeometry(x_pos, y_pos, width, height)
        self.setGeometry(300, 200, 400, 250)
        self.setWindowTitle("PyQT's QTableView and QAbstractTableModel")

        table_model = MyTableModel(self, data_list, header)
        table_view = QTableView()
        table_view.setModel(table_model)

        layout = QVBoxLayout(self)
        layout.addWidget(table_view)
        self.setLayout(layout)


class MyTableModel(QAbstractTableModel):
    def __init__(self, parent, mylist, header, *args):
        QAbstractTableModel.__init__(self, parent, *args)
        self.mylist = mylist
        self.header = header

    def rowCount(self, parent):
        return len(self.mylist)

    def columnCount(self, parent):
        return len(self.mylist[0])

    def data(self, index, role):
        if not index.isValid():
            #return QVariant()
            return None
        elif role != Qt.DisplayRole:
            return None
        return self.mylist[index.row()][index.column()]

    def headerData(self, col, orientation, role):
        if orientation == Qt.Horizontal and role == Qt.DisplayRole:
            return self.header[col]
        return None


header = ['Name', 'Age', 'Weight']
# a list of (name, age, weight) tuples
data_list = [
('Heidi Kalumpa', '36', '127'),
('Frank Maruco', '27', '234'),
('Larry Pestraus', '19', '315'),
('Serge Romanowski', '59', '147'),
('Carolus Arm', '94', '102'),
('Michel Sargnagel', '21', '175')
]

app = QApplication([])
win = MyWindow(data_list, header)
win.show()
app.exec_()

vey thanx dear . but i want print a tableView on the paper. i need example for QPrinter and print vew :) here is code for c++ and qt but i neet for python and pyqt

very thanx dear . but i want print a tableView on the paper. i need example for QPrinter and print vew :) here is code for c++ and qt but i neet for python and pyqt

A typical QPrint example for PyQT:

#!/usr/bin/env python

"""
Lets get the print thing working
http://www.riverbankcomputing.com/pipermail/pyqt/2009-January/021592.html
"""
from PyQt4.QtGui import *
from PyQt4.QtCore import *
import PyQt4.QtWebKit as QtWebKit

import sys

class htmlViewer(QtWebKit.QWebView):
    def __init__(self,url, parent=None):
        QtWebKit.QWebView.__init__(self,parent)
        self.setUrl(QUrl(url))
        self.preview = QPrintPreviewDialog()
        self.connect(self.preview,
            SIGNAL("paintRequested (QPrinter*)"),
            SLOT("print (QPrinter *)"))
        self.connect(self,SIGNAL("loadFinished (bool)"),
            self.execpreview)
    def execpreview(self,arg):
        print(arg)
        self.preview.exec_()

if __name__=="__main__":
    app = QApplication(sys.argv)

    myurl = "http://www.google.com"
    widget = htmlViewer(myurl)
    widget.show()

    sys.exit(app.exec_())
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.