Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Retrieve the file path using Qprinter or Qpainter class.



  • Hi all,

    I want to create a log file in a path from where the user creates a file for "print to file"(The log file should be created in the same folder, from where the print file is chosen). Currently I am opening the save dialog by calling the QPainter constructor as follows:

        QPainter GraphPainter(&SeqPrint);
    

    where SeqPrint is the object of QPrinter.

    I cannot find any APIs of QPrinter or QPainter which can fetch the file path dynamically according to user's choice from the opened save dialog. Though there are docName() and OutPutFileName() functions provided by QPrinter class, they will not return the file path unless it is set before(My requirement is to choose the file path from the dialog dynamically).

    I could see that the QFileDialog class provides many APIs to fetch the file path dynamically from the open file dialog. Since it is mandatory that the save dialog should be opened by calling the QPainter constructor, I cannot opt for QFileDialog class.

    Please suggest a solution for this.



  • @Qt_User444

    Just use the filename/path from your save dialog. Of course you have to specify it in your QPrinter's setOutputFileName(QString fn).
    Otherwise how should your pdf printer know where to print?!

    @Qt_User444 said in Retrieve the file path using Qprinter or Qpainter class.:

    Since it is mandatory that the save dialog should be opened by calling the QPainter constructor

    This is not possible unless you modify your Qt source code.


  • Lifetime Qt Champion

    @Qt_User444 said in Retrieve the file path using Qprinter or Qpainter class.:

    Since it is mandatory that the save dialog should be opened by calling the QPainter constructor

    Why should QPainter open any dialogs?! It's job is painting, not showing any dialogs...
    But if, for some strange reason, you really need to do so then subclass QPainter and add your own constructor.


Log in to reply