Important: Please read the Qt Code of Conduct -

QFontComboBox::removeItem(int) has no effect

  • I need to filter the choices in a QFontComboBox and limit the selection list to about 10 font families that are understood by the GraphViz library. I called QFontComboBox::setWritingSystem with QFontDatabase::Latin as argument but still have 154 fonts to choose from.

    Apparently, the list is not editable because calling removeItem has no effect. I also tried to get the model and called removeRow on the model, but still nothing changes.

    Is there another way except to derive my own font combo box from QComboBox? I think it shouldn't be too hard to do, but I don't enjoy "re-inventing the wheel".

  • Lifetime Qt Champion

    I wonder if a QSortFilterProxyModel could do it. ( Nope, it wont )
    The last code.
    override bool filterAcceptsRow( int source_row, const QModelIndex& source_parent )
    and skip those with wrong famlies.

  • Good idea @mrjj but wont work.
    I have no idea why but instead of adding the model as a private member to QFontComboBox they decided that casting QComboBox::model() worked just as well.

    I think this is a limitation due to bad design of QFontComboBox so I opened a bug report to get the model stored as a private member allowing, going forward, the solution above to work

  • @mrjj and @VRonin : Thank you for your suggestions. In the meantime, I have decided to implement my own font combo box which derives from the standard QComboBox class. I only had to create my own model class deriving from QAbstractListModel, and it seems to work OK for my present situation.

    The only unexpected thing was that I had to override the QAbstractListModel::match() function because it is called by QComboBox::findData(). Otherwise, it was fairly easy.

    I never really liked the QFontComboBox anyway, because most of the fonts appear too large in the list. Besides, there are numerous quirks such as when calling QFontComboBox::setCurrentFont() it doesn't set the font for the line edit control. You must call QFontComboBox::setFont() as well if you want the text of the font family to appear in the chosen font when the list is not shown.

Log in to reply