qt quick中如何实现替换掉原来属性的默认值且保持一些内容不变呢?



  • 我的题目可能有点绕哈.

    我有一个property,他的布局是被设定了的。但是如果用户替换掉这个property,怎么才能保持他的布局是和原来的一样呢?

    举例子:

      Bar{
       id: titleBar
       //left controls
    
       property Item leftControls:
           ControlsList{
               parent: titleBar
               anchors.left: parent.left
               anchors.verticalCenter: parent.verticalCenter
           }
    
       //right controls
       property Item rightControls:
           ControlsList{
               parent: titleBar
               anchors.right: parent.right
               anchors.verticalCenter: parent.verticalCenter
           }
      }
    

    我想在***leftControls***或者***rightControls***被替换的时候,他的布局总是最初默认的布局。

    怎么实现呢?



  • @Accelerated

    Bar{
        id: titleBar
        //left controls
    
        property Item leftControls: ControlsList{ }
    
        //right controls
        property Item rightControls: ControlsList{ }
        Binding {
           target: leftControls
           property: "parent"
           value: titleBar
        }
        Binding {
           target: leftControls
           property: "anchors.left"
           value: titleBar.left
        }
        Binding {
           target: leftControls
           property: "anchors.verticalCenter"
           value: titleBar.verticalCenter
        }
    
        Binding {
           target: rightControls
           property: "parent"
           value: titleBar
        }
        Binding {
           target: leftControls
           property: "anchors.left"
           value: titleBar.left
        }
        Binding {
           target: leftControls
           property: "anchors.verticalCenter"
           value: titleBar.verticalCenter
        }
    }
    


  • @qyvlikqt quick中如何实现替换掉原来属性的默认值且保持一些内容不变呢? 中说:

    //left controls
    
    property Item leftControls: ControlsList{ }
    
    //right controls
    property Item rightControls: ControlsList{ }
    Binding {
       target: leftControls
       property: "parent"
       value: titleBar
    }
    Binding {
       target: leftControls
       property: "anchors.left"
       value: titleBar.left
    }
    Binding {
       target: leftControls
       property: "anchors.verticalCenter"
       value: titleBar.verticalCenter
    }
    
    Binding {
       target: rightControls
       property: "parent"
       value: titleBar
    }
    Binding {
       target: leftControls
       property: "anchors.left"
       value: titleBar.left
    }
    Binding {
       target: leftControls
       property: "anchors.verticalCenter"
       value: titleBar.verticalCenter
    }
    

    我试试哈~



  • @qyvlik 由于ControlsList是继承Row的,运行了代码后。提示:

      QML Row: Cannot anchor to an item that isn't a parent or sibling.
    

    我需要改变它吗?



  • @Accelerated 要,做一些调整,代码总体框架是没错的。


Log in to reply
 

走马观花

最近的回复

  • 诶 没有Linux吗??

    read more
  • 萌梦 男孩,女孩,和蛋

    menghome.png

    read more
  • 设计模式-工厂模式

    使用qt/qml来演示设计模式效果,便于学习理解

    1)定义创建对象的接口,封装对象的创建
    2)使具体化类的工作延迟到工厂子类中

    bg.png
    image.png

    1. 工厂类

    createProduct使用了参数来选择要创建哪个产品

    #ifndef FACTORY_H #define FACTORY_H #include <QObject> class Product; class QString; class Factory: public QObject { Q_OBJECT public: virtual ~Factory() = 0; virtual Product* createProduct(QString type) = 0; protected: Factory(); }; class ConcreteFactory: public Factory { Q_OBJECT public: ~ConcreteFactory(); ConcreteFactory(); public slots: Product* createProduct(QString type); }; #endif // FACTORY_H #include "factory.h" #include "product.h" #include <QtQml/qqml.h> Factory::~Factory() { } Factory::Factory() { qmlRegisterType<Product>("Product", 1, 0, "Product"); } ConcreteFactory::~ConcreteFactory() { } ConcreteFactory::ConcreteFactory() { } Product *ConcreteFactory::createProduct(QString type) { if(type == "boy") return static_cast<Product *>(new ConcreteProduct1()); else if(type == "girl") return static_cast<Product *>(new ConcreteProduct2()); return static_cast<Product *>(new ConcreteProduct1()); } 2 产品类

    一个产品是萌梦男,一个产品是萌梦女

    #ifndef PRODUCT_H #define PRODUCT_H #include <QObject> class Product: public QObject { Q_OBJECT public: virtual ~Product() = 0; Q_PROPERTY(QString icon READ icon NOTIFY iconChanged) QString m_icon; QString icon() const { return m_icon; } signals: void iconChanged(QString icon); protected: Product(); signals: public slots: }; class ConcreteProduct1: public Product { Q_OBJECT public: ~ConcreteProduct1(); ConcreteProduct1(); }; class ConcreteProduct2: public Product { Q_OBJECT public: ~ConcreteProduct2(); ConcreteProduct2(); }; #endif // PRODUCT_H #include "product.h" Product::~Product() { } Product::Product() { } ConcreteProduct1::~ConcreteProduct1() { } ConcreteProduct1::ConcreteProduct1() { m_icon = "qrc:/images/boy.png"; } ConcreteProduct2::~ConcreteProduct2() { } ConcreteProduct2::ConcreteProduct2() { m_icon = "qrc:/images/girl.png"; } 3. main.qml

    使用timer,canvas,listview等实现一个自动化生产的动画效果

    源代码

    Fork me on Gitee

    read more
  • blender建模 章鱼
    捕获2.PNG 😵

    read more

关注我们

微博
QQ群