qtjambi - QApplication.initialize outside of main method

青春壹個敷衍的年華 提交于 2019-12-08 06:01:17

问题


I want to use qt jambi to make screenshots. I use the integrated webkit browser and it works like a charm. The problem is:

How can I initialize

QApplication.initialize(args);

outside of the main method. Since I want to make screenshots out of my java web application without calling an external program I need to initialize QApplication outside of main.

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

import com.trolltech.qt.core.QObject;
import com.trolltech.qt.core.QUrl;
import com.trolltech.qt.gui.QApplication;
import com.trolltech.qt.gui.QImage;
import com.trolltech.qt.gui.QPainter;
import com.trolltech.qt.webkit.QWebPage;
import com.trolltech.qt.webkit.QWebView;

/**
 * 
 * @author Marc Giombetti Created on: 16.04.2010
 */
public class ScreenshotWithoutWindow {
    private QWebView browser;

    /**
     * @param args
     */
    public static void main(String[] args) {
        if (args.length > 2) {
            String file = args[0];
            String baseUrl = args[1];
            String writeTo = args[2];

            Thumbnailer thumbnail = new Thumbnailer(file, baseUrl, writeTo);

        } else {
            System.out.println("Please provide file, baseURL and savetoPath parameters");
            System.out
                    .println("For example:java -jar ScreenshotWithoutWindow C:\\test\\test.htm file:///C:/Marc/test/ c:\\screenshot\\screenshot-new.png");
        }
    }

    public static class Thumbnailer extends QObject {
        private QWebPage page = new QWebPage();
        public Signal0 finished = new Signal0();
        private static String file;
        private static String baseUrl;
        private static String writeTo;

        public Thumbnailer(String _file, String _baseUrl, String _writeTo) {
            file = _file;
            baseUrl = _baseUrl;
            writeTo = _writeTo;

            initialize();

        }

        private void initialize(){
            QApplication.initialize(null);
            page.loadFinished.connect(this, "render(Boolean)");
            try {
                page.mainFrame().setHtml(readFileAsString(file), new QUrl(baseUrl));
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            finished.connect(QApplication.instance(), "quit()");
            QApplication.exec();
        }

        void render(Boolean b) {
            page.setViewportSize(page.mainFrame().contentsSize());
            QImage image = new QImage(page.viewportSize(), QImage.Format.Format_ARGB32);
            QPainter painter = new QPainter(image);

            page.mainFrame().render(painter);
            painter.end();
            image.save(writeTo);
            System.out.println("Saved screenshot as "+writeTo);
            finished.emit();

        }

        private static String readFileAsString(String filePath) throws java.io.IOException {
            StringBuffer fileData = new StringBuffer(1000);
            BufferedReader reader = new BufferedReader(new FileReader(filePath));
            char[] buf = new char[1024];
            int numRead = 0;
            while ((numRead = reader.read(buf)) != -1) {
                String readData = String.valueOf(buf, 0, numRead);
                fileData.append(readData);
                buf = new char[1024];
            }
            reader.close();
            return fileData.toString();
        }
    }

}

This does not work and the application crashes with a fatal error:

# A fatal error has been detected by the Java Runtime Environment:
#
#  EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x670ddc30, pid=4588, tid=2776
#
# JRE version: 6.0_17-b04
# Java VM: Java HotSpot(TM) Client VM (14.3-b01 mixed mode windows-x86 )
# Problematic frame:
# C  [QtCore4.dll+0xddc30]

Does anyone have an idea how to initialize the QApplication outside of the main method.


回答1:


Try calling initialize with new String[0] instead of null.




回答2:


Just use like this: where Form is:

public class Form{
    Ui_Form ui;
    QWidget wid;
    ...
}
String[] a = {null, "a", "a"};
QApplication.initialize(a);
Form f = new Form();
f.wid.show();
QApplication.exec();


来源:https://stackoverflow.com/questions/2691894/qtjambi-qapplication-initialize-outside-of-main-method

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!