【发布时间】: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)