Search code examples
qt-jambi

Converting simple QWebView example to Java


How would you convert the following simple QT example in C using the QWebView widget to Java (QtJambi):

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    QWebView view;
    view.load(QUrl("http://www.trolltech.com/"));
    view.show();
    return app.exec();
}

(Located at: http://doc.qt.nokia.com/qq/qq26-webplugin.html#qtwebkitbasics)

I could be mistaken but I think I recall such an example being present in the Qt-Jambi javadoc last year, but I can't find it any more, when I go to http://qt-jambi.org/documentation it says "Apidoc of newest built (sic) is not still working"


Solution

  • The API in Qt Jambi is very similar to the original Qt API so the samples can be translated almost directly.

    So the C++ version

    QWebView view;
    view.load(QUrl("http://www.trolltech.com/"));
    

    Is translated to the following in Java

    QWebView view = new QWebView();
    view.load(new QUrl("http://www.trolltech.com/"));
    

    The rest of the application (creating the main window, running the app) can be found in the hello world tutorial.

    I don't have a working environment on my home mac, but this sample should work:

    import com.trolltech.qt.core.*;
    import com.trolltech.qt.gui.*;
    import com.trolltech.qt.webkit.*;
    
    public class SO12093494 extends QMainWindow {
    
       private QWebView webView;
    
       public SO12093494() { this(null); }
       public SO12093494(QWidget parent) {
          super(parent);
    
          webView = new QWebView();
          setCentralWidget(webView);
       }
    
       public void loadUrl(String url) {
          webView.load(new QUrl(url));
       }
    
       public static void main(String[] args) {
          QApplication.initialize(args);
    
          SO12093494 app = new SO12093494();
          app.loadUrl("http://www.trolltech.com");
          app.show();
    
          QApplication.exec();
       }
    }