Creating a Java Project
- Create a new Java Project File -> New -> Project -> Java -> Java Project
- Give the project a suitable name
- Click Finish
- Create a new package. Right click the Project -> New -> Package
- Give the package a suitable name : <package_name>
- Click Finish
- Right click the created Package -> New -> Class
- Give the class a suitable Name : <class_name>
- Click Finish
Add Qt libraries to the project
Inorder for Eclipse to recognize the qt library, we have to add the Qt Libraries.
- Right click package -> Build Path -> Configure Build Path …
- Click Library -> Add External Jar
- Browse to C:\qtjambi-4.8.6 and select qtjambi-4.8.6.jar file
- Select the added jar file. Open the drop down list -> Native library location -> Edit
- Click External Folder and Browse to C:\qtjambi-4.8.6\lib
- Click Ok
- The package should now find the references
- 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(); } }
- Right click the <class_name> -> Run As -> Java Application
- A Qt Application should launch as shown
Congratulations, you have successfully written the first QtJambi Application.
No comments:
Post a Comment