Solved 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
withQFontDatabase::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 calledremoveRow
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". -
Hi
I wonder if a QSortFilterProxyModel could do it. ( Nope, it wont )
https://stackoverflow.com/questions/22454264/how-to-filter-the-data-of-qcombobox
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 toQFontComboBox
they decided that castingQComboBox::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 fromQAbstractListModel
, 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 byQComboBox::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 callingQFontComboBox::setCurrentFont()
it doesn't set the font for the line edit control. You must callQFontComboBox::setFont()
as well if you want the text of the font family to appear in the chosen font when the list is not shown.