Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. No way to close a QPrintDialog from code
Forum Updated to NodeBB v4.3 + New Features

No way to close a QPrintDialog from code

Scheduled Pinned Locked Moved General and Desktop
2 Posts 2 Posters 1.2k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    Bargonaut
    wrote on last edited by
    #1

    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_OBJECT

    public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

    private slots:
    void handlePrintClicked();
    void handlePrintOK();
    void closePrintDialog();

    private:
    Ui::MainWindow ui;
    QPrintDialog
    m_printDialog;
    };

    #endif // MAINWINDOW_H
    @

    1 Reply Last reply
    0
    • raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

      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 ;)

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved