Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set non-selectable default text on QComboBox?

Tags:

c++

qt

qcombobox

Using a regular QComboBox populated with items, if currentIndex is set to -1, the widget is empty. It would be very useful to instead have an initial descriptive text visible in the combo box(e.g. "--Select Country--", "--Choose Topic--", etc.) which is not shown in the dropdown list.

I couldn't find anything in the documentation, nor any previous questions with answers.

like image 903
swalog Avatar asked Oct 03 '11 08:10

swalog


3 Answers

It doesn't appear that case was anticipated in the Combo Box API. But with the underlying model flexibility it seems you should be able to add your --Select Country-- as a first "legitimate" item, and then keep it from being user selectable:

QStandardItemModel* model =
        qobject_cast<QStandardItemModel*>(comboBox->model());
QModelIndex firstIndex = model->index(0, comboBox->modelColumn(),
        comboBox->rootModelIndex());
QStandardItem* firstItem = model->itemFromIndex(firstIndex);
firstItem->setSelectable(false);

Depending on what precise behavior you want, you might want to use setEnabled instead. Or I'd personally prefer it if it was just a different color item that I could set it back to:

Qt, How do I change the text color of one item of a QComboBox? (C++)

(I don't like it when I click on something and then get trapped to where I can't get back where I was, even if it's a nothing-selected-yet-state!)

like image 57
HostileFork says dont trust SE Avatar answered Oct 14 '22 17:10

HostileFork says dont trust SE


One way you can do something similar is to set a placeholder:

comboBox->setPlaceholderText(QStringLiteral("--Select Country--"));
comboBox->setCurrentIndex(-1);

This way you have a default that cannot be selected.

enter image description here

like image 27
Ayxan Haqverdili Avatar answered Oct 14 '22 17:10

Ayxan Haqverdili


Leaving my solution here from PyQt5. Create a proxy model and shift all the rows down one, and return a default value at row 0.

class NullRowProxyModel(QAbstractProxyModel):
    """Creates an empty row at the top for null selections on combo boxes
    """

    def __init__(self, src, text='---', parent=None):
        super(NullRowProxyModel, self).__init__(parent)
        self._text = text
        self.setSourceModel(src)

    def mapToSource(self, proxyIndex: QModelIndex) -> QModelIndex:
        if self.sourceModel():
            return self.sourceModel().index(proxyIndex.row()-1, proxyIndex.column())
        else:
            return QModelIndex()

    def mapFromSource(self, sourceIndex: QModelIndex) -> QModelIndex:
        return self.index(sourceIndex.row()+1, sourceIndex.column())

    def data(self, proxyIndex: QModelIndex, role=Qt.DisplayRole) -> typing.Any:
        if proxyIndex.row() == 0 and role == Qt.DisplayRole:
            return self._text
        elif proxyIndex.row() == 0 and role == Qt.EditRole:
            return None
        else:
            return super(NullRowProxyModel, self).data(proxyIndex, role)

    def index(self, row: int, column: int, parent: QModelIndex = ...) -> QModelIndex:
        return self.createIndex(row, column)

    def parent(self, child: QModelIndex) -> QModelIndex:
        return QModelIndex()

    def rowCount(self, parent: QModelIndex = ...) -> int:
        return self.sourceModel().rowCount()+1 if self.sourceModel() else 0

    def columnCount(self, parent: QModelIndex = ...) -> int:
        return self.sourceModel().columnCount() if self.sourceModel() else 0

    def headerData(self, section: int, orientation: Qt.Orientation, role: int = ...) -> typing.Any:
        if not self.sourceModel():
            return None
        if orientation == Qt.Vertical:
            return self.sourceModel().headerData(section-1, orientation, role)
        else:
            return self.sourceModel().headerData(section, orientation, role)
like image 23
kblst Avatar answered Oct 14 '22 17:10

kblst