qtjambi - QApplication.initialize 在主方法之外

发布于 2024-08-29 20:26:39 字数 3741 浏览 11 评论 0原文

我想用qt jambi 来截图。我使用集成的 webkit 浏览器,它非常有用。问题是:

如何初始化

QApplication.initialize(args);

在 main 方法之外。由于我想在不调用外部程序的情况下从我的 java web 应用程序中截取屏幕截图,因此我需要在 main 之外初始化 QApplication。

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();
        }
    }

}

这不起作用,应用程序因致命错误而崩溃:

# 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]

有谁知道如何在主方法之外初始化 QApplication 吗?

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

标点 2024-09-05 20:26:39

尝试使用 new String[0] 而不是 null 调用初始化。

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

泛滥成性 2024-09-05 20:26:39

只需像这样使用:
其中表格是:

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();

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();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文