相信大家已经配置好编译环境了,现在我们用intellij idea创建一个maven项目,至于项目结构,大家自己按照喜好就行。我单独会有一个Shared项目,里面是所有核心功能的实现,防止重复书写,所以我的pom.xml如下

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>one</groupId>
    <artifactId>01-HelloQT</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>

        <dependency>
            <groupId>qt</groupId>
            <artifactId>Shared</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <!-- 配置编译器插件 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.11.0</version>
                <configuration>
                    <source>8</source>
                    <target>8</target>
                    <encoding>UTF-8</encoding>
                    <compilerArgs>
                        <arg>-Xplugin:Manifold</arg>
                    </compilerArgs>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

 但是我的Shared项目里的pom.xml如下

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>qt</groupId>
    <artifactId>Shared</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <!-- Manifold 核心依赖 -->
        <dependency>
            <groupId>systems.manifold</groupId>
            <artifactId>manifold-all</artifactId>
            <version>2025.1.5</version>
        </dependency>
        <dependency>
            <groupId>io.qtjambi</groupId>
            <artifactId>qtjambi</artifactId>
            <version>6.7.1</version>
        </dependency>
        <dependency>
            <groupId>io.qtjambi.uic</groupId>
            <artifactId>qtjambi</artifactId>
            <version>6.7.1</version>
            <scope>system</scope>
            <systemPath>D:/Codes/Qt/Qt-6.7.1/6.7.1/deployment/qtjambi-uic-6.7.1.jar</systemPath>
        </dependency>
        <dependency>
            <groupId>io.qtjambi.tools</groupId>
            <artifactId>qtjambi</artifactId>
            <version>6.7.1</version>
            <scope>system</scope>
            <systemPath>D:/Codes/Qt/Qt-6.7.1/6.7.1/deployment/qtjambi-uitools-6.7.1.jar</systemPath>
        </dependency>
        <dependency>
            <groupId>jna-jpms</groupId>
            <artifactId>jna</artifactId>
            <version>5.17.0</version>
            <scope>system</scope>
            <systemPath>D:/Codes/Qt/Qt-6.7.1/6.7.1/deployment/jna-jpms-5.17.0.jar</systemPath>
        </dependency>
        <dependency>
            <groupId>jna-platform-jpms</groupId>
            <artifactId>jna</artifactId>
            <version>5.17.0</version>
            <scope>system</scope>
            <systemPath>D:/Codes/Qt/Qt-6.7.1/6.7.1/deployment/jna-platform-jpms-5.17.0.jar</systemPath>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <!-- 配置编译器插件 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.11.0</version>
                <configuration>
                    <source>8</source>
                    <target>8</target>
                    <encoding>UTF-8</encoding>
                    <compilerArgs>
                        <arg>-Xplugin:Manifold</arg>
                    </compilerArgs>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

至于maven的配置,大家用AI帮忙写吧。我贴一份自己的maven配置。

<?xml version="1.0" encoding="UTF-8"?>

<!-- Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 -->

<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">

  <!-- 本地仓库路径 -->
  <localRepository>D:\Codes\Java\Repository</localRepository>

  <pluginGroups></pluginGroups>
  <proxies></proxies>

  <servers></servers>

  <!-- 镜像配置 -->
  <mirrors>
    <!-- 阿里云HTTPS镜像 -->
    <mirror>
      <id>aliyun-central</id>
      <mirrorOf>central</mirrorOf>
      <name>aliyun maven</name>
      <url>https://maven.aliyun.com/repository/central</url>
    </mirror>

    <!-- 阿里云全量镜像(建议启用) -->
    <mirror>
      <id>aliyun-all</id>
      <mirrorOf>*</mirrorOf>
      <name>aliyun all repositories</name>
      <url>https://maven.aliyun.com/repository/public</url>
    </mirror>

    <!-- 中央仓库备用镜像 -->
    <mirror>
      <id>repo1</id>
      <mirrorOf>central</mirrorOf>
      <name>Central Repository</name>
      <url>https://repo1.maven.org/maven2</url>
    </mirror>

    <!-- JBoss仓库 -->
    <mirror>
      <id>jboss-public</id>
      <mirrorOf>central</mirrorOf>
      <name>JBoss Public Repository</name>
      <url>https://repository.jboss.org/nexus/content/groups/public</url>
    </mirror>

    <!-- 注释掉可能冲突的镜像配置 -->
    <!-- 
    <mirror>
      <id>repo2</id>
      <mirrorOf>central</mirrorOf>
      <name>Central Repository 2</name>
      <url>http://repo2.maven.org/maven2/</url>
    </mirror>
    -->
  </mirrors>

  <!-- 全局配置 -->
  <profiles>
    <!-- JDK 编译配置 -->
    <profile>
      <id>jdk-1.8</id>
      <activation>
        <activeByDefault>true</activeByDefault>
        <jdk>8</jdk>
      </activation>
      <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
        <maven.compiler.release>8</maven.compiler.release>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
      </properties>
    </profile>

    <!-- 阿里云仓库配置 -->
    <profile>
      <id>aliyun</id>
      <repositories>
        <repository>
          <id>aliyun-public</id>
          <url>https://maven.aliyun.com/repository/public</url>
          <releases><enabled>true</enabled></releases>
          <snapshots><enabled>true</enabled></snapshots>
        </repository>
      </repositories>
      <pluginRepositories>
        <pluginRepository>
          <id>aliyun-public</id>
          <url>https://maven.aliyun.com/repository/public</url>
          <releases><enabled>true</enabled></releases>
          <snapshots><enabled>true</enabled></snapshots>
        </pluginRepository>
      </pluginRepositories>
    </profile>
  </profiles>

  <!-- 激活的Profile -->
  <activeProfiles>
    <activeProfile>jdk-1.8</activeProfile>
    <activeProfile>aliyun</activeProfile>
  </activeProfiles>
</settings>

至此,我们可以开始写第一个Java QT程序了。

package one;

import io.qt.widgets.QApplication;
import io.qt.widgets.QPushButton;

public class Main {
    public static void main(String[] args) {
        QApplication.initialize(args);
        QPushButton button = new QPushButton("Quit");
        button.clicked.connect(QApplication::quit);
        button.show();
        QApplication.exec();
        QApplication.shutdown();
    }
}

 

运行后如下

 

posted on 2025-04-02 13:22  dalgleish  阅读(39)  评论(0)    收藏  举报