使用 QT5 如何打印 QTablewidget 并缩放表格以适应 A4 纸的一侧



我编写了一个 QT5 应用程序,它根据各种输入创建每月轮换/时间表。 它会生成一个 csv 文件,我可以使用 excel 来读取和打印。我可以使用libreoffice将其打印到一张A4纸上。但是,我真正想做的是使用qt将表格直接打印到打印机上。

恐怕我对如何最好地努力实现这一目标感到困惑。 我已经将html与QTextDocument一起使用,以成功打印出轮换/时间表。但是,结果最终会出现在两页而不是一页上。 我以横向模式打印出来。 我认为最好将文档的高度缩小到适合一页。

void ViewEditRotaDialog::m_printButtonSlot()
{
  QString strStream;
  QTextStream out(&strStream);
  const int rowCount = m_tableWidget->rowCount();
  const int columnCount = m_tableWidget->columnCount();
  out <<  "<html>n"
  "<head>n"
  "<meta Content="Text/html; charset=Windows-1251">n"
  <<  QString("<title>%1</title>n").arg("ROTA")
  <<  "</head>n"
  "<body bgcolor=#ffffff link=#5000A0>n"
  "<table border=1 cellspacing=0 cellpadding=2>n";
  // headers
  out << "<thead><tr bgcolor=#f0f0f0>";
  for (int column = 0; column < columnCount; column++)
    out << QString("<th>%1</th>").
        arg(m_tableWidget->horizontalHeaderItem(column)->text());
  out << "</tr></thead>n";
  // data table
  for (int row = 0; row < rowCount; row++)
  {
    out << "<tr>";
    for (int column = 0; column < columnCount; column++)
    {
      QString data 
      m_tableWidget->item(row,column)->text().simplified();
      out << QString("<td bkcolor=0>%1</td>").
                 arg((!data.isEmpty()) ? data :  QString("&nbsp;"));
    }
    out << "</tr>n";
  }
  out <<  "</table>n"
            "</body>n"
            "</html>n";
  QTextDocument *document = new QTextDocument();
  document->setHtml(strStream);
  QPrinter printer(QPrinter::HighResolution);
  printer.setOrientation(QPrinter::Landscape);
  printer.setPageMargins(0.1,0.1,0.1,0.1,QPrinter::Millimeter);
  printer.setFullPage(true);
  QPrintDialog *dialog = new QPrintDialog(&printer, NULL);
  if (dialog->exec() != QDialog::Accepted)
    return;
  document->print(&printer);
  delete document;
}

我已经看到了使用 QPainter 并尝试缩放输出的其他示例。我应该这样做并使用drawcontent((还是应该使用完全不同的方法?

我决定使用painter和drawContent((来玩一玩。 我很高兴我能用最小的努力让它做我需要的事情。 我还没有完全理解它是如何工作的,但我稍后会更详细地研究它。 可能是我需要增强它,但它看起来非常适合我需要的东西。 简单地说,看起来我只需要改变比例就可以让它做我需要的。以前从未使用过QT进行打印,我真的不知道如何最好地做到这一点。但我对结果很满意。

我替换了下面的代码 QTextDocument *document = new QTextDocument();

`
document->setHtml(strStream);
QPrinter printer(QPrinter::HighResolution);
printer.setPaperSize(QPrinter::A4);
printer.setOrientation(QPrinter::Landscape);
printer.setPageMargins(0.1,0.1,0.1,0.1,QPrinter::Millimeter);
printer.setFullPage(true);
QPrintDialog *dialog = new QPrintDialog(&printer, NULL);
if (dialog->exec() != QDialog::Accepted)
  return;
QPainter painter;
painter.begin(&printer);
double xscale = printer.pageRect().width() / document->size().width();
double yscale = printer.pageRect().height() / document->size().height();
painter.translate(printer.paperRect().x() + printer.pageRect().width() / 2,
                printer.paperRect().y() + printer.pageRect().height() / 2);
painter.scale(xscale, yscale);
painter.translate(-document->size().width() / 2,
                                     -document->size().height() / 2);
document->drawContents(&painter);
painter.end();
delete document;
}`

这可能不是最好的答案,但到目前为止它有效。

最新更新