0

I have a situation where I want to bring up a web page that the user will interact with, and when they click on a submit button a response is sent, and I want to get that response in my Qt program and then destroy the view with the page. I am able to bring up the page like this:

  Dialog.resize(1500, 1000);
  loginView = new QWebView(&Dialog);
  loginView->setObjectName(QStringLiteral("webView"));
  loginView->setUrl(QUrl(QStringLiteral("http://foo.bar.com/baz/")));
  Dialog.exec();

But I have no idea how I could get the response sent when the user submits the page, nor how I would destroy the page.

Online I found info on doing it like this:

    QWebEngineView *view = new QWebEngineView(parent);
    view->load(QUrl("http://qt-project.org/"));
    view->show();

Or this:

#include"myWebView.h"

int main(int argc,char** argv)
{
      QApplication app(argc,argv);
      myWebView* view = new myWebView();

      view->load(QUrl("http://www.google.co.uk"));
      view->resize(500,500);

      view->page()->setLinkDelegationPolicy(QWebPage::DelegateAllLinks);
      view->show();
      return app.exec();
}
#include <QtWebKit>
#include <QtGui>
class myWebView:public QWebView
{
  Q_OBJECT
public:
  myWebView():QWebView()
  {
    connect(this,SIGNAL(linkClicked(QUrl)),this,SLOT(linkClicked(QUrl)));
  };
  ~myWebView()
  {};

private slots:  
  void linkClicked(QUrl url)
  {   
    QMessageBox::information(this,"Hello!","You Clicked: \n"+url.toString());
  }; 
};

Which seem better, but I still don't know how I'd get the response from the page back.

UPDATE: This is what my code currently is:

void cap::userLoginStatusButton_clicked()
{
  loginDialog = new QDialog();
  loginDialog->resize(1500, 1000);
  loginView = new QWebView(loginDialog);
  loginView->setObjectName(QStringLiteral("webView"));
  loginView->setUrl(QUrl(QStringLiteral("http://foo.bar.com/baz/")));
  connect(loginView, SIGNAL(urlChanged(QUrl)), this, SLOT(loginCompleted(QUrl)));
  loginDialog->exec();
}

void cap::loginCompleted(QUrl url)
{
  // here is where I am stuck - how do I get the JSON returned from http://foo.bar.com/baz/ here????
}
Larry Martell
  • 3,526
  • 6
  • 40
  • 76
  • Is the web is third party or you made it? – Apin May 03 '16 at 04:09
  • @Apin it's an in house app I made but it's part of another app and I can't make too many changes to it – Larry Martell May 03 '16 at 06:31
  • What do you mean by "the response from the page"? – IAmInPLS May 03 '16 at 06:42
  • check this one, maybe it help you : http://doc.qt.io/qt-4.8/qtwebkit-bridge.html. So it will bridge the Qt application to webkit via javascript – Apin May 03 '16 at 06:46
  • @AlexisP. The page sends a http response with json content and I need to get that – Larry Martell May 03 '16 at 07:34
  • @Apin QtWebKit is deprecated, don't use that! Larry, you will need to get it from JavaScript, then get it via the [QWebChannel/JavaScript API](http://doc.qt.io/qt-5/qtwebchannel-javascript.html) – IAmInPLS May 03 '16 at 08:09
  • See this answer : http://stackoverflow.com/a/36601118/5653461 – IAmInPLS May 03 '16 at 08:11
  • @Apin @Alexis P. I have read all the links you have provided but I still do not know how to get he JSON back from the URL I call. All those links talk about javascript interfacing. In my case there is no JS. The URL I call routs to some python code that returns a HTTP response with the JSON. I have updated my original question to show what my code is now. My page comes up, the `loginCompleted` is called, but once in there how do I get the returned object? – Larry Martell May 05 '16 at 14:55
  • You can call your C++ code from javascript from your web. So thats why I ask you at the first time that you have access to web in order to put javascript on it to call your C++ code. – Apin May 06 '16 at 01:15
  • I appreciate your trying to help me, but as I've said there is no JS involved with this at app. Just some very simple HTML and python. I think I've found another way to do this, but it's a bit kludgey. Instead of returning the JSON to Qt the python code will log in the DB that a request was made. Then I will add another endpoint that my Qt app can sent a GET request to that will query the DB and return the JSON. I don't love it, but I think it will work. – Larry Martell May 06 '16 at 09:56

0 Answers0