返回介绍

Layout management

发布于 2025-02-22 22:19:46 字数 11124 浏览 0 评论 0 收藏 0

In this part of the Qyoto C# programming tutorial, we will introduce layout managers.

When we design the GUI of our application, we decide what components we will use and how we will organize those components in the application. To organize our components, we use specialized non visible objects called layout managers. There are several options in Qyoto. We can use absolute positioning, built-in layout managers or create a custom layout manager. We can also visually build the layouts using the Qt Designer.

Qyoto has some important built-in layout managers. The QVBoxLayout class lines up widgets vertically. QHBoxLayout lines up widgets horizontally. The QGridLayout class lays out widgets in a grid. The grid layout is the most flexible layout manager. The box layouts can be nested into one another to create complex layouts.

Absolute positioning

In most cases, programmers should use layout managers. There are a few situations, where we can use absolute positioning. In absolute positioning, the programmer specifies the position and the size of each widget in pixels. The size and the position of a widget do not change if you resize a window. Applications look different on various platforms, and what looks OK on Linux, might not look OK on Mac. Changing fonts in your application might spoil the layout. If you translate your application into another language, you must redo your layout. For all these issues, use the absolute positioning only when you have a reason to do so.

using System;
using QtCore;
using QtGui;

/**
 * ZetCode Qyoto C# tutorial
 *
 * In this program, we lay out widgets
 * using absolute positioning
 *
 * @author Jan Bodnar
 * website zetcode.com
 * last modified October 2012
 */


public class QyotoApp : QWidget 
{
  public QyotoApp() 
  {
    WindowTitle = "Absolute";

    InitUI();

    Resize(300, 280);
    Move(300, 300);
    Show();
  }

  void InitUI() 
  {    
    StyleSheet = "QWidget { background-color: #414141 }";

    QPixmap bardejov = new QPixmap("bardejov.jpg");
    QPixmap rotunda = new QPixmap("rotunda.jpg");
    QPixmap mincol = new QPixmap("mincol.jpg");

    QLabel barLabel = new QLabel(this);
    barLabel.Pixmap = bardejov;
    barLabel.Move(20, 20);

    QLabel rotLabel = new QLabel(this);
    rotLabel.Pixmap = rotunda;
    rotLabel.Move(40, 160);

    QLabel minLabel = new QLabel(this);
    minLabel.Pixmap = mincol;
    minLabel.Move(170, 50);
  }

  [STAThread]
  public static int Main(String[] args) 
  {
    new QApplication(args);
    new QyotoApp();
    return QApplication.Exec();
  }
}

In this example, we show three images using the absolute positioning.

StyleSheet = "QWidget { background-color: #414141 }";

The StyleSheet property is used to change the background colour of the client area of the window.

QLabel barLabel = new QLabel(this);
barLabel.Pixmap = bardejov;

The QLabel widget is used to hold the image.

barLabel.Move(20, 20);

We use the Move() method to position the label on the window at x=20, y=20.

When we resize the window, the labels retain their initial size.

Absolute
Figure: Absolute positioning

Buttons example

In the following example, we will position two buttons in the bottom right corner of the window.

using System;
using QtCore;
using QtGui;

/**
 * ZetCode Qyoto C# tutorial
 *
 * In this program, use box layouts
 * to position two buttons in the
 * bottom right corner of the window.
 *
 * @author Jan Bodnar
 * website zetcode.com
 * last modified October 2012
 */


public class QyotoApp : QWidget 
{
  public QyotoApp() 
  {
    WindowTitle = "Buttons";

    InitUI();

    Resize(300, 150);
    Move(300, 300);
    Show();
  }

  void InitUI() 
  {  
    QVBoxLayout vbox = new QVBoxLayout(this);
    QHBoxLayout hbox = new QHBoxLayout();

    QPushButton ok = new QPushButton("OK", this);
    QPushButton apply = new QPushButton("Apply", this);

    hbox.AddWidget(ok, 1, AlignmentFlag.AlignRight);
    hbox.AddWidget(apply);

    vbox.AddStretch(1);
    vbox.AddLayout(hbox);
  }

  [STAThread]
  public static int Main(String[] args) 
  {
    new QApplication(args);
    new QyotoApp();
    return QApplication.Exec();
  }
}

We use nested box layouts to get our intended layout.

QVBoxLayout vbox = new QVBoxLayout(this);
QHBoxLayout hbox = new QHBoxLayout();

We use one vertical and one horizontal box.

QPushButton ok = new QPushButton("OK", this);
QPushButton apply = new QPushButton("Apply", this);

These are the two buttons that will go into the bottom right corner of the window.

hbox.AddWidget(ok, 1, AlignmentFlag.AlignRight);

We put the ok button into the horizontal box. The second parameter is the stretch factor. It expands the area allotted to the ok button. It takes all available space left. Inside this area, the button is aligned to the right.

vbox.AddStretch(1);

This line creates a vertically expanded white space, which will push the horizontal box with the buttons to the bottom.

vbox.AddLayout(hbox);

The horizontal box is nested into the vertical box.

Buttons example
Figure: Buttons example

Windows example

The following is a more complicated example with nested box layouts.

