白季飞龙的个人主页

QtJambi大杂烩

目前,Java对Qt的支持只有QtJambi,QtJambi只支持Qt4.8

官方的Sourceforge上的Maven仓库挂了,mvnrepository上搜到的支持QtJambi的仓库只有http://uqbar-wiki.org/mvn/releases/,而且版本略老(4.8.5),依赖的libpng15我的系统上没有(只有libpng16),因此,我把从官方下的jar包放到了自己的Maven仓库(https://raw.github.com/baijifeilong/repo/mvn)

build.gradle

dependencies {
    compile 'com.trolltech.qt:qtjambi:4.8.7'
    compile 'com.trolltech.qt:qtjambi-linux64-gcc:4.8.7'
}

repositories {
    maven {
        url 'https://raw.github.com/baijifeilong/repo/mvn'
    }
}

app.kt

package bj

import com.trolltech.qt.gui.QApplication

/**
 * Created by BaiJiFeiLong@gmail.com at 18-8-13 下午9:13
 */

fun main(args: Array<String>) {
    QApplication.initialize(args)
    QApplication.aboutQt()
    QApplication.execStatic()
}

自定义槽函数示例

package bj;

import com.trolltech.qt.core.Qt;
import com.trolltech.qt.gui.*;

import java.time.LocalDateTime;

public class App {

    public static void main(String[] args) {
        QApplication.initialize(new String[]{null, "-style=Windows"});
        new Window().show();
        QApplication.execStatic();
    }
}

class Window extends QWidget {
    private QLabel label = new QLabel("问天");

    @SuppressWarnings("unused")
    void updateLabel() {
        label.setText(LocalDateTime.now().toString().replace('T', ' '));
    }

    Window() {
        setWindowTitle("白云千载空悠悠");
        resize(999, (int) (999 * 0.618));
        QVBoxLayout layout = new QVBoxLayout();
        layout.addWidget(label);
        QPushButton button = new QPushButton("明月几时有");
        layout.addWidget(button);
        layout.setSpacing(0);
        layout.setContentsMargins(0, 0, 0, 0);
        setLayout(layout);
        setStyleSheet("font-size: 72px; color: magenta");
        button.setStyleSheet("* { background: seagreen; border: none }" +
                "*:pressed { background: darkgreen }");
        label.setStyleSheet("background: steelblue; border: none");
        label.setAlignment(Qt.AlignmentFlag.AlignCenter);
        button.setSizePolicy(QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Preferred);
        button.clicked.connect(this, "updateLabel()");
    }
}

文章首发: https://baijifeilong.github.io


漫漫路,莫论逍遥;潜心修,只为悟道