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. QPrintDialog::open Usage on a Mac
Qt 6.11 is out! See what's new in the release blog

QPrintDialog::open Usage on a Mac

Scheduled Pinned Locked Moved General and Desktop
9 Posts 3 Posters 6.5k 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.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #1

    I've searched everywhere and can't find an example..

    How is QPrintDialog::open() different than QPrintDialog::exec() ?

    What I'm trying to do is disable some buttons in a Mac QPrintDialog.

    The buttons I want to disable are the PDF and Preview buttons. The reason I want to disable them is because they don't act as expected.

    Cancel..it works and returns 0

    Print..it works and returns 1

    Preview..always returns 1. I can't tell what the user actually does. As soon as the user clicks the Preview button, the print dialog closes and returns 1 or accepted. Anything you want to do only on "Accepted" in gets done even if the user eventually Cancels and chooses not to print anything.

    PDF..Again, always returns 1.. I can't tell what the user actually does. As soon as the user clicks the PDF button, the print dialog closes and returns 1 or accepted. Anything you want to do only on "Accepted" gets done even if the user eventually Cancels and chooses not to save as PDF.

    What I'd like to do is disable the Preview & PDF buttons.

    @
    // create the dialog
    QPrintDialog *printDialog = new QPrintDialog( printer, this );

    // hook signals to slots
    connect(printDialog, SIGNAL(accepted(QPrinter *)), this, SLOT(PrintAccepted(QPrinter *)));
    connect(printDialog, SIGNAL(rejected()), this, SLOT(PrintRejected()));
    connect(printDialog, SIGNAL(finished(int)), this, SLOT(PrintFinished(int)));

    // search the dialog for child widgets
    QList<QWidget *> pblist = printDialog->findChildren<QWidget *>();
    #if DEBUG_PAGEMANAGERx
    qDebug() << "PageManager::PDB items in button list:" << pblist.size();
    #endif
    foreach(QWidget *pb, pblist)
    {
    QString name = pb->objectName();
    #if DEBUG_PAGEMANAGERx
    qDebug() << "PageManager::PDB item name:" << name;
    #endif
    }

    int resultofexec = printDialog->exec();
    @

    The above "foreach" is just supposed to capture the items of interest..not do anything with them yet. If possible, the idea would be to find the PDF and Preview buttons and just make them not visible.

    I tried looking through the widgets, but the list is always empty. I tried changing QWidget to QPushButton and QToolButton, etc but the list is always 0.

    I suspect that it may be because the print dialog is not really created until the "open()" or "exec()" call is made, but don't know for sure.

    Am I just spinning my wheels? Should I just make my own Dialog that selects a printer and offers a Print & Cancel button?

    1 Reply Last reply
    0
    • ? Offline
      ? Offline
      A Former User
      wrote on last edited by
      #2

      Partial fix for determining if the user selected the PDF option in the standard Mac Print Dialog...

      @
      // set to nothing
      printer->setOutputFileName("");

      QPrintDialog *printDialog = new QPrintDialog( printer, this );
      int resultofexec = printDialog->exec();

      // if the user clicks PDF in the Mac print dialog, the outputFileName is NOT ""
      // even if they click Cancel in the PDF dialog, outputFileName=="Untitled.pdf"
      @

      Still looking for a way to determine if Preview is clicked...

      1 Reply Last reply
      0
      • S Offline
        S Offline
        SuperFun
        wrote on last edited by
        #3

        Were you ever able to find a solution?

        1 Reply Last reply
        0
        • ? Offline
          ? Offline
          A Former User
          wrote on last edited by
          #4

          Not really an acceptable one..just a work-around.

          @
          public:
          //DATA
          enum pdWhat { pdHUH=-2, pdREJECTED=-1, pdFINISHED, pdACCEPTED };
          pdWhat pdstatus; // print dialog status

          QString             pdmsg1;         // message associated with print dialog accept/reject
          QString             pdmsg2;
          QString             pdprintername;  // selscted printer and file re-direct
          QString             pdprinterfilename;
          

          void PageManager::PrintAccepted( QPrinter *p )
          {
          // slot for accept button clicked
          pdstatus = pdACCEPTED;
          pdmsg1 = "OK to print";

          // the selected printer as the painter destination
          QPainter painter;

          if ( p->isValid() )
          {
          #if DEBUG_PAGEMANAGERx
          qDebug() << "-s-PageManager::PrintAccepted - p->isValid():" << p->isValid();
          qDebug() << "-s-PageManager::PrintAccepted - p->printerName():" << p->printerName();
          #endif

          // should be OK to initialize
          p->setFullPage( true );
          
          if ( painter.begin( p ) )
          {
            // able to start the device
            painter.setRenderHint( QPainter::Antialiasing );
            // draw scene to the printer
            printsheet->render( &painter );
            // tell printer to "go"
            painter.end();
          }
          else
          {
            // unable to start printer..we're hosed
            pdmsg1 = "unable to start printer";
            pdmsg2 = "'" + p->printerName() + "'" + " failed at painter.begin";
            #if DEBUG_PAGEMANAGERx
            qDebug() << "-s-PageManager::PrintAccepted - failed at painter.begin";
            #endif
          }
          

          }
          else
          {
          #if DEBUG_PAGEMANAGERx
          qDebug() << "-s-PageManager::PrintAccepted - printer NOT valid:";
          qDebug() << "-s-PageManager::PrintAccepted - p->printerName():" << p->printerName();
          #endif
          }
          }

          void PageManager::PrintFinished( int result)
          {
          // slot for dialog finished

          // search the dialog for child widgets..need to know the signal sender
          /*
          QList<QWidget *> pblist = printDialog->findChildren<QWidget *>();
          #if DEBUG_PAGEMANAGERx
          qDebug() << "PageManager::PDB items in button list:" << pblist.size();
          #endif
          foreach(QWidget *pb, pblist)
          {
          QString name = pb->objectName();
          #if DEBUG_PAGEMANAGERx
          qDebug() << "PageManager::PDB item name:" << name;
          #endif
          }
          */

          pdstatus = pdFINISHED;
          #if DEBUG_PAGEMANAGERx
          qDebug() << "-s-PageManager::PrintFinished" << QString::number(result) ;
          #endif
          }

          void PageManager::PrintRejected()
          {
          // slot for anything but accept button clicked
          pdstatus = pdREJECTED;
          pdmsg1 = "printing cancelled";
          #if DEBUG_PAGEMANAGERx
          qDebug() << "-s-PageManager::PrintRejected";
          #endif
          }

          @

          1 Reply Last reply
          0
          • ? Offline
            ? Offline
            A Former User
            wrote on last edited by
            #5

            --part 2--

            @


            //Part of PageManager::SaveLayout that handles printing..

            // create a printer object and assume it's a physical printer NOT a file
            QPrinter printer; // = new QPrinter(QPrinter::HighResolution);

            // set printer object to a known set of conditions
            printer.setOutputFormat( QPrinter::NativeFormat );
            printer.setResolution( QPrinter::HighResolution );

            // set the output file to a directory that has print (write) permission
            QString xpng = QApplication::applicationDirPath() + GRAPHICSDIR + "X.png";
            printer.setOutputFileName( xpng );

            // retrieve the printer info that's been initialized to show it's been set
            pdprintername = printer.printerName();
            pdprinterfilename = printer.outputFileName();

            #if DEBUG_PAGEMANAGERx
            qDebug() << "--PageManager::SaveLayout - printDialog setup";
            qDebug() << "---SaveLayout - dialog printer is valid:" << printer.isValid();
            qDebug() << "---SaveLayout - dialog printer selected:" << pdprintername;
            qDebug() << "---SaveLayout - dialog printer output filename:" << pdprinterfilename;
            qDebug() << "---SaveLayout - dialog printer outputFormat:" << printer.outputFormat() ;
            #endif

            // global status bar message
            gsb->showMessage("selecting a printer");
            qApp->processEvents();

            //print issue..works on dev macs(10.6.x), but not on tester's iMac(10.5.8)

            #if DEBUG_PAGEMANAGERx
            qDebug() << "*PageManager::PrintDialog - begin";
            #endif

            pdstatus = pdHUH;
            pdmsg1 = "";
            pdmsg2 = "";

            QPrintDialog printDialog( &printer );
            printDialog.setWindowTitle( "Label Sheet Printing" );

            // hook print dialog signals so we can set progress and result messages
            connect( &printDialog, SIGNAL(accepted(QPrinter *)), this, SLOT(PrintAccepted(QPrinter *)) );
            connect( &printDialog, SIGNAL(rejected()), this, SLOT(PrintRejected()) );
            connect( &printDialog, SIGNAL(finished(int)), this, SLOT(PrintFinished(int)) );

            //??
            // https://bugreports.qt-project.org/browse/QTBUG-17913
            qApp->processEvents(QEventLoop::AllEvents);

            // if the user clicked PDF button in the Mac print dialog, the outputFileName is != ""
            // if the user clicked Print button in the Mac print dialog, the outputFileName is == ""
            int resultofexec = printDialog.exec();

            QPrinter *pdprinter = printDialog.printer();

            // retrieve printer info
            pdprintername = printer.printerName();
            pdprinterfilename = printer.outputFileName();

            #if DEBUG_PAGEMANAGERx
            qDebug() << "--PageManager::SaveLayout - printDialog exec() complete - return result:" << resultofexec;
            qDebug() << "---SaveLayout - printer is valid:" << printer.isValid();
            qDebug() << "---SaveLayout - printer selected:" << pdprintername;
            qDebug() << "---SaveLayout - printer output filename:" << pdprinterfilename;
            qDebug() << "---SaveLayout - printer outputFormat:" << printer.outputFormat() ;
            #endif

            //
            // pdstatus is set by the SLOTs connected to the printDialog
            //
            if ( pdstatus == pdACCEPTED )
            {
            #if DEBUG_PAGEMANAGERx
            qDebug() << "-PageManager::PrintDialog pdACCEPTED";
            #endif
            //** print dialog has closed but it will MAGICALLY pipe or direct output to the **
            //** selected destination ONLY if you run through painter->printer code in slot **
            //
            // PDF redirects to a file ONLY if you run through painter->printer code below
            // if dialog used PDF option..output file name will be != "X" && != ""
            //
            // Preview redirects to printer ONLY if you run through painter->printer code below
            // if dialog used Preview or Print options..output file name will be == ""
            //
            qApp->setOverrideCursor(QCursor( Qt::WaitCursor ));
            // global status bar message
            gsb->showMessage("formatting print output");
            qApp->processEvents()

            // we've now sent the print output wherever the hell it's going
            if ( pdprinterfilename == "" )
            {
              // sent to the printer via the print or preview/print button
              #if DEBUG_PAGEMANAGERx
              qDebug() << "-PageManager::PrintDialog - pdstatus == pdACCEPTED - printer state:" << printer.printerState() ;
              #endif
              pdmsg1 = "previewed or print sent to";
              pdmsg2 = pdprintername;
            }
            else
            {
              // print dialog accepted...but really saved to PDF
              pdmsg1 = "printing redirected to";
              // outputfilename is full path..snag the last section
              pdmsg2 = "'" + pdprinterfilename.section('/', -1) + "'";
            }
            

            }
            else
            {
            // print dialog closed with cancel, reject, etc
            #if DEBUG_PAGEMANAGERx
            qDebug() << "-PageManager::PrintDialog - closed with cancel";
            #endif
            }

            qApp->restoreOverrideCursor();
            // leave the message in the status area for ~60 seconds
            gsb->showMessage( "done - " + pdmsg1 + " " + pdmsg2, 1000*60 );

            #if DEBUG_PAGEMANAGERx
            qDebug() << "-PageManager::SaveLayout - PrintDialog - done";
            #endif

            @

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Hi,

              Did you check the "bug report system":http://bugreports.qt-project.org to see whether it's a known issue ? If not you could consider opening a bug report with a minimal compilable example and instructions on how to reproduce the misbehavior

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • S Offline
                S Offline
                SuperFun
                wrote on last edited by
                #7

                PSI-lbc: Thank you for the detailed response and source code. I tried stripped-down version that just included a slot for accepted(). Even in the slot, the QPrinter isn't reporting the file name entered.

                SGalst: There is "QTBUG-2091":https://bugreports.qt-project.org/browse/QTBUG-2091 which is similar but the code isn't very minimal. I guess its time to report my first bug!

                Thanks

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  Did you report it ? If so can you share the link ?

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  0
                  • S Offline
                    S Offline
                    SuperFun
                    wrote on last edited by
                    #9

                    I reported it as "QTBUG-34404":https://bugreports.qt-project.org/browse/QTBUG-34404

                    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