Grid 网格布局控件

Grid 定义一个内容修饰器,该修饰器可拉伸和缩放单个子元素以填满可用空间。

为什么先介绍这个控件呢,因为在程序中少不了对界面进行排版,所以我们先介绍这个控件。

<Window x:Class="MaterialStorage.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <!--在这里给网格定义了三行一列 第一行高20 第三行高20 其余的自适应-->
        <Grid.RowDefinitions>
            <RowDefinition Height="20"></RowDefinition>
            <RowDefinition Height="*"></RowDefinition>
            <RowDefinition Height="20"></RowDefinition>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" ></ColumnDefinition>
        </Grid.ColumnDefinitions>
        <Button Grid.Row="0">按钮1</Button>
        <!-- 这里需要手动为控件定义自己所在的格子,否则他们都会按默认值0来显示  Margin 四个值对应的是 左 上 右 下-->
        <Button Grid.Row="1" Grid.Column="0" Height="30" Width="60" VerticalAlignment="Center" HorizontalAlignment="Left" Margin="20,0,0,0">按钮2</Button>
        <Button Grid.Row="2">按钮3</Button>
    </Grid>
</Window>

 

转载于:https://www.cnblogs.com/zhuzhenyu/archive/2013/02/28/2937190.html

Qt的网格布局(QGridLayout)是一种方便的方式来排列控件。它将控件放置在一个二维的网格中,类似于表格布局。 下面是一个简单的示例,展示如何使用网格布局来排版控件: ```cpp #include <QtWidgets> int main(int argc, char *argv[]) { QApplication app(argc, argv); // 创建一个主窗口 QWidget window; window.setWindowTitle("Grid Layout Example"); // 创建一个网格布局 QGridLayout *layout = new QGridLayout(&window); // 创建一些控件 QLabel *label1 = new QLabel("Label 1"); QLabel *label2 = new QLabel("Label 2"); QPushButton *button1 = new QPushButton("Button 1"); QPushButton *button2 = new QPushButton("Button 2"); // 将控件添加到网格布局中 layout->addWidget(label1, 0, 0); // 在第一行、第一列 layout->addWidget(label2, 0, 1); // 在第一行、第二列 layout->addWidget(button1, 1, 0); // 在第二行、第一列 layout->addWidget(button2, 1, 1); // 在第二行、第二列 // 设置布局边距和控件间距 layout->setContentsMargins(10, 10, 10, 10); layout->setSpacing(10); // 显示主窗口 window.show(); return app.exec(); } ``` 在这个示例中,我们创建了一个主窗口,并在其中创建了一个网格布局。然后,我们创建了两个标签和两个按钮,并将它们添加到网格布局中的不同位置。最后,我们设置了布局的边距和控件间距,并显示了主窗口。 当运行这个示例时,你会看到标签和按钮按照网格布局的方式进行排列。 希望这个示例对你有所帮助!如果你还有其他问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值