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. How to get FileSaveDialog with QPrintDialog?

How to get FileSaveDialog with QPrintDialog?

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 2 Posters 540 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.
  • D Offline
    D Offline
    deleted385
    wrote on last edited by deleted385
    #1

    Normally, in other apps when I click print:

    cap1.PNG

    after selecting Microsoft Print to PDF a file save dialog pops up automatically BUT with these:

    QPrinter printer;
    QPrintDialog dialog(&printer);
    if(dialog.exec() != QPrintDialog::Accepted) return;
    ...
    

    I don't get that! How to get that file save dialog?

    1 Reply Last reply
    0
    • C Offline
      C Offline
      ChrisW67
      wrote on last edited by
      #4

      @Emon-Haque I do not have a Windows dev environment to hand. What happens if you do not do this?

      printer.setOutputFormat(QPrinter::PdfFormat);
      printer.setOutputFileName("test.pdf"); 
      
      D 1 Reply Last reply
      1
      • C Offline
        C Offline
        ChrisW67
        wrote on last edited by
        #2

        The Microsoft Print to PDF driver (not Qt) opens the Save dialog when the application (yours or anyone else's) prints to it and it has something to save. Your code snippet has chosen a printer but not actually printed anything.

        D 1 Reply Last reply
        1
        • C ChrisW67

          The Microsoft Print to PDF driver (not Qt) opens the Save dialog when the application (yours or anyone else's) prints to it and it has something to save. Your code snippet has chosen a printer but not actually printed anything.

          D Offline
          D Offline
          deleted385
          wrote on last edited by deleted385
          #3

          @ChrisW67, I shortened that for question. The actual function is:

          void QueryResultView::printTable(){
              QPrinter printer;
              QPrintDialog dialog(&printer);
              if(dialog.exec() != QPrintDialog::Accepted) return;
              int row = resultModel->rowCount() + 1;
              int column = resultModel->columnCount();
              printer.setPageSize(QPageSize::A4);
              printer.setPageMargins(QMarginsF(72,72,72,72), QPageLayout::Point);
              printer.setOutputFormat(QPrinter::PdfFormat);
              printer.setOutputFileName("test.pdf"); // here I want to give the chosen file name/path but I don't get any file save dialog
              printer.setResolution(fontMetrics().fontDpi());
              QTextDocument doc;
              doc.setPageSize(printer.pageRect(QPrinter::DevicePixel).size());
              QTextCursor cursor(&doc);
              QTextTableFormat tableFormat;
              tableFormat.setBorder(0);
              tableFormat.setCellSpacing(0);
              tableFormat.setCellPadding(0);
              tableFormat.setHeaderRowCount(1);
              tableFormat.setWidth(QTextLength(QTextLength::PercentageLength, 100));
              cursor.insertTable(row, column, tableFormat);
              addHeader(cursor);
              for (int r = 1; r < row; r++) {
                  for (int c = 0; c < column; c++) {
                      auto index = table->model()->index(r, c);
                      cursor.insertText( table->model()->data(index).toString() );
                      cursor.movePosition(QTextCursor::NextCell);
                  }
              }
              doc.print(&printer);
          }
          

          and if I omit printer.setOutputFileName("test.pdf"), in the Qt Creator output I get QPainter::begin(): Returned false.

          1 Reply Last reply
          0
          • C Offline
            C Offline
            ChrisW67
            wrote on last edited by
            #4

            @Emon-Haque I do not have a Windows dev environment to hand. What happens if you do not do this?

            printer.setOutputFormat(QPrinter::PdfFormat);
            printer.setOutputFileName("test.pdf"); 
            
            D 1 Reply Last reply
            1
            • C ChrisW67

              @Emon-Haque I do not have a Windows dev environment to hand. What happens if you do not do this?

              printer.setOutputFormat(QPrinter::PdfFormat);
              printer.setOutputFileName("test.pdf"); 
              
              D Offline
              D Offline
              deleted385
              wrote on last edited by
              #5

              @ChrisW67, yes, now I get that file save dialog. I also get this message in Qt Creator output Invalid parameter passed to C runtime function.

              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