Qt QcomboBox使用方法

pamxy發表於2013-07-24

轉自:http://blog.sina.com.cn/s/blog_712038ba0101dgfx.html

QcomboBox

The QComboBox widget is a combined buttonand popup list.

說白了就是下拉選單,當點選QcomboBox時,QcomboBox將獲得焦點並將其展開,展開以後,焦點將位於QcomboBox.view(),當選中QcomboBox.view()下的某一項時焦點又回到QcomboBox上,QcomboBox的大體構建為:Model-àviewàQcomboBox,資料存在Model中,通過View顯示出來就構成了QcomboBox;

 

QcomboBox常用的方法有:

Eg:QcomboBox *combox;

1.       將QcomboBox展開:combox->showPopup();

2.       將QcomboBox收起來:combox->hidePopup();

3.       當QcomboBox展開後焦點位於QcomboBox.view()下,這是我們不想有滑鼠點選,想通過方向鍵控制上下移動,安另外一個鍵控制選擇;

當obj== combox->view()時

if(keyEvent->key()==Qt::Key_Up)

{

    Intcurrent_index=combox->currentIndex();

    current_index--;

    combox->setCurrentIndex(current_index);

    QModelIndexitemIndex = combox->view()->model()->index(current_index,0);

    combox->view()->selectionModel()->setCurrentIndex(itemIndex,QItemSelectionModel::SelectCurrent);

}

else if(keyEvent->key()==Qt::Key_Y)

    {

        QModelIndexitemIndex = combox->view()->model()->index(current_index,0);

        combox->view()->selectionModel()->setCurrentIndex(itemIndex,QItemSelectionModel::ToggleCurrent);

        combox->setCurrentIndex(current_index);

        combox->hidePopup();

        returntrue;

    }

 


相關文章