简体   繁体   English

使用垂直和水平标题打印QTableView

[英]Printing QTableView with vertical and horizontal headers

I have a program that gets data from SQL (using QSQLQuery) and puts the result into a QTableView. 我有一个程序可以从SQL获取数据(使用QSQLQuery),然后将结果放入QTableView中。

For user ease of reading I needed to transpose the SQL output (swap rows and columns) but couldn't achieve this easily in SQL (even using PIVOT). 为了使用户易于阅读,我需要转置SQL输出(交换行和列),但无法在SQL中轻松实现这一点(即使使用PIVOT)。 Instead I used a proxy model in Qt which works great. 相反,我在Qt中使用了一个很好的代理模型。

Problem is I need to be able to print the entire QTableView. 问题是我需要能够打印整个QTableView。 I can print the contents of the table with the horizontal headers but for the life of me, cant work out how to print the vertical headers (technically the first column since each row has a label due to the transposition). 我可以使用水平标题打印表格的内容,但是为了我自己的生命,无法解决如何打印垂直标题的问题(技术上来说,第一列是因为换位,每一行都有标签)。 Qt doesn't recognize the first 'column' as being a column of row-names, nor do I know how to get it to treat it as vertical headers for printing. Qt不能将第一个“列”识别为行名称的列,也不知道如何获取它以将其视为打印的垂直标题。

Proxy model code: 代理模型代码:

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

Here are the basics of how I'm trying to print; 这是我尝试打印的基础知识;

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

Really appreciate any help or advice! 真的感谢任何帮助或建议!

If you want to print the headers you must add them as shown in the following code: 如果要打印标题,则必须添加它们,如以下代码所示:

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

TableView: TableView中:

在此处输入图片说明

Part of PDF PDF的一部分

在此处输入图片说明

The code that is implemented for the test can be found in the following link . 在以下链接中可以找到为测试实现的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM