rust语言orbtk GUI基础(old2018)-2.HelloWorld

orbtk的api在2019.2进行了修改,该文章为2018版
新版请转[新版orbtk gui基础]

orbtk helloworld

我们在上一节的代码中加入一个helloworld的label:

use orbtk::*;

fn main() {
    let mut application = Application::default();
    
    //创建一个textblock,添加一个Label类型的属性
    let text_block=TextBlock::create()
        .with_property(Label::from("Hello, World!"));
    //创建root控件,把textBlock设置为子控件
    let root=Template::default()
        .as_parent_type(ParentType::Single)
        .with_child(text_block);

    application
        .create_window()
        .with_bounds(Bounds::new(100, 100, 320, 210))
        .with_title("OrbTk - helloworld")
        .with_resizable(true)//可以调整窗口大小
        .with_root(root)
        //.with_debug_flag(true)
        .build();
    application.run();
}

cargo run运行后界面如下:

orbtk-helloworld
注意:

  • TextBlock的定义是:pub struct TextBlock;
  • 但是TextBlock::create()函数返回的是一个Template
  • .with_property()是Template的函数,返回self,类型是Template
  • Label是一个属性(Property),不是一个控件(widget)
  • root控件定义时必须调用as_parent_type()函数设置ParentType属性

parent_type是什么,root为什么必须要设置呢?

parent_type是Template的一个成员,类型为ParentType,它指示Template控件能够添加子控件的个数。

ParentType定义为:

pub enum ParentType {
    None,//表示不能添加子控件
    Single,//只能添加一个子控件
    Multi,//可以添加多个子控件
}

当添加子控件到一个widget template时。

  • 如果parent_typeParentType::None,则函数自动跳过,什么也不添加
  • 如果parent_typeParentType::Single,则只能添加一个子控件,如果已经有一个子控件,则覆盖/替换
  • 如果parent_typeParentType::Multi,则可以添加任意多个子控件到当前控件的Template

所以,在添加子控件之前必须设置parent_type。事实上orbtk内建的控件都有默认的parent_type值,比如row,colomn等默认为Multi,Button等默认为Single。

使用链式调用方法

orbtk的属性设置,控件(widget)创建都是支持链式调用的,所以像text_block等不需要绑定到变量上,代码看上去也更减短。如下代码与上节helloworld代码效果完全相同

use orbtk::*;

fn main() {
    let mut application = Application::default();

    let root = Template::default()
        .as_parent_type(ParentType::Single)
        .with_child(TextBlock::create().with_property(Label::from("Hello, World!")));

    application
        .create_window()
        .with_bounds(Bounds::new(100, 100, 320, 210))
        .with_title("OrbTk - helloworld")
        .with_resizable(true)
        .with_root(root)
        //.with_debug_flag(true)
        .build();
    application.run();
}

本文代码

本文代码在https://github.com/hustlei/RustGuiOrbtkTutorial

可以使用cargo run --bin helloworld命令编译运行

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

hustlei

您的鼓励将是我创作的最大动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值