把核心功能,封装到Jqt类中,之后会慢慢扩展这个类。目前具有功能是加载ui文件,将ui文件转换为Java代码,登录界面显示。
package jqt; import io.qt.core.*; import io.qt.core.Qt; import io.qt.gui.QColor; import io.qt.gui.QPixmap; import io.qt.uic.Driver; import io.qt.widgets.QApplication; import io.qt.widgets.QSplashScreen; import io.qt.widgets.QWidget; import io.qt.widgets.tools.QUiLoader; public class Jqt { static { loader = new QUiLoader(); } private final static QUiLoader loader; private QSplashScreen splashScreen; private Jqt(){ } public Jqt(String[] args, String loginImage){ QApplication.setApplicationName("QtJambi"); QApplication.initialize(args); splashScreen = new QSplashScreen(); if (loginImage != null){ splashScreen.setPixmap(new QPixmap(loginImage)); splashScreen.show(); } } protected void finalize() throws Throwable { QApplication.shutdown(); super.finalize(); } public void showLoginMessage(String message){ showLoginMessage(message, new QColor(Qt.GlobalColor.black), Qt.AlignmentFlag.AlignRight, Qt.AlignmentFlag.AlignTop); } public void showLoginMessage(String message, QColor color, Qt.AlignmentFlag ... alignment){ splashScreen.showMessage(message, new Qt.Alignment(alignment).toInt(), color); } public void run(){ splashScreen.close(); if (QApplication.allWindows().count() != 0) QApplication.exec(); } public QWidget uiLoad(String ui){ QFile file = new QFile(ui); file.open(QIODevice.OpenModeFlag.ReadOnly); QWidget widget = loader.load(file); file.close(); return widget; } public void uiConvert(String inputFile, String outputDir, String targetPackage){ Driver driver = new Driver(); QCommandLineParser parser = new QCommandLineParser(); parser.setSingleDashWordOptionMode(QCommandLineParser.SingleDashWordOptionMode.ParseAsLongOptions); parser.addHelpOption(); parser.addVersionOption(); QCommandLineOption postfixOption = new QCommandLineOption("postfix"); postfixOption.setDescription("Postfix to add to all generated classnames."); postfixOption.setValueName("postfix"); parser.addOption(postfixOption); QCommandLineOption translateOption = new QCommandLineOption(QList.of("tr", "translate")); translateOption.setDescription("Use <function> for i18n."); translateOption.setValueName("function"); parser.addOption(translateOption); QCommandLineOption importOption = new QCommandLineOption(QList.of("i", "imports")); importOption.setDescription("Add imports to comma-separated packages and/or classes."); importOption.setValueName("imports"); parser.addOption(importOption); driver.option().postfix = parser.value(postfixOption); driver.option().translateFunction = parser.value(translateOption); driver.option().imports = QDir.fromNativeSeparators(parser.value(importOption)); driver.option().outputDir = QDir.fromNativeSeparators(outputDir); driver.option().targetPackage = targetPackage.contains("/")? targetPackage.replace('/','.'):targetPackage; driver.option().dependencies = true; driver.option().autoConnection = true; driver.option().noShellClass = false; driver.option().forceOutput = true; driver.option().idBased = true; driver.option().forceMemberFnPtrConnectionSyntax = true; driver.option().forceStringConnectionSyntax = true; driver.uic(inputFile, driver.option().outputDir,"java"); } }
浙公网安备 33010602011771号