如何从组合盒发出信号

  • 本文关键字:信号 组合 c++ qt
  • 更新时间 :
  • 英文 :


我有一个插槽,我可以在其中创建一个组合框并添加项目,但是每当用户选择项目时,我都会向UI管理器发出信号。我想知道是否有办法使用关键字 emit?

我不能在这里使用连接,因为我无法为 UI 管理器创建对象。 不确定我是否有意义,或者我正在解决我的问题。

void Test::dropDown(){
comboBox = new QComboBox(this);
comboBox->addItem("Test 1");
comboBox->addItem("Test 2");
comboBox->addItem("Test 3");
comboBox->showPopup();
//connect(comboBox, QOverload<int>::of(&QComboBox::activated), )
//I want to emit the activated item using the keyword "emit" and not connect
emit
}

你可以做一个信号链连接两个信号,而不是一个信号和一个插槽,没有理由自己发射信号。

给你的Test班一个这样的信号:

signals:
void comboBoxActivated(int index);

创建组合框时:

comboBox = new QComboBox(this);    
connect(comboBox, QOverload<int>::of(&QComboBox::activated), this, &Test::comboBoxActivated);

现在给你的 UI 管理器类一个槽来接收activated信号:

private slots:
void testComboBoxActivated(int index);

现在,如果您在 UI 管理器类中有一个Test实例,例如

Test * test = new Test();

将其信号连接到管理中心插槽:

connect(test, &Test::comboBoxActivated, this, &UIManager::testComboBoxActivated);

这样,组合将首先发出信号,该信号将由Test转发,最后由UI管理器插槽接收。

如果我理解得很好,您需要在单击QComboBox项时emit信号; 为此,您需要将激活的信号连接到发射,也许使用 lambda 函数:

connect(comboBox, &QComboBox::activated, this, [this](int index)
{
// Here emit your signal for the item at index `index`
});

最新更新