问题描述
我想使用qt jambi制作截图。我使用集成的webkit浏览器,它就像一个魅力。问题是:
I want to use qt jambi to make screenshots. I use the integrated webkit browser and it works like a charm. The problem is:
如何初始化
QApplication.initialize(args);
QApplication.initialize(args);
在主要方法之外。由于我想在不调用外部程序的情况下从我的java Web应用程序中制作屏幕截图,我需要在main之外初始化QApplication。
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]
有没有人知道如何在main方法之外初始化QApplication。
Does anyone have an idea how to initialize the QApplication outside of the main method.
推荐答案
只需使用如下:
其中Form为:
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();
这篇关于qtjambi - 主方法之外的QApplication.initialize的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!