0

I was working with a simple class derived(publicly) from QComboBox and noticed a strange behaviour of setItemData function:

a simple example:

QFont font;
font.setItalic(true);
setItemData(0, font, Qt::FontRole);

I was sure that this had to change the font of the first element in combobox to italic. But it does NOTHING. I was more surprised when this code works porperly in this combination:

setEditable(true);  //setting my derived class editable changes the situation
QFont font;
font.setItalic(true);
setItemData(0, font, Qt::FontRole);

Moreover, if I run my application(which uses my custom combobox class) giving style option from terminal f.e. -style plastique then everything works properly, without setting combobox editable.

I couldn't find anything said about this in documentation. Does anybody have idea what is the reason of all this?

Here is the source code: .h file:

class ComboBoxWrapper : public QComboBox
{
  Q_OBJECT;

public:
  ComboBoxWrapper(QWidget* parent = 0);

  const QString& getDefaultValue() const;
  void setDefaultValue(const QString& defaultValue);

private:
    QString defaultValue_;

};

And here is .cpp:

ComboBoxWrapper::ComboBoxWrapper(QWidget* parent)
  : QComboBox(parent)
{
  setAutoFillBackground(true);
}

const QString& ComboBoxWrapper::getDefaultValue() const
{
  return defaultValue_;
}

void ComboBoxWrapper::setDefaultValue(const QString& defaultValue)
{
  int index = findText(defaultValue);
  if(-1 == index)
  { /// current value is invalid.
    defaultValue_.clear();
    Q_ASSERT(false);
  }    

  defaultValue_ = defaultValue;
  QFont font = QApplication::font();
  font.setItalic(true);

  setEditable(true);  //setItemData doesn't work without this trick
  setItemData(index, font, Qt::FontRole);
  setEditable(false);
}
Karen Tsirunyan
  • 1,938
  • 6
  • 19
  • 30

0 Answers0