Results 1 to 17 of 17

Thread: QTextBrowser setSearchPaths doesn't work

  1. #1
    Join Date
    Aug 2010
    Posts
    107
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Unhappy QTextBrowser setSearchPaths doesn't work

    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 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. I modified the code and now the manual shows, but it does not show the pictures. Here is what I have:
    Original code
    Qt 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. }
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    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. }
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    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. }
    To copy to clipboard, switch view to plain text mode 
    The code to run on Creator 2.4.1
    Qt 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. QUrl loadPage =QUrl::fromLocalFile(page);//New line here
    17. ui->textBrowser->setSearchPaths(QStringList() << path <<"/graphics"); //I also tried path + "/graphics"
    18. ui->textBrowser->setSource(loadPage);
    19. ui->textBrowser->zoomIn(2);
    20. }
    To copy to clipboard, switch view to plain text mode 

    This shows the HTML file but the pictures are not there, just a picture icon. Any ideas?

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QTextBrowser setSearchPaths doesn't work

    Did you really need another thread for the same problem?

    Does a path "/graphics" exist on your machine? Which drive would that be on Windows machines? This is not a path relative to the current working directory or relative to any other path in the search path.

  3. #3
    Join Date
    Aug 2010
    Posts
    107
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTextBrowser setSearchPaths doesn't work

    Sorry for posting again. The problem is a little different from the original one. I got the html file to show, now I keed the images to show. I haven't gotten a response in over a week. I do have a /graphics folder it is in the manual folder.How do I give it a path relative to the working directory? I tried path+"/graphics" and it didn't work. My file structure is "workingDirectory"/manual/graphics. The html code is in the manual folder and the images for the html is in the graphics folder. What am I missing?

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QTextBrowser setSearchPaths doesn't work

    You are missing correct paths either in your search path or in the actual HTML of your pages.

    If your HTML page in the manual directory refers to:
    Qt Code:
    1. <img src="graphics/diagram.png" />
    To copy to clipboard, switch view to plain text mode 
    the diagram.png exists in the graphics folder, and the HTML is loading then it should just work.

  5. #5
    Join Date
    Aug 2010
    Posts
    107
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTextBrowser setSearchPaths doesn't work

    The code for the HTML files is
    Qt Code:
    1. <img style="width: 682px; height: 364px;" src="graphics/mainMenu.jpg">
    To copy to clipboard, switch view to plain text mode 
    I tried adding the trailing foward slash like you had but it still didn't work. And I have tried several variations of setting the search paths, and none of them worked. I even tried hardcoding the path and it didn't work. If I move the graphics folder to the current working folder, it all works. For some reason I cannot get the setSearchPaths to work correctly. And everything worked perfectly with Qt 4.7 but not 4.7.4?? Any ideas where I am going wrong?

    The code for the HTML files is
    Qt Code:
    1. <img style="width: 682px; height: 364px;" src="graphics/mainMenu.jpg">
    To copy to clipboard, switch view to plain text mode 
    I tried adding the trailing foward slash like you had but it still didn't work. And I have tried several variations of setting the search paths, and none of them worked. I even tried hardcoding the path and it didn't work. If I move the graphics folder to the current working folder, it all works. For some reason I cannot get the setSearchPaths to work correctly. And everything worked perfectly with Qt 4.7 but not 4.7.4?? Any ideas where I am going wrong?


    Added after 35 minutes:


    Here is an example:
    test.zip
    Last edited by poporacer; 1st April 2012 at 17:00.

  6. #6
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QTextBrowser setSearchPaths doesn't work

    Your code as posted works just fine here on Linux/Qt 4.7.4/GCC, Windows XP/Qt 4.7.4/MingW, and Windows XP/Qt 4.8.0/MingW. It even works without the erroneous "/graphics" folder in the search path. Does it fail when the image is a PNG? Do you have the Qt JPEG plugin?

  7. #7
    Join Date
    Aug 2010
    Posts
    107
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTextBrowser setSearchPaths doesn't work

    I tried with a .png file and same thing. I am running Windows 7, 64 bit, Qt 4.8 (32 bit), mingw. (If I go to about Qt, it says based on Qt 4.7.4, but in my project setting it says version 4.8.0) I have the qjpeg4.dll in my release version in a folder imageformats. Any ideas why it won't work on my machine? Maybe a Windows 7 bug?

  8. #8
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QTextBrowser setSearchPaths doesn't work

    This is curious. I just grabbed the source again, built it on the same machine with the intention of running it on Win 7 64, and it does not behave even on the XP machine. It's possible that yesterday I was accidentally getting 4.7.3 libraries.

    Seems to be a known issue:
    https://bugreports.qt-project.org/browse/QTBUG-24077
    The comments on this commit imply that this occurred to fix a security bug in QUrl.

    I can confirm that n absolute file path works but is impractical. I can also confirm that:
    Qt Code:
    1. <img style="width: 1280px; height: 703px;" alt="" src="file:graphics/test.jpg">
    To copy to clipboard, switch view to plain text mode 
    also worked and preserves the relative path.

  9. The following user says thank you to ChrisW67 for this useful post:

    poporacer (4th April 2012)

  10. #9
    Join Date
    Aug 2010
    Posts
    107
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Thumbs up Re: QTextBrowser setSearchPaths doesn't work

    Thanks,
    I searched the bug reports but I guess I missed that. I really appreciate the time you spent to help me. I was going crazy when things worked fine with the older libraries. I think I will install the older libraries and use them.

    Thanks a million!

  11. #10
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QTextBrowser setSearchPaths doesn't work

    Or just adjust the local URLs in your HTML and future proof them: "graphics/test.jpg" ==> "file:graphics/test.jpg"

  12. #11
    Join Date
    Aug 2010
    Posts
    107
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTextBrowser setSearchPaths doesn't work

    One other problem I have with the QTextBrowser. It looks like you can't load an html file to an anchor. I have the manual open to the relative anchor point to the part of the manual where they are in the program.
    I call this with:
    Qt Code:
    1. DlgHelpBrowser::showPage("UserManual.html#6.3")
    To copy to clipboard, switch view to plain text mode 
    I will search to see if there is a fix to this.

  13. #12
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QTextBrowser setSearchPaths doesn't work

    That's correct behaviour. You are passing a bare string to a function expecting a QUrl, which results in the string being treated literally as a file name by the conversion constructor (because there's no URI scheme). Use QUrl:
    Qt Code:
    1. QUrl url("file:UserManual.html#anchor");
    2. ui->textBrowser(url);
    3.  
    4. // or perhaps
    5. QString fileName = "UserManual.html";
    6. ...
    7. QUrl url = QUrl::fromLocalFile(fileName);
    8. url.setEncodedFragment("anchor");
    9. ui->textBrowser(url);
    To copy to clipboard, switch view to plain text mode 

  14. #13
    Join Date
    Aug 2010
    Posts
    107
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTextBrowser setSearchPaths doesn't work

    I haven't had a chance to check your second suggestion, but I think I am following your first suggestion already. Here is the relevant code:
    Qt Code:
    1. DlgHelpBrowser::showPage("UserManual.html#6.3")//passes anchor
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. void DlgHelpBrowser::showPage(const QString &page)
    2. {
    3. QString path= directoryOf("manual").absolutePath();
    4. DlgHelpBrowser *browser = new DlgHelpBrowser ( path, page);//creates new QTextbrowser
    5. browser->resize(1000, 800);
    6. browser->show();
    7. }
    To copy to clipboard, switch view to plain text mode 
    Qt 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. QUrl loadPage =QUrl::fromLocalFile(page);//here page is converted to QUrl...shouldn't this work?
    17. ui->textBrowser->setSearchPaths(QStringList() << path);
    18. ui->textBrowser->setSource(loadPage);// I will try ui->textBrowser(loadPage) and see if it works
    19.  
    20. }
    To copy to clipboard, switch view to plain text mode 
    Am I doing this correctly? It is strange that in 4.7 you didn't need to convert to QUrl, it handled QStrings
    I will try your second method as well and see if it works.
    Again, many thanks for all your help!

  15. #14
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QTextBrowser setSearchPaths doesn't work

    At line 16 of your last listing you are telling QUrl that path is a local file name, i.e. there exists a file literally called "UserManual.html#6.3". QUrl will encode the '#' so that it is not interpreted as the fragment separator.

    Contrast these:
    Qt Code:
    1. QUrl url1 = QUrl::fromLocalFile("UserManual.html#6.3");
    2. qDebug() << url1.path() << url1.fragment() << url1.toEncoded();
    3. // "UserManual.html#6.3" "" "file:UserManual.html%236.3"
    4.  
    5. QUrl url2("UserManual.html#6.3");
    6. qDebug() << url2.path() << url2.fragment() << url2.toEncoded();
    7. // "UserManual.html" "6.3" "UserManual.html#6.3"
    To copy to clipboard, switch view to plain text mode 

  16. #15
    Join Date
    Aug 2010
    Posts
    107
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTextBrowser setSearchPaths doesn't work

    I see what you are saying but I can't get it to work. I tried several things but I can't figure out which function to pass the path and fragment to QTextBrowser.

  17. #16
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QTextBrowser setSearchPaths doesn't work

    You use QTextBrowser::setSource() but you pass it a correctly constructed QUrl. I have already given you several ways to construct a QUrl from the file path and anchor (fragment).

  18. The following user says thank you to ChrisW67 for this useful post:

    poporacer (7th April 2012)

  19. #17
    Join Date
    Aug 2010
    Posts
    107
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTextBrowser setSearchPaths doesn't work

    It took me a while to figure it out and here is what I did to get it to work. Originally I was testing to see what part of the program the user was at and then pass the relevant file name (for example: UserManual.html#sec1)to the showPage function. I had problems with the showPage function because of the constraints of the requirement to use QUrl::fromLocalFile to show the html file properly. I couldn't figure out how to get the path and fragment AND use the QUrl::fromLocalFile. Because I am always using the same file to open (UserManual.html), I hard coded the file name and then in the the test function I passed only the fragment. If I need to have a dynamic document and fragment, I think I will pass the document name and fragment as seperate items to the function. I am not sure if this is the best method, but in case someone else runs across the same problem here is the code I used:

    Qt Code:
    1. //test function
    2. void MainWindow::on_btnHelp_clicked()
    3. {
    4. switch (ui->MainStackWidget->currentIndex())//what stacked widget the user is looking at
    5. {
    6. case 0:
    7. DlgHelpBrowser::showPage("");//no fragment
    8. break;
    9. case 1:
    10. DlgHelpBrowser::showPage("sec1");
    11. break;
    12. case 2:
    13. DlgHelpBrowser::showPage("sec2");
    14. break;
    15. case 3:
    16. DlgHelpBrowser::showPage("sec3");
    17. break;
    18. }
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. //part of showPage function
    2. QUrl loadPage =QUrl::fromLocalFile("UserManual.html");
    3. loadPage.setFragment(page);//fragment passed from test function
    4. ui->textBrowser->setSearchPaths(QStringList() << path);//path is generated from QApplication::applicationDirPath()
    5. ui->textBrowser->setSource(loadPage);
    To copy to clipboard, switch view to plain text mode 
    If there is a better way, let me know. ChrisW67, Thanks for all your help!!!
    Last edited by poporacer; 7th April 2012 at 17:20. Reason: spelling corrections

Similar Threads

  1. Replies: 0
    Last Post: 12th August 2010, 16:05
  2. Shortcut doesn't work
    By stella1016 in forum Qt Programming
    Replies: 1
    Last Post: 28th May 2009, 02:37
  3. setFont doesn't work
    By DadaLee in forum Qt for Embedded and Mobile
    Replies: 1
    Last Post: 11th August 2008, 06:29
  4. QRegExp doesn't seem to work as it should
    By thomaspu in forum Qt Programming
    Replies: 3
    Last Post: 21st December 2007, 07:49
  5. setTabStopWidth doesn't work
    By discostu in forum Qt Programming
    Replies: 3
    Last Post: 19th November 2007, 08:29

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.