Qt vs. Swing
Qt | Swing | |
---|---|---|
Class name | Qxxxx |
Jxxxx |
Writing the "GUI" class | Assume the following class declaration GUI.h
#include <qmainwindow.h> class GUI : public QMainWindow { public: GUI (); }and the corresponding function definition in GUI.cc
GUI::GUI () { setWindowTitle ("Simple Qt Window"); } |
The following is written in GUI.java
import javax.swing.JFrame; public class GUI extends JFrame { public GUI () { setTitle ("Simple Swing Window"); } } |
The main() function |
#include <qapplication.h> int main (int argc, char* argv[]) { { QApplication myApp(argc, argv); GUI myGUI; myGUI.show(); return a.exec(); } |
public static void main (String[] args) { { GUI myGUI = new GUI(); myGUI.setVisible (true); } |
Layout Manager |
GUI::GUI (QWidget *parent, const char *name) { /* create a vertical box layout for this widget */ QVBoxLayout *p = new QVBoxLayout (); QPushButton *b1 = new QPushButton ("Go"); QPushButton *b2 = new QPushButton ("Go"); p->addWidget (b1); p->addWidget (b2); } |
public GUI () { JPanel p; p = new JPanel(); /* create a vertical box layout */ BoxLayout vert = new BoxLayout (contPane, BoxLayout.Y_AXIS); p.setLayout (vert); JButton b1 = new JButton ("Go"); JButton b2 = new JButton ("Stop"); p.add (b1); p.add (b2); } |
Menubar |
QMenuBar *mb = new QMenuBar (this); setMenuBar (mb); QMenu *file = new QMenu (mb); QAction *exit = new QAction(); exit.setText ("Exit"); file->addAction (exit); |
JMenubar mb = new JMenubar(); setJMenubar (mb); JMenu file = new JMenu ("File"); mb.add (file); JMenuItem exit = new JMenuItem ("Exit"); file.add (exit); |
Event Handling
Swing implements its event handling mechanism via its event and listener objects. Any user action to a GUI component may result in an event object generated and sent to its listener(s).
In Qt, a similar mechanism is implemented via signals and slots. Any user action to a GUI component (Qt widget) may result in a signal "generated" and sent to its associated slot(s). In short:
Swing events are analogous to Qt signals
Swing listeners are analogous to Qt slots
Swing listeners are analogous to Qt slots
However, Qt signal-slot mechanism is more flexible than Swing event-(event listener) mechanism since Qt allows us to define our own signals.
Swing - Java | Qt - C++ |
---|---|
Assume the following code segment in Java to handle mouse clicks on a JButton
ActionListener myHandler = new MyBtnHandler(); JButton go = new JButton ("Go"); go.addActionListener (myHandler);and the class MyBtnHandler: private class MyBtnHandler implements ActionListener { public void actionPerformed (ActionEvent a) { /* this method gets called when the go button is clicked */ } } |
In Qt, the code is more straight forward:
QPushButton *go = new QPushButton ("Go"); connect (go, SIGNAL(clicked()), this, SLOT(myAction()));Assuming myAction() is a function in this class (GUI.cc ):
void GUI::myAction() { /* this function gets called when the go button is clicked */ }In the header file ( GUI.h ) the function myAction() must be declared as a slot:
class GUI : public QWidget { // other stuff not shown public slots: void myAction(); // other stuff not shown } |
The Qt online documentation provides a more comprehensive reading materials on signals and slots.