Remember that when I learned VB, Delphi, and MFC, The first example is that there is a button and text tag in the window. When you click the button, the text tab displays a paragraph. How does this example work in QT?
In QT, qlabel and qpushbutton are used to connect them. Let's take a look at the header file helloqt2.h:
View plaincopy to clipboardprint?
- # Ifndef hello_qt2
- # Define hello_qt2
-
- # Include <qdialog>
- Class qlabel;
- Class qpushbutton;
- Class myevent: Public qdialog
- {
- Q_object
- Public:
- Myevent (qwidget * parent = 0 );
- Private slots:
- Void button1clicked ();
- PRIVATE:
- Qlabel * m_label;
- Qpushbutton * m_button;
- };
-
- # Endif
# Ifndef hello_qt2 # define hello_qt2 # include <qdialog> class qlabel; Class qpushbutton; Class myevent: Public qdialog {q_objectpublic: myevent (qwidget * parent = 0); Private slots: void button1clicked (); Private: qlabel * m_label; qpushbutton * m_button;}; # endif
Let's look at the implementation body -- helloqt2.cpp:
View plaincopy to clipboardprint?
- # Include <qtgui>
- # Include <qapplication>
- # Include "helloqt2.h"
- Myevent: myevent (qwidget * parent)
- : Qdialog (parent)
- {
- M_label = new qlabel (TR ("label 1 "));
- M_button = new qpushbutton (TR ("button1 "));
- Connect (m_button, signal (clicked (), this, slot (button1clicked ()));
- Qhboxlayout * layout = new qhboxlayout;
- Layout-> addwidget (m_button );
- Layout-> addwidget (m_label );
- Setlayout (layout );
- Setwindowtitle (TR ("myevent "));
- }
- Void myevent: button1clicked ()
- {
- M_label-> settext (TR ("good morning! "));
- }
- Int main (INT argc, char * argv [])
- {
- Qapplication app (argc, argv );
- Myevent dialog;
- Return dialog.exe C ();
- }
# Include <qtgui> # include <qapplication> # include "helloqt2.h" myevent: myevent (qwidget * parent): qdialog (parent) {m_label = new qlabel (TR ("label 1"); m_button = new qpushbutton (TR ("button1"); Connect (m_button, signal (clicked ()), this, slot (button1clicked (); qhboxlayout * layout = new qhboxlayout; Layout-> addwidget (m_button); Layout-> addwidget (m_label); setlayout (layout ); setwindowtitle (TR ("myevent");} voi D myevent: button1clicked () {m_label-> settext (TR ("good morning! ");} Int main (INT argc, char * argv []) {qapplication app (argc, argv); myevent dialog; return dialog.exe C ();}
The key is to pay attention to the 11th rows. Here, the Click Event of the button is associated with the button1clicked () member function of the current class. You can set the TAG content in the button1clicked () member function.
Others are easy to understand. In lines 8th and 9, a new tag and button are created. In Lines 13-16, a typographical object is added to the window and the tag and button are added. In row 17, set the window title to "myevent ".
Execute the following four commands in sequence for compilation and running.
View plaincopy to clipboardprint?
- Qmake-qt4-Project
- Qmake-qt4
- Make
- ./Helloqt2
Qmake-qt4: projectqmake-qt4make./helloqt2
The following window is displayed:
Click button1. The following figure is displayed:
Interestingly, you do not need to specify the button or tag location. QT will automatically typeset. You can continue to try more. For example, try to change row 13th to the following format: qvboxlayout * layout = new qvboxlayout;
Replace the front and back digits of lines 8th and 9th, and compile and run the command to view the result.