QT Basics (i)----buttons, text boxes, windows, layouts, options boxes, etc.

Source: Internet
Author: User

The explanation for each statement is placed in the function comment!

A preliminary study of QT Program, window and button creation and correlation

Application abstract Class #include<qapplication>//window class #include<qwidget>//button class #include<qpushbutton>int main (int ARGC, Char *argv[]) {    //Initialize application    qapplication app (argc, argv);    Constructs a window w    qwidget W;    Set window name    w.setwindowtitle ("hello,world!");    Constructs a button, actually the button is also a button    Qpushbutton;    Sets the text    button.settext ("button") on the button;    Put the button inside the window, create a parent-child relationship//Window object with the Window object to    affect the display position, do not establish such a parent-child relationship, there is no mutual pipe, separate    //No parent window window becomes the main window    Button.setparent (&w);    Why add the following sentence after the window becomes very small, only enough to hold a button    //button.show ();    When the button is clicked, the window is closed    //qt the extension of C + +    //The object of the first parameter is the Qobject subclass, the preceding two parameters are an object, the next two parameters are Chao    //std::bind Std::function, bind two functions together    qobject::connect (&button, SIGNAL (clicked ()), &w, SLOT (Close ()));    Display window    w.show ();    There is a message loop in Exec, the dead loop    return App.exec ();}
Operation Result:

Note: don't forget to add the following code to the pro file:

QT + = Widgets GUI

2 When the line input box and its input message prompt function and its input password when the exception input function
#include <QApplication> #include <qwidget>//input box header file #include<qlineedit>//prompt message #include< qcompleter>//#include <qdebug>int main (int argc, char *argv[]) {    qapplication app (argc, argv);    Qwidget W;    W.setwindowtitle ("hello,world!");    Add an input box    qlineedit edit;    Edit.show ();    Edit.setparent (&w);    Enter the password, yes text is not visible    //edit.setechomode (qlineedit::P assword);    Gets the input result of the password   //edit.text ();    Enter    the prompt edit.setplaceholdertext ("Please enter password:");    Message list    Qcompleter completer (qstringlist () << "Apple" << "Lin" << "Qian" << "Qin");    Set completer matching mode    completer.setfiltermode (qt::matchcontains);    Input information automatically prompt input    edit.setcompleter (&completer);    W.show ();    return app.exec ();}
Run Result: There is an input box in the interface.
3 Setting the position and size of the button
#include <QApplication> #include <qwidget>//input box header file #include<qlineedit>//prompt message #include< qcompleter>//#include <qdebug>int main (int argc, char *argv[]) {    qapplication app (argc, argv);    Qwidget W;    W.setwindowtitle ("hello,world!");    Add an input box    qlineedit edit;    Edit.show ();    Edit.setparent (&w);    Enter the password, yes text is not visible    //edit.setechomode (qlineedit::P assword);    Gets the input result of the password   //edit.text ();    Enter    the prompt edit.setplaceholdertext ("Please enter password:");    Message list    Qcompleter completer (qstringlist () << "Apple" << "Lin" << "Qian" << "Qin");    Set completer matching mode    completer.setfiltermode (qt::matchcontains);    Input information automatically prompt input    edit.setcompleter (&completer);    W.show ();    return app.exec ();} <strong></strong>

Run result: no longer as the first program in the top left of the main interface

