3

I'm trying to generate a simple table (2 rows and 2 columns) and write it to a pdf file, using Qt 4.8.0.

So far, I generate the pdf but there is extra space at the bottom of the "printed" table:

enter image description here

I got the same problem with the right side of the table but I managed to get rid of it. But in this case I am clueless.

Here's the code I have now (all of this code is located in main.cpp):

Main

#include <QtGui/QApplication>

#include <QtCore/QDebug>
#include <QtCore/QMap>
#include <QtCore/QString>
#include <QtGui/QPrinter>
#include <QtGui/QHeaderView>
#include <QtGui/QPainter>
#include <QtGui/QTableWidget>
#include <QtGui/QTableWidgetItem>

/**/
/* Here are the functions.
/**/

int main(int argc, char *argv[]) {
  QApplication a(argc, argv);
  QMap<QString,int> values;
  values.insert("X",7);
  values.insert("Y",13);

  bool status = TableWidgetToPdf("FromWidget.pdf",values);

   return a.exec();
}

TableWidgetToPdf

bool TableWidgetToPdf(const QString& title, const QMap<QString, int>& values) {
  QTableWidget* table = GenerateTable(values);

  QPrinter printer;
  printer.setOutputFileName(title);
  printer.setOutputFormat(QPrinter::PdfFormat);
  QPainter painter(&printer);
  painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing | QPainter::SmoothPixmapTransform);
  printer.setPaperSize(QPrinter::A4);

  table->render(&painter);
  painter.end();
  printer.newPage();

  delete table;
  return true;
};

GenerateTable

QTableWidget* GenerateTable(const QMap<QString,int>& values) {
  QTableWidget* table = new QTableWidget;

  table->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
  table->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
  table->setRowCount(2);
  table->setColumnCount(2);
  table->setEditTriggers(QAbstractItemView::NoEditTriggers);
  table->setShowGrid(false);
  table->verticalHeader()->hide();

  QStringList h_labels;
  h_labels << "X" << "Y";
  table->setHorizontalHeaderLabels(h_labels);
  table->horizontalHeader()->setFont( QFont("Times", 10, QFont::Bold) );
  table->horizontalHeader()->setStretchLastSection(true);

  QTableWidgetItem* item00 = new QTableWidgetItem( QString::number(values["X"]));
  item00->setTextAlignment(Qt::AlignCenter);
  table->setItem(0,0, item00 );

  QTableWidgetItem* item01 = new QTableWidgetItem( QString::number(values["Y"]) );
  item01->setTextAlignment(Qt::AlignCenter);
  table->setItem(0,1,item01);

  table->setItem(1,0,new QTableWidgetItem("ABCD"));

  return table;
};

NOTE:

Putting

table->horizontalHeader()->setResizeMode(QHeaderView::Stretch);
table->verticalHeader()->setResizeMode(QHeaderView::Stretch);

in GenerateTable the space disappears, but the cells are resized and consume too much space than needed for their contents. I would like to avoid that if possible:

enter image description here

EDIT:

OK.

In the end I achieved what I wanted by getting rid of the QTableWidget. I had to create the table using html and feeding it to a QTextEditor. Isn't any way to achieve this with a QTableWidget?

Adri C.S.
  • 2,909
  • 5
  • 36
  • 63

2 Answers2

1

Have you tried the flags for resize content?

Try the following code, I don't have access to Qt right now.

table->horizontalHeader()->setResizeMode(QHeaderView::ResizeToContents);
table->verticalHeader()->setResizeMode(QHeaderView::ResizeToContents);

Hope that works!

Tyler Jandreau
  • 4,245
  • 1
  • 22
  • 47
  • The extra space it's still there :( – Adri C.S. Jan 13 '14 at 15:09
  • I added the `QTableWidget` to a `MainWindow` to see it. When I use the policy `QSizePolicy::Ignored`, the table is perfect (see [here](http://imgur.com/OJNn1eX)). With no extra space. However, in the pdf it still has the extra space. – Adri C.S. Jan 13 '14 at 15:46
0

I realise that this is an old post but it seems fairly often read.

I tried the same methods that you tried and none worked. Eventually I used a QTableView and added an extra method called by adding/removing rows.

    void
    TitleView::verticalResizeTableViewToContents()
    {
      auto count = m_model->rowCount(QModelIndex());
      auto scrollBarHeight = horizontalScrollBar()->height();
      auto horizontalHeaderHeight = horizontalHeader()->height();
      auto rowTotalHeight = scrollBarHeight + (horizontalHeaderHeight * count);
      setMinimumHeight(rowTotalHeight);
      setMaximumHeight(rowTotalHeight);
    }