No way to close a QPrintDialog from code
-
We have an application that uses a QPrintDialog to provide the native Win/Mac Print options. However, if the user of the application unplugs their device, we want to close the modal dialog. QPrintDialog does provide both close() and done(int) methods, but both are useless.
Does anyone know of a way to get the QPrintDialog to cancel in response to another event? This fails in both Qt4.8.5 and Qt5.1.0, tested under Windows 7 64-bit, and Mac OS X 10.8.4.
Here is a sample program which shows the problem...
(The mainwindow.ui is simply a QMainWIndow with a pushbutton connected to handlePrintClicked)
Reported as "QTBUG-32464":https://bugreports.qt-project.org/browse/QTBUG-32464 if anyone wants the project zip.mainwindow.cpp
@#include "mainwindow.h"
#include "ui_mainwindow.h"#if ( QT_VERSION >= QT_VERSION_CHECK(5, 0, 0) )
include <QtPrintSupport/QPrinter>
include <QtPrintSupport/QPrintDialog>
#else
include <QPrinter>
include <QPrintDialog>
#endif
#include <QApplication>
#include <QDebug>
#include <QTimer>MainWindow::MainWindow(QWidget parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QPrinter printer = new QPrinter(QPrinter::HighResolution);
m_printDialog = new QPrintDialog(printer);
}MainWindow::~MainWindow()
{
delete ui;
}void MainWindow::handlePrintClicked()
{
qApp->processEvents(QEventLoop::AllEvents);
QTimer::singleShot(1000, this, SLOT( closePrintDialog() ));
m_printDialog->open(this, SLOT( handlePrintOK() ));
}// This should cancel the Print Dialog
void MainWindow::closePrintDialog()
{
m_printDialog->done(QDialog::Rejected);
qDebug() << "Did it close?";
}void MainWindow::handlePrintOK()
{}
@mainwindow.h
@#ifndef MAINWINDOW_H
#define MAINWINDOW_H#include <QMainWindow>
class QPrintDialog;
namespace Ui {
class MainWindow;
}class MainWindow : public QMainWindow
{
Q_OBJECTpublic:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();private slots:
void handlePrintClicked();
void handlePrintOK();
void closePrintDialog();private:
Ui::MainWindow ui;
QPrintDialog m_printDialog;
};#endif // MAINWINDOW_H
@ -
that's not possible when you're using the native dialog since it runs in a separate windows-eventloop.
I think if it would be possible the trolls would have alreadyprovided an API for that ;)