Results 1 to 5 of 5

Thread: QDesktopServices re-opens previous pdf instead of currently selected pdf

  1. #1
    Join Date
    Nov 2015
    Location
    Vermont
    Posts
    52
    Thanks
    15
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    MacOS X Windows

    Default QDesktopServices re-opens previous pdf instead of currently selected pdf

    I am using QDesktopServices to open pdfs generated by QWebEnginePage and saved to the local filesystem with printToPdf(). I use a QFileSystemWatcher to watch the directory the pdf is being saved in, otherwise QDesktopServices will try to open the pdf before it exists. The code for opening the pdf is as follows:

    Qt Code:
    1. QUrl url_of_file = QUrl::fromLocalFile("path/to/file.pdf");
    2.  
    3. QDesktopServices::openUrl(url_of_file);
    To copy to clipboard, switch view to plain text mode 

    The above works totally fine for the first pdf that is generated. It opens the correct pdf in the most appropriate program (e.g., Adobe Acrobat). The problem that I am encountering is that the second time this is used (i.e., a second pdf has been generated and attempted to open without closing the program in between) the first pdf that was generated gets re-opened instead of the correct, second pdf that was just generated. The problem seems to be specifically with the QDesktopServices class, since if I output url_of_file it does have the path to the correct pdf, but for some reason it is opening the wrong one. The same problem occurs both in a Mac environment using Preview to view pdfs, and a Windows environment using Adobe Acrobat. Any idea why this might be? I have confirmed that the second/correct pdf does exist at the time of attempted opening. Is there some sort of caching that occurs with QDesktopServices? The file paths are 90% the same until the end, since the difference in file names is mainly based on the date and time they were generated, but I can't imagine that there's some sort of character limit. I'm stumped!

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QDesktopServices re-opens previous pdf instead of currently selected pdf

    its hard to say without seeing the real code.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Nov 2015
    Location
    Vermont
    Posts
    52
    Thanks
    15
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    MacOS X Windows

    Default Re: QDesktopServices re-opens previous pdf instead of currently selected pdf

    The thing is that if I output the url_of_file it is the correct path to the correct file, so the problem seems to be specifically related to QDesktopServices. I have tried to cobble together the relevant pieces of my code as best I could below. It all works fine right up until QDesktopServices::OpenUrl, when it seems to open a different url than it is passed in the function.

    Qt Code:
    1. void PrintableWorksheet::initMembers()
    2. {
    3. page = new QWebEnginePage();
    4. layout = new QPageLayout(QPageSize(QPageSize::A4),QPageLayout::Landscape, QMarginsF());
    5.  
    6. // The page loads html asynchronously, so we have to wait for loadFinished before printing
    7. connect(page, SIGNAL(loadFinished(bool)), this, SLOT(printPageToPdf(bool)));
    8. }
    9.  
    10. void PrintableWorksheet::printFile()
    11. {
    12. // html is a QString created in separate function
    13. page->setHtml(qstring_of_html);
    14. }
    15.  
    16. void PrintableWorksheet::printPageToPdf(bool ok)
    17. {
    18. if (ok) {
    19. page->printToPdf(file_path, *layout); // file_path is set in separate function
    20. } else {
    21. QMessageBox error_box(QMessageBox::Critical,
    22. "ERROR",
    23. "Error: the pdf failed to load properly.");
    24. error_box.exec();
    25. }
    26. }
    27.  
    28. // In ReceivingLogWidget class, the PrintableWorksheet object is created and used and a QFileSystemWatcher looks for when the pdf is generated
    29. QFileSystemWatcher *file_watcher = new QFileSystemWatcher();
    30. connect(file_watcher, SIGNAL(directoryChanged(QString)), this, SLOT(openPrintedPage(QString)));
    31.  
    32. QString file_path = QDir::homePath()+"/Desktop/Backstock_Worksheets";
    33. PrintableWorksheet worksheet;
    34. worksheet->setFilePath(file_path, file_name);
    35. file_watcher->addPath(file_path);
    36. worksheet->printFile();
    37.  
    38. void ReceivingLogWidget::openPrintedPage(QString path)
    39. {
    40. Q_UNUSED(path);
    41.  
    42. QUrl url_of_file = QUrl::fromLocalFile(worksheet->getFilePath());
    43.  
    44. QDesktopServices::openUrl(url_of_file);
    45. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QDesktopServices re-opens previous pdf instead of currently selected pdf

    If you add a debug to check the file path, does it output the correct path?
    Qt Code:
    1. void ReceivingLogWidget::openPrintedPage(QString path)
    2. {
    3. Q_UNUSED(path);
    4.  
    5. QUrl url_of_file = QUrl::fromLocalFile(worksheet->getFilePath());
    6. qDebug()<<url_of_file.toString();
    7.  
    8. QDesktopServices::openUrl(url_of_file);
    9. }
    To copy to clipboard, switch view to plain text mode 

    One quick way to test is to delete the first file while the application is still running and then trigger the creation of the second, and seeing what happens.

    Can you show the WorkSheet::getFilePath() function?
    And the code responsible for setting the file path variable.
    Last edited by high_flyer; 18th April 2017 at 16:53.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  5. #5
    Join Date
    Nov 2015
    Location
    Vermont
    Posts
    52
    Thanks
    15
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    MacOS X Windows

    Default Re: QDesktopServices re-opens previous pdf instead of currently selected pdf

    Aha, the tip about deleting the old file and then triggering opening the new one was revealing. It looks like it's maybe re-writing the same pdf under a new name. It was getting the correct file path as I mentioned previously, but it looks like the content of the pdf was the problem. I could have sworn I checked that, but obviously I didn't. I'm guessing that I'm not clearing the html template properly or something. I'll start investigating there. Thanks for your help!

Similar Threads

  1. Qt Widget opens in new window
    By spikestar in forum Qt Programming
    Replies: 1
    Last Post: 10th September 2012, 14:32
  2. Replies: 0
    Last Post: 16th November 2011, 21:57
  3. How to load file when program opens
    By TomJoad in forum Newbie
    Replies: 1
    Last Post: 10th April 2011, 22:57
  4. Dialog opens twice
    By ulmly in forum Qt Programming
    Replies: 9
    Last Post: 30th June 2009, 20:25
  5. Shell opens when start Qt app
    By giusepped in forum Qt Programming
    Replies: 15
    Last Post: 2nd February 2009, 07:20

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.