zoukankan      html  css  js  c++  java
  • swing VS QT

    Qt vs. Swing

     QtSwing
    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 - JavaQt - 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.

  • 相关阅读:
    向存储过程中传入列表参数
    TRUNCATE TABLE (Transact-SQL)
    Program.cs
    联想G40-30 安装win10系统
    完美解决github访问速度慢[转]
    关于Visual studio 2017安装方法的若干问题
    [转载] 中国象棋软件-引擎实现(七)测试程序
    [转载] 中国象棋软件-引擎实现(六)局面评估
    [转载] 中国象棋软件-引擎实现(五)历史启发及着法排序
    [转载] 中国象棋软件-引擎实现(四)搜索算法
  • 原文地址:https://www.cnblogs.com/alleyonline/p/4886551.html
Copyright © 2011-2022 走看看