Mar 16 2009

Creating an Application Specific Console in QT Jambi

I am currently working on a project that required a console with a very specific purpose. It needed to communicate with a custom back-end protocol for use in a client/server environment. This sort of specific tutorial is not surprisingly unavailable online. The first step when setting up an application specific console is actually setting up the back-end. This is something that will be potentially done as a future tutorial. Anyways back to building the console window. The components included in the console window will be one QTextEdit, and one QLineEdit. This can be constructed in any way you like. The QTextEdit can be on the bottom and the QLineEdit on top or the other way around, that’s up to you.

Next, we will need to be adding a custom action listener to the QLineEdit so that when the enter key is pressed inside the QLineEdit the command will be sent to the server and then displayed in the QTextEdit window. As a future feature implementation, syntax highlighting could be implemented for an easier display inside the QTextEdit window. The custom action listener to add to the QLineEdit bar looks like this…

1
QLineEdit.returnPressed.connect(this, "NameOfActionListenerMethod()");

This will ensure that everytime the enter button is pressed, the string that the user entered will be able to be transfered to the server, or wherever else in the program you would like it to go. Now for the action listener method. The method should be declared as public with a return type of void.

1
2
3
4
5
public void terminalWindowEnterPressed()
    {
        terminalWindow.append(enterArea.text());
        enterArea.setText("Protocol Name > ");
    }

Then once you have the action listener built to just send the data to the window, you can do the same with sending it across a network.

All Together the code looks like this. All you have to do is copy/paste the code into your program and use it at will.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/**
     *
     */
    public void buildTerminal()
    {
        terminalFrame = new QFrame();
        terminalFrame.setWindowTitle("Window Title");
 
        terminalWindow = new QTextEdit();
        terminalWindow.setReadOnly(true);
 
        enterArea = new QLineEdit();
        enterArea.setText("Protocol Name > ");
        enterLabel = new QLabel("Enter Command");
 
        terminalFrame.setMinimumSize(600, 400);
 
        enterArea.returnPressed.connect(this, "terminalWindowEnterPressed()");
 
        QGridLayout layout = new QGridLayout();
        layout.addWidget(terminalWindow,0,0,10,10);
        layout.addWidget(enterArea,11,1,1,9);
        layout.addWidget(enterLabel, 11,0,1,1);
 
        terminalFrame.setLayout(layout);
        terminalFrame.show();
    }
 
    /**
     *
     */
    public void terminalWindowEnterPressed()
    {
        terminalWindow.append(enterArea.text());
        enterArea.setText("Protocol Name > ");
    }

Cheers


Feb 17 2009

Creating a Basic Window in Qt

This is a basic piece of code to get you started with the QT Jambi API. Ensure that you have the QT libraries installed before you try and run this code. IF you don’t have this then it won’t run properly and will throw an error. This displays a basic window. Nothing added to it just a basic QT Jambi Window.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import com.trolltech.qt.gui.*;
 
public class basicWindow
{
 private final int WINDOW_WIDTH = 500;
 private final int WINDOW_HEIGHT = 400;
 private QFrame frame;
 
 public basicWindow()
 {
  //create the frame
  frame = new QFrame();
 
  //create the size of the window
  frame.resize(WINDOW_WIDTH, WINDOW_HEIGHT);
 
  //create the layout for the window
  QGridLayout gridLayout = new QGridLayout();
 
  //add the layout to the main frame
  frame.setLayout(gridLayout);
  frame.show(); 
 }
 
 public static void main(String[] args)
 {
  //Used to initialize the window
  QApplication.initialize(args);
 
  //creates a new instance of the basicWindow
  new basicWindow();
 
  //runs the window as an application
  QApplication.exec();
 }
}

Cheers


Feb 17 2009

Swing vs QT Jambi

So I figured I should touch on this as I’ve been frustrated with this for a while at school. First of all I would like to say how frustrating I think java swing is…. I mean you change one thing in the layout and the entire GUI changes for some reason or another that I cannot explain…. So that is my first topic…

Java GridLayout Vs. QGridLayout

QGridLayout is coded in a way that I found extremely easy to use and manipulate. If you change one thing it doesn’t change the rest of the GUI and it keeps everything where you want it to be. You have the ability to set how big you want something to be. Then, based on the size of all the components in your display it displays the GUI in an appropriate manner. Overall the QGridLayout wins this battle.

Java Gui Components Vs. QT Components

The methods that swing uses to handle components causes an extreme amount of code that is not useful when creating a GUI. For instance, when creating a group of radio buttons, you must first create the buttons, then add the buttons to whichever group then add each individual button to a panel or something to make them appear in the GUI. This is crazy in my opinion and there is no reason that you cannot add a ButtonGroup to a panel. In QT you can add whatever component you like to a GUI. This radio button example is just that, one example and there are far too many to display here.

This is just some short thoughts on QT vs Swing, however If you try it out yourself then I’m sure you will find out which is better for yourself. Bye For Now,

Cheers