【问题标题】:QT - How to get SIGNAL "currentIndexChanged" from QVector<QComboBox*>QT - 如何从 QVector<QComboBox*> 获取信号“currentIndexChanged”
【发布时间】:2017-08-25 18:35:21
【问题描述】:

我想从选定的 QComboBox 中获取 QString 文本。当我在 QComboBox 上选择一个索引时,我想在单击 QcomboBox 上所需的索引后从所选索引中获取 QString。

我研究过这个,

  1. Qt QCombobox currentIndexChanged signal

但还没有找到解决办法,

QVector<QComboBox*> cboxes;
 for (int i =0; i< 40 ; i++)
{
QComboBox *box = new QComboBox();
cboxes.append(box);
}
    for(int i = 0; i < 40; i++)
    {
        connect(cboxes[i], SIGNAL(currentIndexChanged(const QString &text)), this, SLOT(comboBoxAdjusted_Changed(QString)));
    }

comboBoxAdjusted_Changed 函数

void DialogSettings::comboBoxAdjusted_Changed(QString text)
{
    std::cout << text.toStdString() << endl;
}

我有尝试,但每次我更改组合框索引时,它都不会给出输出。

for (int i =0; i< 40 ; i++)
    {
    connect(cboxes[i], static_cast<void(QComboBox::*)(const QString &)>(&QComboBox::currentIndexChanged),
                        [=](const QString &text){
                        std::cout << text.toStdString() << endl;
    });

我该怎么办?

【问题讨论】:

  • 有什么问题?你想得到什么?,也请显示comboBoxAdjusted_Changed
  • QSignalMapper 可能是你想要的,作为一个疯狂的猜测......
  • @eyllanesc 我已经编辑了我的问题。
  • 你想在comboBoxAdjusted_Changed中做什么?
  • @eyllanesc 我想写信给 tcpSocket。

标签: c++ qt


【解决方案1】:

我看到信号语法缺少函数输入参数。

以下是currentIndexChanged 的两个有效信号

void    currentIndexChanged(int index)
void    currentIndexChanged(const QString &text)

如果您必须处理index,请尝试以下情况。

 for(int i = 0; i < 40; i++)
 {
     connect(cboxes[i], static_cast<void(QComboBox::*)(int)>(&QComboBox::currentIndexChanged),[=](int index){ /* YOUR CODE */ });
 }

【讨论】:

  • 好的,我试试
  • qOverload&lt;int&gt;(&amp;QComboBox::currentIndex),更清晰。 (或 QOverload&lt;int&gt;::of(&amp;QComboBox::currentIndex) 在旧版本上)。
猜你喜欢
  • 2012-02-12
  • 1970-01-01
  • 2013-08-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-17
相关资源
最近更新 更多