Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set an item in a QListWidget as initially highlighted?

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.

like image 469
mj1261829 Avatar asked Apr 14 '14 16:04

mj1261829


People also ask

How do I use qlistwidget?

QListWidget class is an item-based interface to add or remove items from a list. Each item in the list is a QListWidgetItem object. ListWidget can be set to be multiselectable. The following example shows the click event being captured to pop up a message box. The above code produces the following output.

Who can see the itemiseditable flag in qlistwidget?

Only users with topic management privileges can see it. Qt 5.7. I am using a QListWidget, where its QListWidgetItem s have the ItemIsEditable flag. At the moment, at least, the items are the standard strings, with the editor being the built-in QLineEdit, i.e.

How do I set the selection mode of a list widget?

This can be set with the setSelectionMode () function. There are two ways to add items to the list: they can be constructed with the list widget as their parent widget, or they can be constructed with no parent widget and added to the list later. If a list widget already exists when the items are constructed, the first method is easier to use:

How do I update only single values from a list item?

If you want to update only single values, create three events and update the list items in MyDialog::customEvent accordingly. … Download, Vote, Comment, Publish. Forgot your password? This email is in use. Do you need your password? Submit your solution!


1 Answers

You just need to set focus to the list:

if (ui->listWidget->count() > 0) {
  ui->listWidget->item(0)->setSelected(true);
}
ui->listWidget->setFocus();
like image 161
Pavel Strakhov Avatar answered Sep 16 '22 22:09

Pavel Strakhov