6-3 separated controls (splitters)

Source: Internet
Author: User
Tags qt designer

Splitter handles)

A qsplitter control can contain other controls. These controls are separated by a separator bar, and the size of the control can be changed. The qsplitter control is often used as the layout manager to provide more interface control.

The child controls in the qsplitter control are automatically arranged side by side (or up or down) in order ). There is a separator between adjacent controls. The following code creates the form in Figure 6.7: int main (INT argc, char * argv []) {qapplication app (argc, argv); qtextedit * editor1 = new qtextedit; qtextedit * editor2 = new qtextedit; qtextedit * editor3 = new qtextedit; qsplitter (QT: horizontal); splitter. addwidget (editor1); splitter. addwidget (editor2); splitter. addwidget (editor3 );... splitter. show (); Return app.exe C ();} Figure 6.7. The splitter Application

In this example, the form has a qsplitter control, which has three horizontally arranged qtextedit controls. Unlike the layout manager, qsplitter can not only arrange child controls, but also have a visual appearance, the qsplitter control inherits from the qwidget and has all the functions of the qwidget.

Figure 6.8. The splitter application's Widgets

 

The first is to call setstretchfactor of rightsplitter, set the tensile coefficient of the control (textedit) with an index value of 1 to 1, and the second is to call setstretchfactor () of mainsplitter (), set the tensile coefficient of the control rightsplitter to 1. In this way, the textedit control can obtain as much space as possible. When the application starts, qsplitter rationally allocates the size of each sub-control based on the initial size or sizehint of the sub-control. In the program, we can call qsplitter: setsizes () to change the position of the separator bar. Qsplitter can also save the current state and restore the previous state at the next running of the program. The writesettings () function is used to save the status of the current separator entry: void mailclient: writesettings () {qsettings settings ("Software Inc. "," mail client "); settings. begingroup ("mainwindow"); settings. setvalue ("size", size (); settings. setvalue ("mainsplitter", mainsplitter-> savestate (); settings. setvalue ("rightsplitter", rightsplitter-> savestate (); settings. endgroup () ;}the corresponding readsettings () function: void mailclient: readsettings () {qsettings Settings ("Software Inc. "," mail client "); settings. begingroup ("mainwindow"); resize (settings. value ("size", qsize (480,360 )). tosize (); mainsplitter-> restorestate (settings. value ("mainsplitter "). tobytearray (); rightsplitter-> restorestate (settings. value ("rightsplitter "). tobytearray (); settings. endgroup ();} QT designer also supports qsplitter. Place the child control in a proper position, select them, and select the menu form | lay out horizontally in Splitter or form | lay out verticallly in splitter, the selected child control is added to the qsplitter control.

Horizontal or vertical nesting of qsplitter can achieve more complex layout. For example, in the mailclient program shown in Figure 6-9, a vertical qsplitter control is nested with a horizontal qsplitter control.Figure 6.9. The mail client application on Mac OS XThe following code is the main window class constructor code of the mailclient program: mailclient (){... rightsplitter = new qsplitter (QT: vertical); rightsplitter-> addwidget (messagestreewidget); rightsplitter-> addwidget (textedit); rightsplitter-> setstretchfactor (1, 1 ); mainsplitter = new qsplitter (QT: horizontal); mainsplitter-> addwidget (folderstreewidget); mainsplitter-> addwidget (rightsplitter); mainsplitter-> setstretchfactor (1, 1); setcent Ralwidget (mainsplitter); setwindowtitle (TR ("mail client"); readsettings ();} after creating three widgets (messagetreewidget, textedit, folderstreewidget, we create a vertical qsplitter and rightsplitter control and add the messagetreewidget and textedit controls to rightsplitter. Create a horizontal qsplitter and mainsplitter control to add rightsplitter and folderstreewidget to mainsplitter. Use mainsplitter as the central control of qmainwindow. When the user changes the window size, qsplitter usually gives the same space to all child controls. In the mailclient program, we want the folderstreewidget and messagetreewidget on the left to keep their sizes and allocate other spaces to qtextedit. This is implemented by two setstretchfactor () calls. The first parameter is the index value of the Child control starting from 0, and the second parameter is the tensile coefficient we set. The default value is 0.Figure 6.10. The mail client's splitter Indexing

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.