簡體   English   中英

從不同的功能編輯相同的QList

[英]Edit the same QList from different functions

我有一個QList存儲在一個類中,該類擁有一個指向另一個類的實例的指針,該類具有一些編輯QList的函數。 我想要做的是在C ++中從多個類中編輯一個QList。 當我從列表中刪除一個元素時,我想將它從classA中的myvariable變量中刪除。 代碼如下

class classA {
    ...

private:
    ClassB *myclass;
    QList<mytype> myvariable;
}

class classB {
    classB::classB(QList<mytype> &variable) {
        this->myvariable = variable;
    }

    ...

private:
    QList<mytype> myvariable;
}

你非常接近只是幾個調整。 使myvariable成為引用,並在classB的構造函數中初始化它,如下所示:

class classB 
{
  classB(QList<mytype> &variable) 
   : myvariable(variable)
  {
  }

private:
   QList<mytype>& myvariable;
}

您可以引入一個包含靜態qlist的類c。 然后使它成為類a和b的公共子類。

編輯:

class classC {
protected:
    static QList<mytype> mystatic;
}    

class classA : public classC {
private:
}

class classB : public classC {
    classB::classB(QList<mytype> &variable) {
        mystatic = variable;
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM