Results 1 to 4 of 4

Thread: QWebView & external css file

  1. #1
    Join Date
    Nov 2011
    Posts
    79
    Thanks
    5
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QWebView & external css file

    I use QWebView as template editor in my app.
    Structure of my project:
    Qt Code:
    1. MyProject/
    2. app.exe
    3. Templates/
    4. style.css
    5. template.html
    To copy to clipboard, switch view to plain text mode 
    This is simplified HTML code of template:
    Qt Code:
    1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    2. <html><head>
    3. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    4. <link rel="stylesheet" type="text/css" href="style.css" />
    5. </head>
    6. <body>
    7. <p>Some content</p>
    8. </body>
    9. </html>
    To copy to clipboard, switch view to plain text mode 
    in same directory I have style.css:
    Qt Code:
    1. html,body {
    2. font-family:Tahoma;
    3. }
    To copy to clipboard, switch view to plain text mode 
    But QWebView ignores my styles. I guess it just can't load it.
    Code for content loading:
    Qt Code:
    1. QFile file(QApplication::applicationDirPath() + "/Templates/template.html");
    2. QUrl url(QApplication::applicationDirPath() + "/Templates");
    3. ui->textEdit->setHtml(file.readAll(),url);
    To copy to clipboard, switch view to plain text mode 
    I tried
    Qt Code:
    1. ui->textEdit->settings()->setUserStyleSheetUrl(url);
    To copy to clipboard, switch view to plain text mode 
    and
    Qt Code:
    1. QUrl url("file://" + QApplication::applicationDirPath() + "/Templates");
    To copy to clipboard, switch view to plain text mode 
    but nothing helps.
    So how can I load local css file to QWebView?

  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: QWebView & external css file

    Your code snippet does not open the file before trying to read from it, which may explain a blank display.

    Why don't you simply:
    Qt Code:
    1. webView->load(QUrl::fromLocalFile(QApplication::applicationDirPath() + "/Templates/template.html"));
    To copy to clipboard, switch view to plain text mode 
    and let the browser find the styles based on the header link element?


    BTW: Why have you given a QWebView the truly misleading name of textEdit?

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

    folibis (27th November 2013)

  4. #3
    Join Date
    Nov 2011
    Posts
    79
    Thanks
    5
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QWebView & external css file

    Thanks ChrisW67, you gave me hope to dig it one more time )
    I changed setHtml() to load() and now it works fine. with all css files and page()->settings()->setUserStyleSheetUrl() works too.

    At first it loads blank page just because my file was page.tpl and not page.html ))))))


    Quote Originally Posted by ChrisW67 View Post
    BTW: Why have you given a QWebView the truly misleading name of textEdit?
    Heh ... At first it was QTextEdit but it was not good for html editing. So I changed it to QWebView but all names remained )

  5. #4
    Join Date
    Nov 2011
    Posts
    79
    Thanks
    5
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QWebView & external css file

    It is so strange ... now setHtml() works too. I mean load external css files etc.
    It was problem with file extension. I just changed it from *.tpl to *.html and it works fine ))

Similar Threads

  1. External resources and QWebView::setHtml
    By anoraxis in forum Qt Programming
    Replies: 0
    Last Post: 27th March 2012, 22:00
  2. QWebView sometimes not loading external resources
    By complexgeek in forum Qt Programming
    Replies: 2
    Last Post: 6th June 2011, 02:37
  3. Replies: 1
    Last Post: 27th September 2010, 14:21
  4. QML file not loading external JS file?
    By anothertest in forum Qt Programming
    Replies: 2
    Last Post: 14th April 2010, 09:11
  5. Replies: 3
    Last Post: 25th February 2010, 08:53

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.