Question

I am using QT 5.2 and Have a QListWidget witch displays a list of Strings and I want the first item (at 0) to be initially set to be highlighted. I tried the following:

mUi->listWidget->setCurrentRow(0);
    mUi->listWidget->setCurrentItem(mUi->listWidget->item(0),QItemSelectionModel::Select);
    mUi->listWidget->currentItem()->setSelected(true);
    mUi->listWidget->selectionModel()->select(mUi->listWidget->model()->index(0,0, QModelIndex()),QItemSelectionModel::Select); 

Even if the if the item is selected it is not highlighted. If ofcourse I navigate to the item using mouse (click) or keyboard (tab key) it is highlighted but I want it to be highlighted initially without using mouse or keyboard. How to do it? Thanks in advance.

Was it helpful?

Solution

You just need to set focus to the list:

if (ui->listWidget->count() > 0) {
  ui->listWidget->item(0)->setSelected(true);
}
ui->listWidget->setFocus();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top