【问题标题】:pyqt QTableView printing (4 line c++ code translate to python)pyqt QTableView打印(4行c++代码翻译成python)
【发布时间】:2012-05-29 02:23:14
【问题描述】:

我有一个用 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);
}

请帮助我。非常感谢:)

【问题讨论】:

  • 真正的问题是什么?不是在编译吗?如果不是,编译器错误是什么?
  • 它正在编译。我要翻译 c 2 py。问题就在这里:
  • option.rect = self.visualRect(idx)
  • self.itemDelegate().paint(painter, option, idx)

标签: c++ python qt pyqt


【解决方案1】:

如果你能解释一下问题就好了。

让我们看看...

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 = ...(尚未测试)。问题出在哪里?

【讨论】:

  • 我想把它翻译成 python 并测试它。但我不明白该怎么做
【解决方案2】:

C++ 到 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);

【讨论】:

    猜你喜欢
    • 2019-03-29
    • 1970-01-01
    • 2016-06-26
    • 1970-01-01
    • 1970-01-01
    • 2018-09-16
    • 2013-11-25
    • 2018-09-09
    • 1970-01-01
    相关资源
    最近更新 更多