2017-08-25 70 views
0

我想从选定的QComboBox中获取QString文本。当我在QComboBox上选择一个索引时,我想从QcomboBox中点击所需的索引后,从所选索引中获取QString。QT - 如何从QVector获得SIGNAL“currentIndexChanged”<QComboBox*>

我研究这个,

  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; 
    }); 

我该怎么办?

+0

什么问题?你想得到什么?,也请显示comboBoxAdjusted_Changed – eyllanesc

+1

QSignalMapper可能是你想要的,作为一个疯狂的猜测... – hyde

+0

@eyllanesc我编辑我的问题。 – Khalif21

回答

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 */ }); 
} 
+0

好的,我会试试 – Khalif21

+0

我已经试过了。我编辑了我的问题。 – Khalif21

+0

做完“试验和错误”后,它就起作用了。 – Khalif21