4 Layout Management
#include <QApplication> #include <QWidget> #include <QPushButton> #include <qlineedit>//window layout, Vertical Change #include<qvboxlayout>//window layout, horizontal change #include<qhboxlayout>//lattice window #include<qgridlayout>//hint Text # include<qlabel>//#define HBOX//#define GBOX//#define LABINT Main (int argc, char *argv[]) {qapplication app (argc,    ARGV);    Qwidget W;    W.setwindowtitle ("Positon");    Qpushbutton button;    Button.settext ("UserName:");    Qpushbutton button2;    Button2.settext ("Password:");//Button.setparent (&AMP;W);    Qobject::connect (&button, SIGNAL (clicked ()), &w, SLOT (Close ()));    W.show ();    Qlineedit edit; Qlineedit *password; Edit.setparent (&w); #ifdef Hbox//layout is not a window, it is an object that actively creates a parent-child relationship with the main window//layout automatically arranges windows in the System window and changes as the window changes//Note    With layout, the SetParent function of the button and edit box can comment out the qhboxlayout layout;    On the right there is a spring layout.addstretch (1);    Layout inside add two windows, while adding the words push the button to the middle Layout.addwidget (&button); There is a slight distance between the previous button and the Next button, such as 50 pixels LAYout.addspacing (50);    Layout.addwidget (&edit);    Add a spring, the editor window does not change with the main window//the Left has a spring layout.addstretch (1); #endif #ifdef gbox qgridlayout layout;    Add button buttons to row No. 0, column No. 0 layout.addwidget (&button, 0, 0);    Layout.addwidget (&edit, 0, 1);    Layout.addwidget (New Qpushbutton ("10"), 1, 0);    Layout.addwidget (New Qpushbutton ("11"), 1, 1);    Add spring put the object on the upper left corner//Add the 2nd column of Spring Layout.setcolumnstretch (2, 1);    Add the 2nd row of Spring Layout.setrowstretch (2, 1); #endif qgridlayout layout;    Layout.setrowstretch (0,1);    Layout.setcolumnstretch (3,1);    Layout.addwidget (&button, 1, 1);    Layout.addwidget (&edit,1,2);    Layout.addwidget (&button2, 2, 1);    Layout.addwidget (password = new Qlineedit (), 2, 2);    Password->setechomode (qlineedit::P assword);    Layout is a qhboxlayout *hbox that can be nested;    Layout.addlayout (Hbox = new Qhboxlayout, 3, 2);    Hbox->addstretch (1);    Hbox->addwidget (New Qpushbutton ("Login")); Layout.addwidget (New Qpushbutton ("login"), 3, 2);    Layout.setrowstretch (4,1);    Layout.setcolumnstretch (0,1);    W.show ();    Add Layout w.setlayout (&layout) to the entire system window; App.exec ();}
Operation Result:


5 qt button, Slide box option, sub-text item and so on first create a class that adds qt + = Widgets GUI [Enter] CONFIG + = c++11 header file in Pro file Mywidget.h
#ifndef mywidget_h#define mywidget_h#include <QWidget> #include <qtextedit>class mywidget:public qwidget{    q_objectpublic:    explicit Mywidget (Qwidget *parent = 0);    qtextedit* myedit;signals:public Slots:}; #endif//Mywidget_h

Source file Mywidget.cpp
#include "mywidget.h" #include <QApplication> #include <QLabel>//display a single line of text or pictures #include<qpushbutton># Include<qlineedit>//single-line Input box #include<qcombobox>//Slide box option #include<qcheckbox>//Can tick the box #include&lt ; qradiobutton>//Radio box #include<qtextedit>//Multi-line text box, sub-text #include<qtextbrowser>//read-only text #include< qgroupbox>//Small box, containing content, used to classify the #include<qslider>//analog display value #include<qspinbox>//digital, through the button can be added to reduce the #inclu de<qdateedit> #include <QDateTimeEdit> #include <QTimeEdit> #include <QTabWidget>//Do not count controls # include<qvboxlayout> #include <QCompleter> #include <QHBoxLayout> #include <QDebug> #include <QPixmap>//Add image #include<qlcdnumber>//digital Mywidget::mywidget (Qwidget *parent) to display the LCD style: Qwidget (pare    NT) {qvboxlayout *layout = new Qvboxlayout (this);          Qlabel *label;       Display a single line of text or pictures Qcombobox *combo;    Slide Box Qpushbutton *button;    Qradiobutton *radio; QcompLeter *completer;    Qtextedit *edit;    Qgroupbox *groupbox; The label supports HTML format layout->addwidget (label = new Qlabel ("<font color = red>Operation Result:




QT Basics (i)----buttons, text boxes, windows, layouts, options boxes, etc.

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.