using System;
using QtCore;
using QtGui;

/**
 * ZetCode Qyoto C# tutorial
 *
 * In this program, use box layouts
 * to create a windows example.
 *
 * @author Jan Bodnar
 * website zetcode.com
 * last modified October 2012
 */


public class QyotoApp : QWidget 
{
  public QyotoApp() 
  {
    WindowTitle = "Windows";

    InitUI();

    Resize(350, 300);
    Move(300, 300);
    Show();
  }

  void InitUI() 
  {
    QVBoxLayout vbox = new QVBoxLayout(this);

    QVBoxLayout vbox1 = new QVBoxLayout();
    QHBoxLayout hbox1 = new QHBoxLayout();
    QHBoxLayout hbox2 = new QHBoxLayout();

    QLabel windLabel = new QLabel("Windows", this);
    QTextEdit edit = new QTextEdit(this);
    edit.Enabled = false;

    QPushButton activate = new QPushButton("Activate", this);
    QPushButton close = new QPushButton("Close", this);
    QPushButton help = new QPushButton("Help", this);
    QPushButton ok = new QPushButton("OK", this);

    vbox.AddWidget(windLabel);

    vbox1.AddWidget(activate);
    vbox1.AddWidget(close, 0, AlignmentFlag.AlignTop);
    hbox1.AddWidget(edit);
    hbox1.AddLayout(vbox1);

    vbox.AddLayout(hbox1);

    hbox2.AddWidget(help);
    hbox2.AddStretch(1);
    hbox2.AddWidget(ok);
    
    vbox.AddLayout(hbox2, 1);

    Layout = vbox;
  }

  [STAThread]
  public static int Main(String[] args) 
  {
    new QApplication(args);
    new QyotoApp();
    return QApplication.Exec();
  }
}

In this layout, we use two vertical and horizontal boxes.

QVBoxLayout vbox = new QVBoxLayout(this);

This is the base layout of the example.

vbox.AddWidget(windLabel);

First goes the label widget. It goes simply to the top of the vertical box.

vbox1.AddWidget(activate);
vbox1.AddWidget(close, 0, AlignmentFlag.AlignTop);
hbox1.AddWidget(edit);
hbox1.AddLayout(vbox1);

vbox.AddLayout(hbox1);

In the center part of the window we have a text edit widget and two vertically lined up buttons. The buttons go into a vertical box. The buttons are aligned to the top within this vertical box. The vertical box and the text edit go into a horizontal box. This horizontal box goes to the base vertical box, just below the label widget.

hbox2.AddWidget(help);
hbox2.AddStretch(1);
hbox2.AddWidget(ok);

vbox.AddLayout(hbox2, 1);

The help and the ok buttons go into another horizontal box. There is an expanded white space between these two buttons. Again, the horizontal box goes to the base vertical box.

Layout = vbox;

The base vertical box is set to be the main layout of the window.

Windows example
Figure: Windows example

New Folder example

In the last example, we use the QGridLayout manager to create a New Folder layout example.

using System;
using QtCore;
using QtGui;

/**
 * ZetCode Qyoto C# tutorial
 *
 * In this program, use the QGridLayout manager
 * to create a New Folder example.
 *
 * @author Jan Bodnar
 * website zetcode.com
 * last modified October 2012
 */


public class QyotoApp : QWidget 
{
  public QyotoApp() 
  {
    WindowTitle = "New Folder";

    InitUI();

    Resize(300, 300);
    Move(300, 300);
    Show();
  }

  void InitUI() 
  {
    QGridLayout grid = new QGridLayout(this);

    QLabel nameLabel = new QLabel("Name", this);
    QLineEdit nameEdit = new QLineEdit(this);
    QTextEdit text = new QTextEdit(this);
    QPushButton okButton = new QPushButton("OK", this);
    QPushButton closeButton = new QPushButton("Close", this);

    grid.AddWidget(nameLabel, 0, 0);
    grid.AddWidget(nameEdit, 0, 1, 1, 3);
    grid.AddWidget(text, 1, 0, 2, 4);
    grid.SetColumnStretch(1, 1);
    grid.AddWidget(okButton, 4, 2);
    grid.AddWidget(closeButton, 4, 3);
  }

  [STAThread]
  public static int Main(String[] args) 
  {
    new QApplication(args);
    new QyotoApp();
    return QApplication.Exec();
  }
}

In our example, we have one label, one line edit, one text edit and two buttons.

QGridLayout grid = new QGridLayout(this);

We create an instance of the QGridLayout manager.

grid.AddWidget(nameLabel, 0, 0);

We place the label widget in the first cell of the grid. The cells count from 0. The last two parameters are the row and column number.

grid.AddWidget(nameEdit, 0, 1, 1, 3);

The line edit widget is placed at the first row, second column. The last two parameters are the row span and the column span. Horizontally, the widget will span three columns.

grid.SetColumnStretch(1, 1);

The parameters of the method are the column number and the stretch factor. Here we set stretch factor 1 to the second column. This means that this column will take all remaining space. This was set, because we wanted our buttons to retain their initial size.

New Folder example
Figure: New Folder example

In this part of the Qyoto C# tutorial, we have mentioned layout management of widgets.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文