[Qt-interest] How to disable combo box list items?

J-P Nurmi jpnurmi at gmail.com
Mon Dec 29 21:16:09 CET 2008


On Mon, Dec 29, 2008 at 9:36 PM, Robert Hairgrove <evorgriahr at hispeed.ch>wrote:

> What would be the easiest way of graying out list items similar to when
> a menu item is disabled? I have a configuration dialog with stacked
> widgets selected by a combo box (a QComboBox created in QtDesigner).
>
> In this particular case, I am offering database support for a limited
> number of interfaces: MySQL, MSAccess, ODBC and SQLite. The idea would
> be to keep the same code base accross different platforms, but maybe
> disable MSAccess support on Linux.
>
> Looks like there is no built-in way to do this? I would really like to
> avoid subclassing the combo box, item delegates etc. if possible (LOTS
> of work involved there...).


QComboBox uses QStandardItemModel by default. The following snippet would
disable the current item:

QStandardItemModel* model =
qobject_cast<QStandardItemModel*>(comboBox->model());
if (model) {
    QModelIndex index = model->index(comboBox->currentIndex(),
comboBox->modelColumn(), comboBox->rootModelIndex());
    QStandardItem* item = model->itemFromIndex(index);
    if (item) {
        item->setEnabled(false);
    }
}

--
J-P Nurmi
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20081229/2093b00c/attachment.html 


More information about the Qt-interest-old mailing list