Friday, September 25, 2015

QtJambi Tutorial 2: Hello World

Now that we have the environment setup, let the fun begin.

Creating a Java Project

  1. Create a new Java Project File -> New -> Project -> Java -> Java Project


  2. Give the project a suitable name
  3. Click Finish

  4. Create a new package. Right click the Project -> New -> Package
  5. Give the package a suitable name : <package_name>
  6. Click Finish


  7. Right click the created Package -> New -> Class
  8. Give the class a suitable Name : <class_name>
  9. Click Finish

Add Qt libraries to the project

Inorder for Eclipse to recognize the qt library, we have to add the Qt Libraries.
  1. Right click package -> Build Path -> Configure Build Path …
  2. Click Library -> Add External Jar
  3. Browse to C:\qtjambi-4.8.6 and select qtjambi-4.8.6.jar file
  4. Select the added jar file. Open the drop down list -> Native library location -> Edit
  5. Click External Folder and Browse to C:\qtjambi-4.8.6\lib
  6. Click Ok
  7. The package should now find the references
  8. Make sure the class name matches with the .java file

Run the Jambi class

Based on how you have set <project_name> and <class_name>, the Jambi class would look something similar to the template below:
package <package_name>;

import com.trolltech.qt.gui.*;

public class <class_name> 
{

    public static void main(String[] args) 
    {
        // TODO code application logic here
        QApplication.initialize(args);
        QMainWindow window = new QMainWindow();
        Ui_<ui_class_name> ui = new Ui_<ui_class_name>();
        ui.setupUi(window);
        window.show();
        QApplication.execStatic();
     }
}

class_name.java

Below is a working example code.

QtJambiSimpleCls.java
package QtJambiSimplePck;

import com.trolltech.qt.gui.QApplication;
import com.trolltech.qt.gui.QPushButton;
import com.trolltech.qt.gui.QWidget;

public class QtJambiSimpleCls extends QWidget
{

    public QtJambiSimpleCls() 
    {

        setWindowTitle("Quit button");

        initUI();

        resize(250, 150);
        move(300, 300);
        
        show();
    }

    private void initUI() 
    {

        QPushButton quit = new QPushButton("Quit", this);
        quit.setGeometry(30, 30, 75, 30);

        quit.clicked.connect(this, "close()");
    }
    
    public static void main(String args[])
    {
        QApplication.initialize(args);
        new QtJambiSimpleCls();
        QApplication.execStatic();
    }
}

  1. Right click the <class_name> -> Run As -> Java Application
  2. A Qt Application should launch as shown



Congratulations, you have successfully written the first QtJambi Application.


No comments:

Post a Comment