QT (8) Layout Change Dynamic Layout

Source: Internet
Author: User

This time, we will learn the following questions:

  1. Dynamic Change Layout
  2. Qqueue usage
  3. Linkage between components

Reference Source: Workshop.

  1. A dialog is divided into three parts;
  2. The first part is a groupbox, which is in the gridlayout () mode. Press the rotate widget button to rotate the component Position clockwise. These four components can be linked. One modification and the other three display values can also be modified. These four components are stored in qqueue.
  3. The second part is also a groupbox. Based on the choice of ComboBox, determine the position of the third part.
  4. The third part is dialogbuttonbox, which has two dialog buttons, close and help, and adds a rotation button "rotate widget"

 

1. Create a program subject using the dialog method. Before that, we discussed how to create a dialog subject, which is no longer repeated. The mydialog class inherits the qdialog class.

2. Rotate the four components in the first part

In mydialog. h:
PRIVATE:
/* Components in the first part */
Qgroupbox * rotablegroupbox;
Qqueue <qwidget *> rotablewidgets;
Qgridlayout * rotablelayout;
In mydialog. cpp:
Void mydialog: createrotablegroupbox ()
{
Rotablegroupbox = new qgroupbox (TR ("rotable widgets "));

Rotablewidgets. enqueue (New qspinbox );
Rotablewidgets. enqueue (New qslider );
RotableWidgets. enqueue (new QDial );
RotableWidgets. enqueue (new QProgressBar );
Int n = rotableWidgets. count ();
/* Set linkage here. Note that this is usually used for the third parameter in the past to call the functions of the current class. For association, we need to call the functions of other components, so this is another component, the first action leads to the second action, the second action, the third action, and all changes. Otherwise, the change will continue */

For (int I = 0; I <n; ++ I ){
Connect(RotableWidgets[I], SIGNAL (ValueChanged(Int )),
RotableWidgets[(I + 1) % n], SLOT (SetValue(Int )));
}

RotableLayout = new QGridLayout;
RotableGroupBox-> setLayout (rotableLayout );
RotateWidgets ();
}

Void MyDialog: rotateWidgets () // This is also a slot function
{
// Delete the component on the panel
Foreach (QWidget * widget, rotableWidgets)
RotableLayout->RemoveWidget(Widget );
// Delete the item of the head and add it to the end, so that we can rotate it.
RotableWidgets.Enqueue(RotableWidgets.Dequeue());

RotableLayout-> addWidget (rotableWidgets [0], 1, 0 );
RotableLayout-> addWidget (rotableWidgets [1], 1, 1 );
RotableLayout-> addWidget (rotableWidgets [2], 0, 1 );
RotableLayout-> addWidget (rotableWidgets [3], 0, 0 );
}

3. Horizontal/vertical mode. Since the iPhone introduced a weight sensor, horizontal and vertical layout of a program is usually the same, but some programs also provide different la S. Here is an example.

In mydialog. h:
Private:
/* Component in the second part */
QGroupBox * optionsGroupBox;
QGridLayout * optionsLayout;
QLabel * buttonsOrientationLabel;
QComboBox * buttonsOrientationComboBox;
/* Components in the third part */
QDialogButtonBox * buttonBox;
QPushButton * closeButton;
QPushButton * helpButton;
QPushButton * rotateWidgetsButton;
In mydialog. cpp:
Void MyDialog: buttonsOrientationChanged (int index)
{
// Because we change the layout, the size of the new layout may be smaller than the original size, but the widget's resize size is smaller than the original size. Use setMinmumSize (0, 0) to reset. To put it simply, if we want to set a new size for every layout modification, the minimum reset size will be used.

SetMinimumSize (0, 0 );

// There are two parameters in the direction: Qt: Horizontal 0x1, Qt: Vertical 0x2. Here 0 is Horizontal, and 1 is vertical.

Qt: OrientationOrientation = Qt: Orientation (index + 1 );
If (buttonBox-> orientation () = orientation) // The direction is not changed.

Return;

MainLayout-> removeWidget (buttonBox); // Delete the original buttonBox, and add

Int spacing = mainLayout-> spacing ();
QSize oldSizeHint = buttonBox-> sizeHint () + QSize (spacing, spacing );
ButtonBox-> setOrientation (orientation );
QSize newSizeHint = buttonBox-> sizeHint () + QSize (spacing, spacing );

If (orientation = Qt: Horizontal ){
MainLayout-> addWidget (buttonBox, 2, 0); // new location, placed at the bottom

Resize (size () + QSize (-OldSizeHint. width (), NewSizeHint. height ()));
// Set the proper size
} Else {
MainLayout-> addWidget (buttonBox,); // a new position, which is placed on the rightmost side.

Resize (size () + QSize (newSizeHint. width (),-OldSizeHint. height ()); // Set the appropriate size

// Resize (minimumSize (). width (), minimumSize (). height (); in this way, we can get the most compact arrangement, but we want other parts to remain unchanged, so we can typeset Based on the computation.

}
}

4. For the second part: the second part only has two components, which occupy a small space. When we pull the dialog box, we hope that both components will be placed on the leftmost side, fill in blank space on the right side. As follows, we add a blank class in gruilayout.

// Set the extension here. We hope that the following blank space will fill up the remaining space. Therefore, setting the 2nd column has a higher extension factor, if we want the 1st-column combobox to fill up the remaining Russian space, setColumnStretch (), we can pull the regular dialog to see the effect.

OptionsLayout->SetColumnStretch(2, 1 );

5. The default button provided by dialog. Using qdialogbuttonbox * buttonbox, we add two system buttons, close and help, and a Custom button, as shown below:

CloseButton = buttonBox-> addButton (QDialogButtonBox: Close);
HelpButton = buttonBox-> addButton (QDialogButtonBox: Help);
RotateWidgetsButton = buttonBox-> addButton (Tr ("Rotate & Widget"), QDialogButtonBox: ActionRole);
Connect (rotateWidgetsButton, SIGNAL (clicked (), this, SLOT (rotateWidgets ()));
Connect (closeButton, SIGNAL (clicked (), this, SLOT (Close()));
// Close is a system that inherits the QDialog
Connect (helpButton, SIGNAL (clicked (), this, SLOT (myhelp ()));
// Myhelp is customized

Some ideas: Chinese primary and middle school students learn the most complex mathematical and Physical Sciences in the world, and our college graduates have such a low quality? This was a strange problem. In fact, let's look at primary and secondary education. Cramming education aims to teach knowledge. Instead of teaching how to learn knowledge, it is not how to enjoy learning. If it is just knowledge, you can check it online. How to learn knowledge is the learning ability of people, how to think independently, and how to innovate. Learn not to question, how to learn innovation?

Related Links: My meego/moblin articles

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.