PDA

View Full Version : QDesktopServices re-opens previous pdf instead of currently selected pdf



ce_nort
17th April 2017, 18:05
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:



QUrl url_of_file = QUrl::fromLocalFile("path/to/file.pdf");

QDesktopServices::openUrl(url_of_file);


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!

high_flyer
18th April 2017, 11:08
its hard to say without seeing the real code.

ce_nort
18th April 2017, 14:13
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.




void PrintableWorksheet::initMembers()
{
page = new QWebEnginePage();
layout = new QPageLayout(QPageSize(QPageSize::A4),QPageLayout:: Landscape, QMarginsF());

// The page loads html asynchronously, so we have to wait for loadFinished before printing
connect(page, SIGNAL(loadFinished(bool)), this, SLOT(printPageToPdf(bool)));
}

void PrintableWorksheet::printFile()
{
// html is a QString created in separate function
page->setHtml(qstring_of_html);
}

void PrintableWorksheet::printPageToPdf(bool ok)
{
if (ok) {
page->printToPdf(file_path, *layout); // file_path is set in separate function
} else {
QMessageBox error_box(QMessageBox::Critical,
"ERROR",
"Error: the pdf failed to load properly.");
error_box.exec();
}
}

// In ReceivingLogWidget class, the PrintableWorksheet object is created and used and a QFileSystemWatcher looks for when the pdf is generated
QFileSystemWatcher *file_watcher = new QFileSystemWatcher();
connect(file_watcher, SIGNAL(directoryChanged(QString)), this, SLOT(openPrintedPage(QString)));

QString file_path = QDir::homePath()+"/Desktop/Backstock_Worksheets";
PrintableWorksheet worksheet;
worksheet->setFilePath(file_path, file_name);
file_watcher->addPath(file_path);
worksheet->printFile();

void ReceivingLogWidget::openPrintedPage(QString path)
{
Q_UNUSED(path);

QUrl url_of_file = QUrl::fromLocalFile(worksheet->getFilePath());

QDesktopServices::openUrl(url_of_file);
}

high_flyer
18th April 2017, 15:44
If you add a debug to check the file path, does it output the correct path?


void ReceivingLogWidget::openPrintedPage(QString path)
{
Q_UNUSED(path);

QUrl url_of_file = QUrl::fromLocalFile(worksheet->getFilePath());
qDebug()<<url_of_file.toString();

QDesktopServices::openUrl(url_of_file);
}


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.

ce_nort
18th April 2017, 20:30
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!