用垂直和水平打印 QTableView headers
Printing QTableView with vertical and horizontal headers
我有一个程序从 SQL 获取数据(使用 QSQLQuery)并将结果放入 QTableView。
为了方便用户阅读,我需要转置 SQL 输出(交换行和列),但无法在 SQL 中轻松实现(即使使用 PIVOT)。相反,我在 Qt 中使用了一个代理模型,效果很好。
问题是我需要能够打印整个 QTableView。我可以用水平 headers 打印 table 的内容,但对于我来说,无法弄清楚如何打印垂直 headers (技术上第一列,因为每一行都有一个标签由于换位)。 Qt 无法将第一个 'column' 识别为 row-names 的列,我也不知道如何将其视为垂直 headers 进行打印。
代理型号代码:
class Horizontal_proxy_model : public QAbstractProxyModel {
public:
Horizontal_proxy_model(QObject * parent = 0);
QModelIndex mapToSource(const QModelIndex &proxyIndex) const;
QModelIndex mapFromSource(const QModelIndex &sourceIndex) const;
QModelIndex index(int row, int column, const QModelIndex &parent =
QModelIndex()) const;
QModelIndex parent(const QModelIndex &child) const;
int rowCount(const QModelIndex &parent) const;
int columnCount(const QModelIndex &parent) const;
QVariant headerData(int section, Qt::Orientation orientation, int role)
const;
};
Horizontal_proxy_model::Horizontal_proxy_model(QObject *parent) :
QAbstractProxyModel(parent) {
}
QModelIndex Horizontal_proxy_model::mapToSource(const QModelIndex
&proxyIndex) const {
if (sourceModel()) {
return sourceModel()->index(proxyIndex.column(), proxyIndex.row());
} else {
return QModelIndex();
}
}
QModelIndex Horizontal_proxy_model::mapFromSource(const QModelIndex
&sourceIndex) const {
return index(sourceIndex.column(), sourceIndex.row());
}
QModelIndex Horizontal_proxy_model::index(int row, int column, const
QModelIndex &) const {
return createIndex(row, column, (void*) 0);
}
QModelIndex Horizontal_proxy_model::parent(const QModelIndex &) const {
return QModelIndex();
}
int Horizontal_proxy_model::rowCount(const QModelIndex &) const {
return sourceModel() ? sourceModel()->columnCount() : 0;
}
int Horizontal_proxy_model::columnCount(const QModelIndex &) const {
return sourceModel() ? sourceModel()->rowCount() : 0;
}
QVariant Horizontal_proxy_model::headerData(
int section, Qt::Orientation orientation, int role) const {
if (!sourceModel()) { return QVariant(); }
Qt::Orientation new_orientation = orientation == Qt::Horizontal ?
Qt::Vertical : Qt::Horizontal;
return sourceModel()->headerData(section, new_orientation, role);
}
以下是我尝试打印的基础知识;
void Snapshot_finance::on_pushButton_print_clicked()
{
QString html;
html = "<html><body><table border=\"0\">";
for(int row = 0; row < ui->tableView->model()->rowCount(); row++) {
html += "<tr>";
for(int column = 0; column < ui->tableView->model()->columnCount();
column++) {
QString data = ui->tableView->model()->data(ui->tableView->model()->
index(row, column), Qt::DisplayRole).toString();
html += "<td>" + data + "</td>";
}
html += "</tr>";
}
html += "</table></body></html>";
QPrinter printer;
QPrintDialog *dialog = new QPrintDialog(&printer);
if(dialog->exec() == QDialog::Accepted) {
QTextDocument document;
document.setHtml(html);
document.print(&printer);
}
}
非常感谢任何帮助或建议!
如果要打印 headers,则必须按照以下代码所示添加它们:
void Snapshot_finance::on_pushButton_print_clicked()
const QString format("<td>%1</td>");
QString html;
QAbstractItemModel *md = ui->tableView->model();
html = "<html><body><table border=\"0\">";
html += "<td></td>";
for(int column = 0; column < md->columnCount();
column++) {
QString data = md->headerData(column, Qt::Horizontal, Qt::DisplayRole).toString();
html += format.arg(data);
}
for(int row = 0; row < md->rowCount() ; row++) {
html += "<tr>";
QString data = md->headerData(row, Qt::Vertical, Qt::DisplayRole).toString();
html += format.arg(data);
for(int column = 0; column < md->columnCount();
column++) {
QString data = md->index(row, column).data(Qt::DisplayRole).toString();
html += format.arg(data);
}
html += "</tr>";
}
html += "</table></body></html>";
QPrinter printer;
QPrintDialog *dialog = new QPrintDialog(&printer);
if(dialog->exec() == QDialog::Accepted) {
QTextDocument document;
document.setHtml(html);
document.print(&printer);
}
}
表格视图:
部分PDF
为测试实现的代码可以在下面找到link。
我有一个程序从 SQL 获取数据(使用 QSQLQuery)并将结果放入 QTableView。
为了方便用户阅读,我需要转置 SQL 输出(交换行和列),但无法在 SQL 中轻松实现(即使使用 PIVOT)。相反,我在 Qt 中使用了一个代理模型,效果很好。
问题是我需要能够打印整个 QTableView。我可以用水平 headers 打印 table 的内容,但对于我来说,无法弄清楚如何打印垂直 headers (技术上第一列,因为每一行都有一个标签由于换位)。 Qt 无法将第一个 'column' 识别为 row-names 的列,我也不知道如何将其视为垂直 headers 进行打印。
代理型号代码:
class Horizontal_proxy_model : public QAbstractProxyModel {
public:
Horizontal_proxy_model(QObject * parent = 0);
QModelIndex mapToSource(const QModelIndex &proxyIndex) const;
QModelIndex mapFromSource(const QModelIndex &sourceIndex) const;
QModelIndex index(int row, int column, const QModelIndex &parent =
QModelIndex()) const;
QModelIndex parent(const QModelIndex &child) const;
int rowCount(const QModelIndex &parent) const;
int columnCount(const QModelIndex &parent) const;
QVariant headerData(int section, Qt::Orientation orientation, int role)
const;
};
Horizontal_proxy_model::Horizontal_proxy_model(QObject *parent) :
QAbstractProxyModel(parent) {
}
QModelIndex Horizontal_proxy_model::mapToSource(const QModelIndex
&proxyIndex) const {
if (sourceModel()) {
return sourceModel()->index(proxyIndex.column(), proxyIndex.row());
} else {
return QModelIndex();
}
}
QModelIndex Horizontal_proxy_model::mapFromSource(const QModelIndex
&sourceIndex) const {
return index(sourceIndex.column(), sourceIndex.row());
}
QModelIndex Horizontal_proxy_model::index(int row, int column, const
QModelIndex &) const {
return createIndex(row, column, (void*) 0);
}
QModelIndex Horizontal_proxy_model::parent(const QModelIndex &) const {
return QModelIndex();
}
int Horizontal_proxy_model::rowCount(const QModelIndex &) const {
return sourceModel() ? sourceModel()->columnCount() : 0;
}
int Horizontal_proxy_model::columnCount(const QModelIndex &) const {
return sourceModel() ? sourceModel()->rowCount() : 0;
}
QVariant Horizontal_proxy_model::headerData(
int section, Qt::Orientation orientation, int role) const {
if (!sourceModel()) { return QVariant(); }
Qt::Orientation new_orientation = orientation == Qt::Horizontal ?
Qt::Vertical : Qt::Horizontal;
return sourceModel()->headerData(section, new_orientation, role);
}
以下是我尝试打印的基础知识;
void Snapshot_finance::on_pushButton_print_clicked()
{
QString html;
html = "<html><body><table border=\"0\">";
for(int row = 0; row < ui->tableView->model()->rowCount(); row++) {
html += "<tr>";
for(int column = 0; column < ui->tableView->model()->columnCount();
column++) {
QString data = ui->tableView->model()->data(ui->tableView->model()->
index(row, column), Qt::DisplayRole).toString();
html += "<td>" + data + "</td>";
}
html += "</tr>";
}
html += "</table></body></html>";
QPrinter printer;
QPrintDialog *dialog = new QPrintDialog(&printer);
if(dialog->exec() == QDialog::Accepted) {
QTextDocument document;
document.setHtml(html);
document.print(&printer);
}
}
非常感谢任何帮助或建议!
如果要打印 headers,则必须按照以下代码所示添加它们:
void Snapshot_finance::on_pushButton_print_clicked()
const QString format("<td>%1</td>");
QString html;
QAbstractItemModel *md = ui->tableView->model();
html = "<html><body><table border=\"0\">";
html += "<td></td>";
for(int column = 0; column < md->columnCount();
column++) {
QString data = md->headerData(column, Qt::Horizontal, Qt::DisplayRole).toString();
html += format.arg(data);
}
for(int row = 0; row < md->rowCount() ; row++) {
html += "<tr>";
QString data = md->headerData(row, Qt::Vertical, Qt::DisplayRole).toString();
html += format.arg(data);
for(int column = 0; column < md->columnCount();
column++) {
QString data = md->index(row, column).data(Qt::DisplayRole).toString();
html += format.arg(data);
}
html += "</tr>";
}
html += "</table></body></html>";
QPrinter printer;
QPrintDialog *dialog = new QPrintDialog(&printer);
if(dialog->exec() == QDialog::Accepted) {
QTextDocument document;
document.setHtml(html);
document.print(&printer);
}
}
表格视图:
部分PDF
为测试实现的代码可以在下面找到link。