关于QT的QCombox的掉坑出坑

关于QT的QCombox的掉坑出坑

   最近项目中开发在用到QCombox,然而在开发中,踩到了一个坑,花了一个晚上,一直在想,好在最后找到问题所在了。

这是业务的流程。直接说重点:QCombox在下拉窗更新数据的时候,会默认把下拉窗的第一个条数据,设置为选中的状态,这样的话,就会产生curentTextChanged的一个信号,直接把逻辑层原先有的数据给修改了。

  为此,进行了日志打印一探究竟:

void TalkWindow::getMicList(const QStringList & texts)
{

	DEBUG_TRACE("getCurrentText %s" , m_pDeviceWidget->m_pMicrophoneCombox->currentText().toStdString().c_str());
	if (m_pDeviceWidget == NULL) return;
	m_pDeviceWidget->m_pMicrophoneCombox->clear();
	int a = m_pDeviceWidget->m_pMicrophoneCombox->count();
	m_pDeviceWidget->m_pMicrophoneCombox->setCurrentIndex(-1);
	DEBUG_TRACE("getCurrentText1 %s", m_pDeviceWidget->m_pMicrophoneCombox->currentText().toStdString().c_str());
	m_pDeviceWidget->m_pMicrophoneCombox->addItems(texts);
	DEBUG_TRACE("getCurrentText2 %s", m_pDeviceWidget->m_pMicrophoneCombox->currentText().toStdString().c_str());
	m_pDeviceWidget->m_pMicrophoneCombox->setCurrentIndex(-1);
	m_pDeviceWidget->m_pMicrophoneCombox->insertItem(texts.size()+1, TR_TALK_DEVICE_COMBOX, 0);
	m_pDeviceWidget->m_pMicrophoneCombox->setCurrentIndex(-1);
	DEBUG_TRACE("getCurrentText3 %s", m_pDeviceWidget->m_pMicrophoneCombox->currentText().toStdString().c_str());
	m_pDeviceWidget->m_pMicrophoneCombox->setCurrentIndex(-1);
	DEBUG_TRACE("getCurrentText4 %s", m_pDeviceWidget->m_pMicrophoneCombox->currentText().toStdString().c_str());
	m_pDeviceWidget->m_pMicrophoneCombox->setCurrentIndex(-1);
	DEBUG_TRACE("getCurrentText!!!!! %s", m_pDeviceWidget->m_pMicrophoneCombox->currentText().toStdString().c_str());
}

   日志显如下:

	Line 151: [D:TalkWindow+13100:10356] 23:05:55.942 101261.059234 getCurrentText 麦克风 (7.1 SOUND EFFECT GAMING HEADSET)
	Line 152: [D:TalkWindow+13100:10356] 23:05:55.943 101261.059569 getCurrentText1 
	Line 155: [D:TalkWindow+13100:10356] 23:05:55.943 101261.060339 getCurrentText2 麦克风 (High Definition Audio 设备)
	Line 156: [D:TalkWindow+13100:10356] 23:05:55.944 101261.060662 getCurrentText3 
	Line 157: [D:TalkWindow+13100:10356] 23:05:55.944 101261.060866 getCurrentText4 
	Line 158: [D:TalkWindow+13100:10356] 23:05:55.944 101261.061036 getCurrentText!!!!! 
在getCurrentText2的日志中,很明显,把additems的数据加载完完,默认选中就变了,然后再执行 setCurrentIndex的时候,把默认选中置为空,也就是说,一共产生了两次 textchange的信号。

解决办法:
我目前的解决办法是,在获取数据的时候,先断开信号槽的连接,disconnect函数,然后等把下拉窗的数据加载完,再connect函数连接上,这样,加载数据就避免了发送两次信号,修改原有的数据。当然也
可以一个标记bool值,信号发出去的时候,不去处理就好了,但是这样的话,可能不太好控制。所以我选择了前者。有更好的办法,多多指教。
关于QT的QCombox的掉坑出坑

相关文章:

你感兴趣的文章:

标签云: