English 中文(简体)
如何在QComboBox上制定不可选择的缺省案文?
原标题:How to set non-selectable default text on 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.

我在文件中找不到任何东西,也没有回答以前的任何问题。

最佳回答

在Combo盒式APIC中,似乎没有预料到这种情况。 但是,根据基本模式的灵活性,你似乎应当能够将-当选国-<>/em>作为第一个“合法”项目,然后保持其可选择的用途:

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

视你想要的准确行为而定,你可能希望使用<条码>。 或者,如果这只是一个不同的肤色项目,我可以回头来:

Qt, 我如何改变QComboBox一个项目的文字颜色? (C++)

(我不喜欢我点击某件事,然后被困在我可以回去的地方,即使我只举一个没有当选的国家!)

问题回答

你们可以采取的一种类似做法是,设置一个地方持有人:

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

这样,你就有一个无法选择的缺省。

“enterography

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)




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签