Results 1 to 2 of 2

Thread: QTableView column to take size of delegate editor

  1. #1
    Join Date
    Apr 2010
    Posts
    27
    Thanks
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QTableView column to take size of delegate editor

    Hey everyone,

    This is my first post here, I'm pretty new to Qt and I'd appreciate your help, thanks.

    I've got a QTableView which I have stuck the example SpinBox delegate in. My problem is the when I edit a value in the table, the spinBox is has a small width due to my column heading labels being short. As a result, the values in the spinBox editor become difficult to read (seeing only one character at a time).

    I attempted to use editor->setMinimumWidth(45); within the delegate constructor, which does change the size of the delegate editor when it is opened. Unfortunately the column width seems to have been determined based on the display delegate, meaning it overlaps the column to its right.

    I'd appreciate it if anyone could let me know if there is an easy way to base the column's width on the size of the delegate editor not the delegate display. (not sure if my terminology is correct there)

    For the set up of the table, I'm using resizeColumnsToContents() as below.

    Qt Code:
    1. QTableView *table = new QTableView;
    2. table->setModel(chanSetModel);
    3. table->setItemDelegateForColumn(4, new SpinBoxDelegate);
    4. table->resizeColumnsToContents();
    To copy to clipboard, switch view to plain text mode 

    ... and this is my delegate constructor.

    Qt Code:
    1. QWidget *SpinBoxDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &/* option */, const QModelIndex &/* index */) const
    2. {
    3. QSpinBox *editor = new QSpinBox(parent);
    4. editor->setMinimum(0);
    5. editor->setMaximum(63);
    6. editor->setMinimumWidth(45);
    7.  
    8. return editor;
    9. }
    To copy to clipboard, switch view to plain text mode 

    Thanks in advance for your help!

    - Michael

  2. #2
    Join Date
    Apr 2010
    Posts
    27
    Thanks
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTableView column to take size of delegate editor

    For anybody who might face the same problem, my solution involved creating a reimplementation of the sizeHint function. I also removed the setMinimumWidth(45) line as it was no longer needed.

    Qt Code:
    1. QSize SpinBoxDelegate::sizeHint(const QStyleOptionViewItem & /*option*/, const QModelIndex &/*index*/ ) const
    2. {
    3. return QSize(45,10);
    4. }
    To copy to clipboard, switch view to plain text mode 

    Instead of using constants for values for this it would be to base the size on the font metrics I suppose, and probably use a monospaced font.

  3. The following user says thank you to Cotlone for this useful post:

    TheRonin (28th June 2013)

Similar Threads

  1. Item Delegate Editor Size
    By QAlex in forum Qt Programming
    Replies: 0
    Last Post: 19th April 2010, 16:43
  2. Item Delegate editor background
    By LynneV in forum Qt Programming
    Replies: 2
    Last Post: 16th March 2010, 21:33
  3. Delegate and editor hide event
    By zuck in forum Qt Programming
    Replies: 1
    Last Post: 1st November 2009, 17:59
  4. Delegate editor sizing problem
    By smacchia in forum Qt Programming
    Replies: 19
    Last Post: 7th December 2007, 16:27
  5. No delegate for 1 column in QTreeView
    By mace in forum Qt Programming
    Replies: 1
    Last Post: 15th February 2007, 10:55

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.