上篇文章 講了輸入法的大概流程,製作。 使用等步驟。
在正式應有的時候發現, QComboBox的時候 會彈出輸入法,但是我不需要。 於是琢磨了幾種辦法都沒行同, 想了下在java裏面每個對象有個屬性就是對象名稱。 於是我就試試在qt裏面找。 居然讓我發現了。 ObjectName. 由於QComboBox 分爲兩部分, 一個是QLineEdit, 一個是QAbstractItemView , 第一個不用說就是主顯示,第二個是下拉列表哪個控件。 有多個實現方式,具體怎麼實現的可以看源代碼。 可以用ComboBox.showPopup() ComboBox.hidePopup(),手動顯示隱藏該控件。 由於comboBox自己內部創建的QAbstractItemView 對象沒有名稱, 於是我就在顯示輸入法的時候 判斷當前的焦點窗口對象(這裏說下, QCombobox的焦點會在QLineEdit與QAbstractItemView之間變化。 當沒有你點擊彈出下拉列表時候 QLineEdit → QAbstractItemView, 當點擊了一個選項或者隱藏了QAbstractItemView的時候 焦點又從QAbstractItemView 回到了QLineEdit 。 )名稱是否爲空, 如爲空就不彈出。
代碼: myInput.cpp function updateHandle.
void myInput::updateHandler(int type)
{
QPointer<QWidget> w = QApplication::focusWidget();
switch(type)
{
case QWSInputMethod::FocusIn:
{
if( w && !w->inherits( "QComboBox" ) && !w->objectName().isEmpty() )
{
updatePosition();
m_ky->show();
break;
}
}
case QWSInputMethod::FocusOut:
{
m_ky->hide();
break;
}
default:
break;
}
QWSInputMethod::updateHandler( type );
}
好了。如有後續bug,會附上解決方案。