我有一个清晰的C++ Qt代码块,其中main创建一个图形项(父级),该图形项创建了一堆子图形项。 child 和 parent 必须互相调用方法,即 parent 需要告诉 child 做一些事情(移动,改变颜色等), child 会发信号通知 parent 要对其他 child 做事。彼此调用彼此的方法会导致产生一些丑陋的循环代码,并且令人沮丧地避免C2027错误。使用自定义信号/插槽系统作为 child 与 parent 之间的通信方式是否有意义?还是应该继续使用当前的设计并尝试解决这些错误?

例如,此代码(我当前的设计)生成C2027:

主窗口:

#include "mainwindow.h"
#include "ui_mainwindow.h"

vector<Foo*> FooArray;

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    scene = new QGraphicsScene(this);
    ui->graphicsView->setScene(scene);

    int viewwidth = ui->graphicsView->width();

    ...

    FooArray.push_back(new Foo(5, viewwidth, scene));
    FooArray[0]->loadBars();
}

Foo:
#include "Foo.h"  //includes bar.h


vector<int> ActiveList;
vector<Bar*> BarArray;

Foo::Foo(int id, int w, QGraphicsScene* scene)
{
    this->id = id;
    this->width = w;
    this->scene = scene;
}

...

void Foo::loadBars()
{
    ...
    BarArray.resize(nitems);
    BarArray[k] = new Bar(id, k, this);
    BarArray[k]->setPos(...);
    scene->addItem(BarArray[k]);
    ...
}

void Foo::doSomething()
{
    ...
}

酒吧:
#include "Bar.h"  //forward declares Foo; isn't executed until Foo exists.

Bar::Bar(int setid, int num, Foo* foo)
{
    this->s = setid;
    this->n = num;
    this->owner = foo;
}

...

void Bar::someFunction()
{
    ...
    owner->doSomething();
    ...
}

最佳答案

是的,这正是信号和插槽的作用。

现在你有Bar::someFunction(),它调用Foo::doSomething()
您可以做的是:

  • 声明doSomething()为插槽
  • Have Bar发出信号somethingHappened()
  • 创建子代时,将其somethingHappened()信号连接到父代的doSomething()插槽

  • 这是依赖注入(inject)的一个示例,它使您将来可以将父类交换为其他对象,而不必完全更改Bar

    10-04 22:54
    查看更多