"Qt Learning Notes" 2. Form Widgets && screen coordinates && layout

Source: Internet
Author: User

One, form

In Qt, the form (the mouth) is called the widget.

The widget can be either the main form (the port) or a variety of controls attached to the main form (the port), as a subform, both of which are called Top-level windows (top-level widgets) and child windows (sub widgets).


Top-level window: a standard window with a border, a title bar, and several buttons. Independent

child window: A window inside a window, such as a button, text box, and other controls. (not independent, as the parent window moves)


Attention:

1. Each child window has a parent window

2, sub-window may contain a number of sub-windows, is a layer of relationship.

3, the top-level window also has a parent window: The desktop Widget, the desktop itself is a window



Second, screen coordinates

Describes a window that has two properties:

1. Position: The coordinates of the upper left corner of the window x, y

2, Size: width, height (units: pixels)

For example, my display resolution is (1600 * 900), then the upper left corner (top-left corner) coordinates are (0, 0), the lower right corner (bottom-right corner) coordinates are (1599,899) Also note, here said "upper left Corner" " The lower right corner is the active area of the window. Does not include the above title bar.


Iii. relative coordinates of the Subwindow

The position of a child window in the parent window.


Iv. Defining a Window

Goal: Implement a window interface to add a button control to it.


Method:

1. Derivative Qwidget

2. Create Qbutton object, add to parent window

3, set the size and position of the button


Steps:

1, write a main window: This is a class inherited from Qwidget (can be handwritten, but in VS, directly in the solution manager right-click, add QT class, more convenient)

MyWin.h:

#pragma once#include <QWidget> #include <qpushbutton>class mywin:public Qwidget{q_objectpublic:mywin ( Qwidget *parent); Constructor, which represents the parent window of the current window, or null to indicate the main window ~mywin ();};

MyWin.cpp

#include "MyWin.h" Mywin::mywin (Qwidget *parent): Qwidget (parent) {}mywin::~mywin () {}


2. Write a button

The Mywin class is perfected as follows:

#pragma once#include <QWidget> #include <qpushbutton>class mywin:public Qwidget{q_objectpublic:mywin ( Qwidget *parent); ~mywin ();p rivate:qpushbutton* My_button;};

MyWin.cpp

#include "MyWin.h" Mywin::mywin (Qwidget *parent): Qwidget (parent) {My_button = new Qpushbutton (this); The button window's parent window is the object that this is pointing to, which is itself my_button->settext ("Change World"); My_button->setgeometry (100, 100, 200, 50);} Mywin::~mywin () {//delete here, but actually the child window control is managed by the parent window, so you do not have to show DeleteIf (My_button! = NULL) {delete My_button;}}

Main.cpp

#include "test2_3a_11_25.h" #include <QtWidgets/QApplication> #include "MyWin.h" int main (int argc, char *argv[]) { Qapplication A (argc, argv); Mywin Basewindow (NULL); Mywin Derivedwindow (&basewindow);    Basewindow.show (); return a.exec ();}


Note When adding child controls:

1. When you create a child object, you must specify the parent window (as a parameter to the constructor)

2. Destructors: Child window controls are managed by the parent window, so you do not have to display the delete


Five, more controls

Qlineedit: Single-line text box

Qplaintextedit: normal multi-line text box

Updated more child windows, now the code is as follows:

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M00/8A/AB/wKioL1g3GbWQBvirAAB-gc7eGzI320.png "style=" float: none; "title=" 8.png "alt=" Wkiol1g3gbwqbviraab-gc7egzi320.png "/>

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M00/8A/AF/wKiom1g3GbeTL9HTAAD9ld03I0k371.png "style=" float: none; "title=" 9.png "alt=" Wkiom1g3gbetl9htaad9ld03i0k371.png "/>

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M01/8A/AF/wKiom1g3GbjR3A-QAACad9PGlJY764.png "style=" float: none; "title=" 10.png "alt=" Wkiom1g3gbjr3a-qaacad9pgljy764.png "/>

Perform:

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M01/8A/AF/wKiom1g3Gefw_UDEAAAVS9SSgXQ068.png "title=" 11.png "alt=" Wkiom1g3gefw_udeaaavs9ssgxq068.png "/>


Now there are a few very unsightly questions:

1, the layout is too difficult

2. When the parent window resizes, how do the child windows move along?

3. How do child windows fill the control?

These problems will be resolved next.



Tail:

I found that the name of the member object in Qt is the big hump method, so when defining your own object, try to change the naming style to avoid naming conflicts with objects in the library.

"Qt Learning Notes" 2. Form Widgets && screen coordinates && layout

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.