-1

I use physical Knob for control menu than included QCombobox. When press knob then call click function of ComboBoxObject. When knob rotate then rotate function call with value {-1,1}. I need change highlighted item when QComboBox is popup in rotate function.

void ComboBoxObject::click(){
showPopup();
}

void ComboBoxObject::rotate(direct int) // Left=-1, Right=1
{
    up/down
}

When focus on Combobox setCurrenIndex(index) work correct but if pupop list then it doesn't work. I use highlighted(index) but not work.

I try simulate key_up and key_dow for change highlighted item. highlighted item doesn't change.

void ComboBoxObject::rotate(direct int) // Left=-1, Right=1
{      
    QKeyEvent *event;
    if(direct == 1){
        event = new QKeyEvent ( QEvent::KeyPress, Qt::Key_Up,Qt::NoModifier);
    }
    else{
        event = new QKeyEvent ( QEvent::KeyPress, Qt::Key_Down,Qt::NoModifier);
    }
    bool result = QApplication::sendEvent(this,event);
}
ahad
  • 1
  • 1

1 Answers1

0

QComboBox doesn't have that functionality built in, but you can implement it yourself easily enough:

void rotateComboBox(QComboBox * theComboBox, int direction)
{
   const int numItems = theComboBox->count();
   const int curIndex = theComboBox->currentIndex();
   int newIndex = curIndex+direction;
   if (newIndex <= 0) newIndex = numItems-1;  // wrap around
   if (newIndex >= numItems) newIndex = 0;    // wrap around
   theComboBox->setCurrentIndex(newIndex);
}
Jeremy Friesner
  • 70,199
  • 15
  • 131
  • 234