-3

我正在尝试使用带有 html 的 qprint 和 QTextDocument 以 a4 格式打印发票 ..但我找不到任何解决方案来调整 html 表的左边距和右边距…

4

1 回答 1

0

我的例子

QString data;
m_printer.setPageSize(QPrinter::A4);
m_printer.setOrientation(QPrinter::Landscape);
QTextStream out(&m_strStream);
const int rowCount = m_TableWidget->model()->rowCount();
const int columnCount = m_TableWidget->model()->columnCount();
out <<  "<html>\n"
        "<head>\n"
        "<meta Content=\"Text/html; charset=Windows-1251\">\n"
     <<  QString("<title>%1</title>\n").arg("title")
      <<  "</head>\n"
          "<body bgcolor=#ffffff link=#5000A0>\n"
          "<table border=1 cellspacing=0 cellpadding=2>\n";
out << "<thead><tr bgcolor=#f0f0f0>";
for (int column = 0; column < columnCount; column++)
    if (!m_TableWidget->isColumnHidden(column))
        out << QString("<th>%1</th>").arg(m_TableWidget->model()->headerData(column, Qt::Horizontal).toString());
out << "</tr></thead>\n";
// data table
out << "<tr>";
for (int column = 0; column < columnCount; column++) {
    if(column%7 == 1) {
        QString data = m_TableWidget->model()->data(m_TableWidget->model()->index(0, column)).toString().simplified();
        out << QString("<td colspan=7>%1</td>").arg((!data.isEmpty()) ? data : QString("&nbsp;"));
    } else if(column == 0){
        QString data = m_TableWidget->model()->data(m_TableWidget->model()->index(0, column)).toString().simplified();
        out << QString("<td bkcolor=0>%1</td>").arg((!data.isEmpty()) ? data : QString("&nbsp;"));
    }
}
out << "</tr>\n";
for (int row = 1; row < rowCount; row++) {
    out << "<tr>";
    for (int column = 0; column < columnCount; column++) {
        if(row==1 && column==1)
        {   data =m_ComboBox->currentText().simplified();
        }
        if (!m_TableWidget->isColumnHidden(column)) {
            data = m_TableWidget->model()->data(m_TableWidget->model()->index(row, column)).toString().simplified();
            out << QString("<td bkcolor=0>%1</td>").arg((!data.isEmpty()) ? data : QString("&nbsp;"));
        }
    }
    out << "</tr>\n";
}
out <<  "</table>\n"
        "</body>\n"
        "</html>\n";
m_document = new QTextDocument();
m_document->setHtml(m_strStream);
m_dialog = new QPrintDialog(&m_printer, NULL);
if (m_dialog->exec() == QDialog::Accepted) {
    m_document->print(&m_printer);
}
delete m_document;
于 2017-07-19T06:48:34.010 回答