March 31, 2012

poporacer poporacer
Lab Rat
48 posts

QTextBrowser setSearchPaths not working correctly

 

I am trying to set up a user manual and followed the method in C++ Gui Programming with Qt. It worked exactly as planned on Qt Creator version 2.0.1 using Qt 4.7. I then transferred the code to Creator Version 2.4.1 using Qt 4.7.4 and it didn’t work. At first the browser window would not load anything. My file structure has the application and then a subfolder for the manual, then a subfolder in manual for the graphic files. So I have root/manual/graphics. I modified the code and now the manual shows, but it does not show the pictures unless I move the graphics subfolder to the root directory. Here is what I have: Original code:

  1. DlgHelpBrowser::DlgHelpBrowser(const QString &path, const QString &page,
  2.                                QWidget *parent) :
  3.     QDialog(parent),
  4.     ui(new Ui::DlgHelpBrowser)
  5. {
  6.     ui->setupUi(this);
  7.     setAttribute(Qt::WA_DeleteOnClose);
  8.     setAttribute(Qt::WA_GroupLeader);
  9.  
  10.     connect (ui->btnHome, SIGNAL(clicked()),
  11.              ui->textBrowser, SLOT (home()));
  12.     connect (ui->btnBack, SIGNAL(clicked()),
  13.              ui->textBrowser, SLOT (backward()));
  14.     connect (ui->btnClose, SIGNAL(clicked()),
  15.              this, SLOT (close()));
  16.     ui->textBrowser->setSearchPaths(QStringList() << path <<"/graphics");
  17.     ui->textBrowser->setSource(page);
  18.  }

  1. void DlgHelpBrowser::showPage(const QString &page)
  2. {
  3.     QString path= directoryOf("manual").absolutePath();
  4.     DlgHelpBrowser *browser = new DlgHelpBrowser ( path, page);
  5.     browser->resize(1000, 800);
  6.     browser->show();
  7. }

  1. QDir DlgHelpBrowser::directoryOf(const QString &subdir)
  2. {
  3.     QDir dir(QApplication::applicationDirPath());
  4.     if (dir.dirName().toLower()== "debug" || dir.dirName().toLower()=="release")
  5.         dir.cdUp();
  6.  
  7.     dir.cd(subdir);
  8.     return dir;
  9. }

The above code works correctly in Creator 2.0.1 with Qt 4.7 but with Qt 4.7.4 the QTextBrowser is blank and an error message “QTextBrowser: No document for UserManual.html”
To get UserManual to show I had to change the code to:

  1. DlgHelpBrowser::DlgHelpBrowser(const QString &path, const QString &page,
  2.                                QWidget *parent) :
  3.     QDialog(parent),
  4.     ui(new Ui::DlgHelpBrowser)
  5. {
  6.     ui->setupUi(this);
  7.     setAttribute(Qt::WA_DeleteOnClose);
  8.     setAttribute(Qt::WA_GroupLeader);
  9.  
  10.     connect (ui->btnHome, SIGNAL(clicked()),
  11.              ui->textBrowser, SLOT (home()));
  12.     connect (ui->btnBack, SIGNAL(clicked()),
  13.              ui->textBrowser, SLOT (backward()));
  14.     connect (ui->btnClose, SIGNAL(clicked()),
  15.              this, SLOT (close()));
  16.     QUrl loadPage =QUrl::fromLocalFile(page); //New line added
  17.     ui->textBrowser->setSearchPaths(QStringList() << path <<"/graphics"); // I even tried path + "/graphics"
  18.     ui->textBrowser->setSource(loadPage);//Variable name changed
  19. }

But with this code, the images in the graphics subfolder do not show up. If I move the graphics folder to the root of the application, the images show… Which is wierd, because the html code references the images as src=“graphics/mainMenu.jpg”. Any ideas why this is working strangely? And how do I get it to work properly? I really don’t want to have 2 copies of the graphics subfolder.

0 replies

 
  ‹‹ Qt Research/Academic/Journal Papers/Articles      Change style ››

You must log in to post a reply. Not a member yet? Register here!