PDA

View Full Version : using QComboBox as an ItemView



EricTheFruitbat
24th January 2007, 05:31
This touches heavily on program design and code extensibility.

Currently, in my application I have a subclass of QApplication which holds a list of all the available brushes in a QList<Brush> member. Other pieces of my app access with code like qApp->brushList().

The Brush class has members which hold information such as name, color, style, etc... It also has a member function that returns a 12x12 QPixmap of what it looks like ( say a red unfilled circle ). I have a Dialog that's allows the user to edit properties of the brushes.

I'm looking for a way to allow the user to select the active brush. It's relatively easy to build a QComboBox and fill it with each of the brushes in the global list. But, if the user changes any of the properties of the brushes through the brush config dialog, I'd have to regenerate the QComboBox and I'd end up losing the active brush setting.

Alternatively, I could store all the Brushes in a BrushModel (subclass of QAbstractTableModel) and then implement the active brush selector as a subclass of QComboBox. I like this solution better, because I know that other pieces of the application will be wanting knowledge about the brushes, and it wolud be nice, from a design standpoint, to all go through the Model.

Unfortunately, This Model/View stuff confuses me, and I'm not sure exactly how I'd make a QComboBox that keeps itself up to date with changes in the brush model. For example, it has to show both the brush icon ( red unfilled circle ) and the name ( "circle" ). QComboBox->setModel(), defaultly places the data from only a single column in the selection menu, whereas I need both an icon and the name.

The attached code is a slim version of my Brush class and an associated attempt at a Model with dynamic updating of a QComboBox. If someone could enlighten me about how to get a ComboBox to display both the Icon and Name of each brush, I would really appreciate it. Any other tips and comments about the overall design won't hurt me either.

aamer4yu
24th January 2007, 06:23
I am not much aware of the model view...
but was just wondering ...
can void QComboBox::addItem ( const QIcon & icon, const QString & text, const QVariant & userData = QVariant() ) be of some use to you ??

jpn
24th January 2007, 10:04
For an icon, make QBrushModel::data() return a pixmap for Qt::DecorationRole (http://doc.trolltech.com/4.2/qt.html#ItemDataRole-enum). QComboBox can only represent the contents of a single column, so you need to return both decoration and display roles for the same column.

EricTheFruitbat
24th January 2007, 17:14
Qt::DecorationRole was exactly what was called for.

Thank you.