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.
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.
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.
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:
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!
You just need to set focus to the list:
if (ui->listWidget->count() > 0) {
ui->listWidget->item(0)->setSelected(true);
}
ui->listWidget->setFocus();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With