Results 1 to 4 of 4

Thread: using QComboBox as an ItemView

  1. #1
    Join Date
    Dec 2006
    Posts
    9
    Thanks
    2
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11

    Default using QComboBox as an ItemView

    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.
    Attached Files Attached Files

  2. #2
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: using QComboBox as an ItemView

    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 ??

  3. #3
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: using QComboBox as an ItemView

    For an icon, make QBrushModel::data() return a pixmap for Qt::DecorationRole. QComboBox can only represent the contents of a single column, so you need to return both decoration and display roles for the same column.
    J-P Nurmi

  4. The following user says thank you to jpn for this useful post:

    EricTheFruitbat (24th January 2007)

  5. #4
    Join Date
    Dec 2006
    Posts
    9
    Thanks
    2
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11

    Default Re: using QComboBox as an ItemView

    Qt::DecorationRole was exactly what was called for.

    Thank you.

Similar Threads

  1. QDataWidgetMapper <=> QComboBox best practice
    By saknopper in forum Qt Programming
    Replies: 1
    Last Post: 18th January 2007, 10:50
  2. segmentation fault insert QString in QCombobox
    By regix in forum Qt Programming
    Replies: 16
    Last Post: 8th August 2006, 08:46
  3. Automatically add items to a QComboBox (Qt4)
    By darkadept in forum Qt Programming
    Replies: 2
    Last Post: 19th May 2006, 15:32
  4. QcomboBox items
    By therealjag in forum Newbie
    Replies: 5
    Last Post: 27th March 2006, 08:21
  5. QComboBox +SUSE10.0 +qt4.1 strange behavior
    By antonio.r.tome in forum Qt Programming
    Replies: 6
    Last Post: 20th March 2006, 17:49

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.