swing VS QT
2015-10-16 22:21
423 查看
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
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 JButtonActionListener 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 thisclass ( 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 } |
相关文章推荐
- 将某个Qt4项目升级到Qt5遇到的问题(13条方法)
- QTP安装注意事项
- Qt使用MinGW编译,如何忽略警告
- Qt自定义sleep延时函数(巧妙的使用时间差,但这样似乎CPU满格,而不是沉睡)
- Windows7下移植Qt4.8.4项目到QT5.2上时遇到的一些问题(包括三篇参考文章)
- Qt下使用的静态链接库------ *.lib转化为mingw使用的.a格式的静态库
- QT中QMainwindow 类与 QWidget类使用 VS2012 QT5.5
- Qt多国语言QT_TR_NOOP和QT_TRANSLATE_NOOP
- Qt简介
- Qt跨线程信号和槽的连接(默认方式是直连和队列的折中)
- 使用Qt实现MDI风格的主窗体
- Qt中文乱码问题(比较清楚,同一个二进制串被解释成不同的语言)
- qmake使用实践:包含动态库的Qt4工程
- 如何修改Qt标准对话框的文字(例如,英文改成中文)
- qt-vs-addin:Qt4和Qt5之VS插件如何共存与使用
- Qt在Windows下的三种编程环境搭建
- Qt在Mac OS X下的编程环境搭建
- 给Qt生成的exe执行程序添加版本信息
- 基于QT开发的第三方库
- 解决Qt中文乱码以及汉字编码的问题(UTF-8/GBK)——ubuntu环境设置默认是utf-8,文件编码可使用Encodersoft批量转换