Results 1 to 10 of 10

Thread: delegate sizeHint not getting called

  1. #1
    Join Date
    Oct 2007
    Posts
    65
    Thanks
    15
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default delegate sizeHint not getting called

    Hi,
    i have created a pretty simple delegate to fix the size of the last column that get too big when i call setStretchLastSection(true) in the header (curious that if i don't call this method the column get the correct size and have a good margin until the end of the table), i have read the related post http://www.qtcentre.org/threads/1885...ustom-delegate but i can't find the problem in my code.

    The delegate it is:
    Qt Code:
    1. class LineasDelegate : public QStyledItemDelegate
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. LineasDelegate(QObject *parent = 0) : QStyledItemDelegate(parent) {};
    7. QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const;
    8.  
    9. QSize LineasDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
    10. {
    11. std::cout << "sizeHint method delegate" << std::endl;
    12. }
    To copy to clipboard, switch view to plain text mode 

    I create it and attach to the TableView with this code:

    Qt Code:
    1. ui->tableView->setAlternatingRowColors(true);
    2. ui->tableView->verticalHeader()->setDefaultSectionSize(ui->tableView->fontMetrics().lineSpacing() + 8);
    3.  
    4. lineasDelegate = new LineasDelegate();
    5. ui->tableView->setItemDelegate(lineasDelegate);
    To copy to clipboard, switch view to plain text mode 

    And after attach my custom model i call ui->tableView->resizeColumnsToContents(), still the method get not called ever, i have tried to add too at the data method of my custom model:

    Qt Code:
    1. if (role == Qt::SizeHintRole) {
    2. std::cout << "data method sizeHintRole" << std::endl;
    3. }
    To copy to clipboard, switch view to plain text mode 

    That does not get called either. The only way i have found to set the size of the column it is using ui->tableView->horizontalHeader()->resizeSection(section, size) but i would prefer to use the delegate.

    The delegate it is the correct and works because i have in the delegate implemented createEditor too and works ok (I have not posted the method because i don't think should be related).

    Thanks.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: delegate sizeHint not getting called

    If you have the last column set to stretch, the sizeHint is totally ignored because the size of the element is calculated upon the header size and not the element size. Seems you are approaching the problem from a wrong end. What exactly are you trying to achieve?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


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

    Dutchman (25th January 2012)

  4. #3
    Join Date
    Oct 2007
    Posts
    65
    Thanks
    15
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: delegate sizeHint not getting called

    I would like to have the last column stretched to get a table without gaps in the header and without horizontal scrollbar and have the columns resized correctly with his contents, I think that was possible in Qt 4.5 with the steps i have followed above, but i think they have "fixed" this in Qt 4.6 and above, what would be the correct way to achieve it?

  5. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: delegate sizeHint not getting called

    So why don't you just use QHeaderView::setResizeMode() to set the resize mode of all but the last column to ResizeToContents and the last column to Stretch?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  6. The following user says thank you to wysota for this useful post:

    skuda (16th March 2011)

  7. #5
    Join Date
    Oct 2007
    Posts
    65
    Thanks
    15
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: delegate sizeHint not getting called

    I will try it and post here the results but my original problem it is that the header size of the last column it is calculated badly when i use SetStretchLastSection(True), all other columns have a default ok size but the last one get a with about three or four times the needed for his header text and force a horizontal scrollbar.

  8. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: delegate sizeHint not getting called

    If you stretch the last column then it takes all the remaining space by definition. It doesn't take into consideration the width of the data in the item.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. #7
    Join Date
    Oct 2007
    Posts
    65
    Thanks
    15
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: delegate sizeHint not getting called

    It goes out of the remaining space (nearly the half of the last column goes out of the viewport of the tableview) and force a horizontal scrollbar, don't seem a good behavior for me.

  10. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: delegate sizeHint not getting called

    Quote Originally Posted by skuda View Post
    It goes out of the remaining space (nearly the half of the last column goes out of the viewport of the tableview) and force a horizontal scrollbar, don't seem a good behavior for me.
    It shouldn't be happening like that. You must have done something to the other columns.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  11. #9
    Join Date
    Oct 2007
    Posts
    65
    Thanks
    15
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: delegate sizeHint not getting called

    Here you have photos taken in every case

    without use setStretchLastSection(true):
    not_strecth.png

    using setStretchLastSection(true):
    stretch.png

    using setStretchLastSection(true) and later resizeSection to fix the size:
    stretch_and_resize.png

    I am not doing anything special to the columns, maybe the problem it is that i have set a maximumWith of 320 to the mainwindow.
    Last edited by skuda; 16th March 2011 at 12:38. Reason: attach photos.

  12. #10
    Join Date
    Oct 2007
    Posts
    65
    Thanks
    15
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: delegate sizeHint not getting called

    I don't find anything strange but anyway i have found a way to fix it using resizeSection after the stretch so i think it is solved (more or less).

    Thanks wysota.

Similar Threads

  1. Replies: 15
    Last Post: 11th December 2012, 21:10
  2. Replies: 2
    Last Post: 18th October 2010, 22:44
  3. createEditor() is not called in custom delegate
    By landstreicher in forum Qt Programming
    Replies: 3
    Last Post: 22nd July 2010, 04:41
  4. sizeHint() called for invisible rows
    By markusho in forum Qt Programming
    Replies: 2
    Last Post: 10th March 2010, 14:58
  5. QTableWidget sizeHint Not Called
    By mbrusati in forum Qt Programming
    Replies: 0
    Last Post: 27th September 2008, 15:53

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.