JChan
JChan

Reputation: 1451

How to display a web page with dynamic data using c++ and QT

I am trying to display a web page using C++/QT with dynamic data.

  1. If user clicks, the web page should be displayed
  2. The web page should contains a table format data. The data will be read from the table using C++ and will be dynamic.

Upvotes: 3

Views: 2983

Answers (3)

Frexus
Frexus

Reputation: 150

You can set the HTML content of QWebView using setHtml() function. Have a look at the documentation. Call it when the user clicks the button to load HTML. To get the contents of loaded HTML code you can use two methods provided by QWebFrame inside your QWebView:
webview->page()->mainFrame()->toPlainText()
webview->page()->mainFrame()->toHTML()
You will have to parse the HTML to read data from table shown in QWebView.

Upvotes: 3

rocky147
rocky147

Reputation: 1

Have a look at QWebView, http://doc.qt.io/qt-5/qwebview.html

Example from the website above

QWebView *view = new QWebView(parent);
    view->load(QUrl("http://qt.nokia.com/"));
    view->show();

Create this view in the event handler from where the User clicks and all should work fine.

Upvotes: 0

Ammar
Ammar

Reputation: 2007

Answer to First Question

You can use QWebView to display your start-up html using QWebView::setHtml() or QWebView::load() function.

Answer to Second Question

Regarding this question, there can be multiple ways in which you can set dynamic data in your page.

One way is you can use javascript function to update your html table. Which you will insert in the html in <script> tag. Now you can pass data (read through c++) using following function..

webView->page()->mainFrame()->evaluateJavaScript(yourJavascript);

Well you will have to generate javascript function call string yourJavascript from the data read through c++.

But if you can retrieve your data from database in JSON format, it will be very easy for you. For example you can use QJson third party library to parse and serialize JSON data as per following..

QByteArray data;   // Say data arrived from the database is stored in this object.

QJson::Parser parser;
QVariantMap map = parser.parse(data).toMap();

// Now serialize it and pass it to javascript function as an argument..

QJson::Serializer serializer;
QString javaScript = "updateHtmlView(" + serializer.serialize(map) + ");";

webView->page()->mainFrame()->evaluateJavaScript(javaScript);

So now you can update your html table by reading that JSON object in javascript.

Upvotes: 2

Related Questions