Results 1 to 7 of 7

Thread: set the width of the columns of QTableView

  1. #1
    Join Date
    Jul 2008
    Posts
    6
    Thanks
    1

    Question set the width of the columns of QTableView

    Hi,

    I am a new bee of QT.

    I try to use QTableView and QAbstractTableModel. I created my own TableModel and called ui.tableView->setModel. Everything looks fine.

    But I find that I cannot set the width of column from my TableModel.

    I found the function, QTableView::setColumnWidth. But I don't know when I am able to call it. Because I only return the string from Model, I don't know when the framework will create the columns. But I need set width of the column after it is created.

    Anybody knows what I must do? Thanks a lot.

    Best Regards,
    Michael zhang

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: set the width of the columns of QTableView

    How about using the SizeHint role in the model?

  3. #3
    Join Date
    Jul 2008
    Posts
    6
    Thanks
    1

    Default Re: set the width of the columns of QTableView

    Hi Jacek,

    First of all, thanks for you quick reply.

    I added it Qt::SizeHintRole into the code. But looks like it only be able to change the height of header but not width.

    following is my code, Do I do anything not correct? or any other suggestion?

    Qt Code:
    1. QVariant TableModel::headerData(int section, Qt::Orientation orientation,int role) const
    2. {
    3. if (orientation == Qt::Horizontal)
    4. {
    5. if (section < _columnInfos.size())
    6. {
    7. if (role == Qt::DisplayRole)
    8. {
    9. return QVariant(_columnInfos[section]->GetName());
    10. }
    11. else if(role == Qt::SizeHintRole)
    12. {
    13. QSize size(100, 50);
    14. if (section == 0)
    15. {
    16. size.setWidth(200);
    17. }
    18.  
    19. return QVariant(size);
    20. }
    21. }
    22. }
    23.  
    24. return QVariant();
    25. }
    To copy to clipboard, switch view to plain text mode 

    Regards,
    Michael zhang
    Last edited by jpn; 29th July 2008 at 22:01. Reason: missing [code] tags

  4. #4
    Join Date
    Apr 2008
    Posts
    26
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: set the width of the columns of QTableView

    I generally do this by extending the table view and modifying the resizeEvent method;

    Qt Code:
    1. # Python code ...
    2. class MyTable(QTableView):
    3.  
    4. def __init__(self, model, parent = None):
    5. super(MyTable, self).__init__(parent)
    6.  
    7. rowHeight = self.fontMetrics().height()
    8. self.verticalHeader().setDefaultSectionSize(rowHeight)
    9. self.setModel(model)
    10.  
    11. def resizeEvent(self, event):
    12. width = event.size().width()
    13. self.setColumnWidth(1, width * 0.25) # 25% Width Column
    14. self.setColumnWidth(2, width * 0.75) # 75% Width Column
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Jul 2008
    Posts
    6
    Thanks
    1

    Default Re: set the width of the columns of QTableView

    Hi,

    Thanks for your reply.

    I am not really know Python. But looks like you means I must create my own view class. And override the funciton resizeEvent.

    But if I do so, how can I let Visual Studio to know this view need use my view class but not the QTableView?

    And is it sure that when resizeEvent called, the column has been created?

    Best Regards,
    Michael zhang

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: set the width of the columns of QTableView

    If you change header's resize mode to QHeaderView::ResizeToContents, it should work.

    Quote Originally Posted by zhanglr View Post
    But if I do so, how can I let Visual Studio to know this view need use my view class but not the QTableView?
    In Qt Designer you can use promotion mechanism.

    Quote Originally Posted by zhanglr View Post
    And is it sure that when resizeEvent called, the column has been created?
    You can always check how many columns exist in resizeEvent().

  7. The following user says thank you to jacek for this useful post:

    zhanglr (31st July 2008)

  8. #7
    Join Date
    Jul 2008
    Posts
    6
    Thanks
    1

    Default Re: set the width of the columns of QTableView

    Hi jacek,

    Thanks a lot.

    Best Regards
    Michael zhang

Similar Threads

  1. Width QTableView for QCombobox with multiple columns
    By visor_ua in forum Qt Programming
    Replies: 7
    Last Post: 21st June 2011, 10:54
  2. autoexpanding QTableView columns
    By ucntcme in forum Qt Programming
    Replies: 6
    Last Post: 11th March 2008, 09:28
  3. Ensure columns in qtableview uses all available space
    By oysteinpettersen in forum Qt Programming
    Replies: 2
    Last Post: 10th January 2008, 11:28
  4. QTableView - Enable Clicking for some columns only
    By Gabriel in forum Qt Programming
    Replies: 1
    Last Post: 23rd June 2006, 22:42
  5. How to obtain the width of a QTableWidget?
    By Giel Peters in forum Qt Programming
    Replies: 3
    Last Post: 9th January 2006, 22:34

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.