69184

Qt 按键控制combobox

<h2>需求:</h2>

1、按键控制combobox显示下拉框,收起下拉框

2、按键控制combobox上下选取

3、按键控制combobox光标选取

4、combobox显示滚动条

<h2>实现:</h2> <h3>combobox初始化:</h3> //航次选择 font.setPointSize(14); ui->ComboBox_Voyage->setFont(font); ui->ComboBox_Voyage->setStyleSheet(QString::fromUtf8(ButtonUnFocus)); ui->ComboBox_Voyage->view()->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);//设置滚动条 ui->ComboBox_Voyage->setAttribute(Qt::WA_DeleteOnClose); <h3>按键处理:</h3> void Display_PlayBack::key_Scan() { QModelIndex itemIndex; //----------读取全局变量中的按键信息----------// globeData->systemPara.mutex.lock(); //主线程开启一个按键读取线程 switch(globeData->systemPara.keyPressedValue) { case KEY_RETURN_SHORT: //1、判断焦点所处位置 //2、执行相应操作 switch(flag_count) { case 1: break; case 2: break; case 3://按下combox if( ui->ComboBox_Voyage->isEnabled())//如果航次combox可以选取,那么执行以下操作 { bool_combox = !bool_combox; if( true == bool_combox ) { //ui->ComboBox_Voyage->QComboBox::showPopup(); }else { QStringList list = ui->ComboBox_Voyage->currentText().split(QString::fromUtf8("航次")); str_voyage = list[1]; //ui->ComboBox_Voyage->QComboBox::hidePopup(); qDebug()<<"航次="<<str_voyage; } } break; case 4: break; case 5: break; case 6: break; default:break; } break; case KEY_SET_SHORT: break; case KEY_Left: if(true == bool_combox )//如果选中combox { flag_combox--; if( flag_combox<=0 )flag_combox=ui->ComboBox_Voyage->count();//combox下拉项只有(搜索结果)个 itemIndex = ui->ComboBox_Voyage->view()->model()->index(flag_combox-1,0);//combox视图中下拉项编号 ui->ComboBox_Voyage->view()->selectionModel()->select(itemIndex,QItemSelectionModel::SelectCurrent);//SelectCurrent ui->ComboBox_Voyage->setCurrentIndex(flag_combox-1); //combox视图选中 固定编号的项目 }else if(true == bool_label_speed) { ; }else//焦点汇聚到全局控件上 { flag_count--; if(flag_count<=0)flag_count=6; } break; case KEY_Right: if(true == bool_combox)//如果选中combox { flag_combox++; if( flag_combox>=(ui->ComboBox_Voyage->count()+1) )flag_combox=1; itemIndex = ui->ComboBox_Voyage->view()->model()->index(flag_combox-1,0);//获取位置 ui->ComboBox_Voyage->view()->selectionModel()->select(itemIndex,QItemSelectionModel::SelectCurrent);//选中 ui->ComboBox_Voyage->setCurrentIndex(flag_combox-1);//设置text //combox视图选中 固定编号的项目 }else if(true == bool_label_speed) { ; }else//焦点汇聚到全局控件上 { flag_count++; if(flag_count>=7)flag_count=1; } break; case keyPressed_None: break; default:break; } globeData->systemPara.mutex.unlock(); }

来源:博客园

作者:硕果磊磊

链接:https://www.cnblogs.com/shuoguoleilei/p/11425634.html

Recommend