Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

put member QScopedPointer in QList for convenience



  • hi,
    lack of c++ knowledge here

    i have 3 QScopedPointers defined as member variables like this

    private:
    QScopedPointer<QOpcUaNode> m_Node1;
    QScopedPointer<QOpcUaNode> m_Node2;
    QScopedPointer<QOpcUaNode> m_Node3;
    

    in one of my class methods i want to put them in a QList to access them by index but i don't seem to be able to do it correctly
    not sure if i have to put value/reference or pointer of my members in the list

    something like this :

    void enableOneMonitoring(int indexToEnable)
    
        QList<QScopedPointer<QOpcUaNode>*> nodes;
        nodes<<&m_Node1<<&m_Node2<<&m_Node3;
    
        for(int i=0;i<nodes.length();i++){
            if(i==indexToEnable){
                //nodes.at(i)->enableMonitoring(QOpcUa::NodeAttribute::Value, QOpcUaMonitoringParameters());
            }else{
                //nodes.at(i)->disableMonitoring(QOpcUa::NodeAttribute::Value);
            }
        }
    

    can someone give me a hint on this please ?
    thx


  • Moderators

    put member QScopedPointer in QList

    Hi @LeLev, QScopedPointer is like std::unique_ptr. You are not meant to have multiple copies of it. Putting something into a container like QList creates a copy, which is not allowed.

    What do you want to achieve by using QScopedPointer?



  • @JKSH thank you for your answer

    @JKSH said in put member QScopedPointer in QList for convenience:

    What do you want to achieve by using QScopedPointer?

    i did a QList<QScopedPointer<QOpcUaNode>*> just because my members are defined as QScopedPointer<QOpcUaNode>

    all i want is to put my QOpcUaNode s in a Qlist to be able to iterate over them

    would this be better ?

        QList<QOpcUaNode*> nodes;
        nodes<<m_g54Node.data()<<m_g55Node.data()<<m_g56Node.data();
    

    edit : actually this still creates a copy as you said..
    what option i have to be able to iterate over my QOpcUaNode s then ?


  • Moderators

    @LeLev said in put member QScopedPointer in QList for convenience:

    just because my members are defined as QScopedPointer<QOpcUaNode>

    In my last post, I was asking, "Why do you want to store your members as QScopedPointer"? Please answer this question.

    would this be better ?

    QList<QOpcUaNode*> nodes;
    

    Yes.

    edit : actually this still creates a copy as you said..

    Well, it doesn't copy a QScopedPointer and it doesn't copy QOpcUaNode, so that's OK.

    • Copying a raw pointer is OK.
    • Copying a std::shared_ptr is OK.
    • Copying a QScopedPointer is not OK
    • Copying a std::unique_ptr is not OK

    what option i have to be able to iterate over my QOpcUaNode s then ?

    You can use QList<QOpcUaNode*> m_nodes; as your member variable. Instead of caling m_node1->nodeId(), call m_nodes[0]->nodeId().



  • @JKSH said in put member QScopedPointer in QList for convenience:

    In my last post, I was asking, "Why do you want to use QScopedPointer"? Please answer this question.

    i have no real reason, i have started this application a year ago, i saw the official exemples and did the same thing
    https://doc.qt.io/QtOPCUA/qtopcua-waterpump-waterpump-qmlcpp-example.html#member-variables

    @JKSH said in put member QScopedPointer in QList for convenience:

    You can use QList<QOpcUaNode*> m_nodes; as your member variable. Instead of caling m_node1->nodeId(), call m_nodes[0]->nodeId().

    yes tought having My Qlist as member too. And i will try without QScopedPointer i guess
    Thank you very much



  • @JKSH one more question pls
    now i have this members

      QList<QOpcUaNode*> m_nodes;
    
       QOpcUaNode* m_Node1;
       QOpcUaNode* m_Node2;
       QOpcUaNode* m_Node3;
    

    and in my class constructor i do

    m_nodes<<m_Node1<<m_Node2<<m_Node3;
    
    

    but then i have troubles to initialisze and set the node ids of my nodes

    m_nodes[0] = new QOpcUaNode(m_client->node("ns=4;s=Global_Variables.v1"),m_client);
    //no matching constructor 
    

    with QScopedPointers i was doing

    QScopedPointer<QOpcUaNode> m_Node1;
    m_Node1.reset(m_client->node("ns=4;s=Global_Variables.v1"));
    

  • Qt Champions 2019

    @LeLev said in put member QScopedPointer in QList for convenience:

    QOpcUaNode* m_Node1;
    QOpcUaNode* m_Node2;
    QOpcUaNode* m_Node3;

    Why do you need them if you have m_nodes?
    Jist do

    m_nodes.append(new QOpcUaNode(m_client->node("ns=4;s=Global_Variables.v1"),m_client));
    

    If it says no matching constructor then please check the QOpcUaNode documentation.



  • @jsulm said in put member QScopedPointer in QList for convenience:

    Why do you need them if you have m_nodes?

    i wanted to create m_Node1,2,3 to put them in my list..

    i never ask questions here without looking doc and googling first

    constructor wants QOpcUaNodeImpl* and a QOpcUaClient * i could not figure out what exactly is QOpcUaNodeImpl

    edit looking deeper in docs i found this :

        QOpcUaNode *node( m_client->node("ns=4;s=Global_Variables.v1") );
        m_nodes.append( node );
    

    thx for your help @JKSH and @jsulm


  • Moderators

    @LeLev said in put member QScopedPointer in QList for convenience:

    i saw the official exemples and did the same thing
    https://doc.qt.io/QtOPCUA/qtopcua-waterpump-waterpump-qmlcpp-example.html#member-variables

    I see.

    That's quite a different coding style compared to other Qt examples out there.

    @LeLev said in put member QScopedPointer in QList for convenience:

    now i have this members

      QList<QOpcUaNode*> m_nodes;
    
       QOpcUaNode* m_Node1;
       QOpcUaNode* m_Node2;
       QOpcUaNode* m_Node3;
    

    and in my class constructor i do

    m_nodes<<m_Node1<<m_Node2<<m_Node3;
    

    m_Node1, m_Node2, and m_Node3 are uninitialized pointers here. Make sure you initialize the pointers before you put them inside your list!

    but then i have troubles to initialisze and set the node ids of my nodes

    m_nodes[0] = new QOpcUaNode(m_client->node("ns=4;s=Global_Variables.v1"),m_client);
    //no matching constructor 
    

    with QScopedPointers i was doing

    QScopedPointer<QOpcUaNode> m_Node1;
    m_Node1.reset(m_client->node("ns=4;s=Global_Variables.v1"));
    

    I highly recommend you spend some time reading about smart pointers and how to use them. Examples:

    constructor wants QOpcUaNodeImpl* and a QOpcUaClient * i could not figure out what exactly is QOpcUaNodeImpl

    In Qt, if something is labelled ...Impl or ...Private it usally refers to an internal class. We should pretend those classes don't exist.

    i never ask questions here without looking doc and googling first

    That's great. Thank you for making the effort to do your own research! I wish all people who ask questions here would do that :-D



  • @JKSH thank you very much for valuable informations and advises


Log in to reply