Results 1 to 2 of 2

Thread: QTableView: horizontal header "resize to contents", want resize to edit widget

  1. #1
    Join Date
    Dec 2009
    Posts
    52
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default QTableView: horizontal header "resize to contents", want resize to edit widget

    Hello,

    I have a class derived from QTableView, the constructor of which is:
    Qt Code:
    1. my_table_view::my_table_view(QWidget* parent)
    2. : QTableView(parent)
    3. {
    4. setSelectionMode(QAbstractItemView::SingleSelection);
    5. setModel(new my_model(this));
    6. setItemDelegate(new my_delegate(this));
    7. verticalHeader()->setResizeMode(QHeaderView::ResizeToContents);
    8. horizontalHeader()->setResizeMode(QHeaderView::ResizeToContents);
    9. }
    To copy to clipboard, switch view to plain text mode 

    For column 1, my_model returns a QString
    Qt Code:
    1. QString pslist;
    2. for (...) {
    3. pslist += (i->first.c_str());
    4. pslist += '=';
    5. pslist += (i->second.c_str());
    6. pslist += '\n';
    7. }
    8. return pslist;
    To copy to clipboard, switch view to plain text mode 
    which yields the cell containing a list of strings.
    That cell/columnl is one with the most height out of the 3 columns in the table.

    Double clicking on that cell, my delegate creates an editor:

    Qt Code:
    1. QWidget* my_delegate::createEditor(QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index) const
    2. {
    3. const int row = index.row();
    4. if (row<0)
    5. return 0;
    6.  
    7. switch(index.column()) {
    8. ///
    9. case 1: {
    10. QWidget* widget = ;////
    11. widget->setAutoFillBackground(true);
    12. return widget;
    13. }
    14. ///
    15. default:
    16. return 0;
    17. };
    18. }
    To copy to clipboard, switch view to plain text mode 

    This returned widget is trivial. It has a QGridLayout taking care of 5 QLabels in front of 5 QLineEdits.
    When I double click on the cell, the widget is created and returned.

    The tableview ctor set the resizing mode of the horizontal header to resize to contents.
    Because the row was sized according to the display widget (coming from QString), the edit widget (with the QLabels,QLineEdits) doesn't display nicely.

    Can I resize the row of the table view after the delegate has created the widget to edit the cell ,so that the row holds enough space to display the edit widget properly?

    Regards,

  2. #2
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: QTableView: horizontal header "resize to contents", want resize to edit widget

    Untested but here's something you might try:
    Emit a signal in createEditor(). Connect it to a slot in my_table_view that resizes the row and/or column so the editor widget fits.
    Then to change things back emit a signal in setModelData() that is connected to another my_table_view slot that calls resizeRowsToContents() and/or resizeColumnsToContents().

Similar Threads

  1. Replies: 4
    Last Post: 18th April 2010, 01:37
  2. Replies: 0
    Last Post: 28th May 2009, 23:38
  3. Replies: 8
    Last Post: 5th August 2008, 13:09
  4. Translation QFileDialog standart buttons ("Open"/"Save"/"Cancel")
    By victor.yacovlev in forum Qt Programming
    Replies: 4
    Last Post: 24th January 2008, 20:05
  5. How to make "west" tabs horizontal?
    By plumbum in forum Qt Programming
    Replies: 2
    Last Post: 7th June 2007, 11:32

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.