pyqt QTableView打印(4行c ++代码转换为python)

时间:2012-05-22 15:01:02

标签: c++ python qt pyqt

我有一个用C ++编写的Qt代码。我想将它翻译成python代码,但我有4行问题: 所有代码都是:

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");

但问题在于:

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);
}

请帮帮我。非常感谢你:))

2 个答案:

答案 0 :(得分:0)

如果你解释这个问题会很好。

让我们看看......

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);
}

直接的翻译是:

for c in range(cols):
    idx = model.index(r, c)
    option = self.viewOptions()
    option.rect = self.visualRect(idx)
    self.itemDelegate().paint(painter, option, idx)

不确定option.rect = ...(尚未测试)。你有问题吗?

答案 1 :(得分:0)

c ++ CODE TO PYTHON是:

def printTable(self,printer,painter,area):
    model = self.model
    myTableView = self.view
    printer = painter #self.myprinter
    rows = model.rowCount();
    columns = model.columnCount();
    totalWidth = 0.0;
    totalPageHeight = 0.0;
    totalHeight = 0.0;
    for c in range(columns):
        totalWidth += myTableView.columnWidth(c)

    for p in range(45):
        totalPageHeight += myTableView.rowHeight(p);

    for r in range(rows):
        totalHeight += myTableView.rowHeight(r);

    xscale = area.width() / totalWidth;
    yscale = area.height() / totalHeight;
    pscale = area.height() / totalPageHeight;
    painter.scale(xscale, pscale);
    painter.translate(area.x() + xscale, area.y() + yscale);

    x=0
    #QStyleOptionViewItem option;

    for r in range(rows):
        ++x
        for c in range(columns):
            idx = model.index(r,c);
            option = myTableView.viewOptions();
            option.rect = myTableView.visualRect(idx);
            if r % 2 == 0:
                brush= QtGui.QBrush(QtGui.QColor(220, 220, 220), QtCore.Qt.SolidPattern);
                painter.fillRect(option.rect, brush);
            myTableView.itemDelegate().paint(painter, option, idx);

        if (x == 45):
            ok = printer.newPage();
            x=0;
            painter.translate(0, -1350);