WebKit-qt4 Application

Source: Internet
Author: User

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?
  1. # Ifndef hello_qt2
  2. # Define hello_qt2
  3.  
  4. # Include <qdialog>
  5. Class qlabel;
  6. Class qpushbutton;
  7. Class myevent: Public qdialog
  8. {
  9. Q_object
  10. Public:
  11. Myevent (qwidget * parent = 0 );
  12. Private slots:
  13. Void button1clicked ();
  14. PRIVATE:
  15. Qlabel * m_label;
  16. Qpushbutton * m_button;
  17. };
  18.  
  19. # 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?
  1. # Include <qtgui>
  2. # Include <qapplication>
  3. # Include "helloqt2.h"
  4. Myevent: myevent (qwidget * parent)
  5. : Qdialog (parent)
  6. {
  7. M_label = new qlabel (TR ("label 1 "));
  8. M_button = new qpushbutton (TR ("button1 "));
  9. Connect (m_button, signal (clicked (), this, slot (button1clicked ()));
  10. Qhboxlayout * layout = new qhboxlayout;
  11. Layout-> addwidget (m_button );
  12. Layout-> addwidget (m_label );
  13. Setlayout (layout );
  14. Setwindowtitle (TR ("myevent "));
  15. }
  16. Void myevent: button1clicked ()
  17. {
  18. M_label-> settext (TR ("good morning! "));
  19. }
  20. Int main (INT argc, char * argv [])
  21. {
  22. Qapplication app (argc, argv );
  23. Myevent dialog;
  24. Return dialog.exe C ();
  25. }

# 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?

  1. Qmake-qt4-Project
  2. Qmake-qt4
  3. Make
  4. ./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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.