Results 1 to 5 of 5

Thread: Custom Model? Custom View? Custom Delegate?

  1. #1
    Join Date
    Jan 2006
    Location
    Berkeley California
    Posts
    109
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Custom Model? Custom View? Custom Delegate?

    I currently have a QSqlTableModel and a QTableView. One of the column's data represents US Dollar amounts. I would like to format it in US style: $n,nnn.nn and have it editable. It's confusing to me what I need to do to implement this; my best guess is a custom delegate (??). Since this kind of formatting must be very common, someone must have already created a delegate to do this - possibly by passing a formatting string to the delegate. Does anyone know if such a thing exists? If not, please point me in the direction to proceed.

    Thanks!

  2. #2
    Join Date
    Jul 2009
    Location
    Enschede, Netherlands
    Posts
    462
    Thanked 69 Times in 67 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Custom Model? Custom View? Custom Delegate?

    Indeed, go for the delegate.
    Horse sense is the thing that keeps horses from betting on people. --W.C. Fields

    Ask Smart Questions

  3. #3
    Join Date
    Jan 2006
    Location
    Berkeley California
    Posts
    109
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Custom Model? Custom View? Custom Delegate?

    Thanks very much!

  4. #4
    Join Date
    Jan 2006
    Location
    Berkeley California
    Posts
    109
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Custom Model? Custom View? Custom Delegate?

    Still confused. Do I subclass QAbstractItemDelegtate, QItemDelegate, or QStyleItemDelegate? Looking at the sources, it looks very complex.

    Thanks

  5. #5
    Join Date
    Jan 2006
    Location
    Berkeley California
    Posts
    109
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Custom Model? Custom View? Custom Delegate?

    Here is how I solved my problem; I subclassed QStyledItemDeleage and altered initStyleOption and displayText:

    void MoneyDelegate::initStyleOption(QStyleOptionViewIte m *option, const QModelIndex &index) const
    {
    QStyledItemDelegate::initStyleOption(option, index); // Let base handle all else.
    option->displayAlignment = Qt::AlignRight | Qt::AlignCenter; // Force Right Alignment
    }

    QString MoneyDelegate::displayText(const QVariant &value, const QLocale& locale) const
    {
    if(value.userType() == QMetaType::Float || value.userType() == QVariant:ouble)
    {
    QString text = "$" + QString().setNum(value.toDouble(), 'f', 2);
    return text;
    }
    return QStyledItemDelegate::displayText(value, locale); // Not a Double so have base function deal with it.
    }

    Now ... to figure out the Editor ...

Similar Threads

  1. Replies: 0
    Last Post: 1st February 2010, 12:00
  2. Question about custom view (or custom delegate)
    By skuda in forum Qt Programming
    Replies: 1
    Last Post: 21st September 2009, 21:06
  3. Replies: 0
    Last Post: 28th August 2009, 10:49
  4. Model/View: Custom Persistent Editor
    By No-Nonsense in forum Qt Programming
    Replies: 2
    Last Post: 7th February 2007, 15:55
  5. Replies: 1
    Last Post: 13th July 2006, 22:10

Tags for this Thread

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.