Commit 7730b552 authored by Hugo Hörnquist's avatar Hugo Hörnquist

Add printer functionality to SQL queries.

Currently it pops up a preview dialog, which is good enough for the time
being.
parent 20b2a53c
......@@ -18,6 +18,12 @@
#include <QFileInfo>
#include <QPrinter>
#include <QPrintDialog>
#include <QPrintPreviewDialog>
#include <QPainter>
#include "tableprinter.h"
#include <cstring>
#include "comboboxitemdelegate.h"
......@@ -1013,3 +1019,40 @@ void MainWindow::on_sqlOutReload_clicked()
model->setQuery(ui->sqlEdit->toPlainText());
ui->tabWidget_2->setCurrentIndex(0);
}
void MainWindow::printTable(QPrinter* printer)
{
QPainter painter;
if (!painter.begin(printer)) {
qWarning() << "can't start printer";
return;
}
QSqlQueryModel* model = (QSqlQueryModel*) ui->sqlOutput->model();
TablePrinter tablePrinter(&painter, printer);
QVector<int> columnStretch = QVector<int>();
QVector<QString> headers = QVector<QString>();
for (int i = 0; i < model->columnCount(); i++) {
columnStretch << 1;
headers << model->headerData(i, Qt::Horizontal).toString();
}
QFont headerFont;
headerFont.setBold(true);
tablePrinter.setHeadersFont(headerFont);
if (!tablePrinter.printTable(ui->sqlOutput->model(), columnStretch, headers)) {
qDebug() << tablePrinter.lastError();
}
painter.end();
}
void MainWindow::on_sqlPrint_clicked()
{
QPrintPreviewDialog dialog;
connect(&dialog, SIGNAL(paintRequested(QPrinter*)), this, SLOT(printTable(QPrinter*)));
dialog.exec();
}
......@@ -5,6 +5,7 @@
#include <QSqlTableModel>
#include <QSqlRelationalTableModel>
#include <QTableView>
#include <QPrinter>
#include "disablingmodel.h"
......@@ -93,6 +94,10 @@ private slots:
void on_sqlOutReload_clicked();
void on_sqlPrint_clicked();
void printTable(QPrinter* printer);
private:
Ui::MainWindow *ui;
QSqlTableModel* model;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment