Results 1 to 3 of 3

Thread: How do I implement column width tracking between 2 QTableViews?

  1. #1
    Join Date
    Sep 2020
    Posts
    2
    Qt products
    Qt5
    Platforms
    Windows

    Question How do I implement column width tracking between 2 QTableViews?

    I have 2 QTableView widgets with the same number of columns in a QVBoxLayout such that one is on top of the other. When I change the width of a column ( via mouse drag ) in the top QTableView , I would like the same column in the bottom QTableView to resize along with it such that the column widths in the top and bottom views are the same.

    my environment:
    Qt 5.15.12
    Windows 10
    Visual Studio 2019
    C++

    Any help would be appreciated.

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,018
    Thanks
    280
    Thanked 816 Times in 805 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: How do I implement column width tracking between 2 QTableViews?

    The QTableView uses QHeaderView instances to manage the horizontal and vertical column headers. You can use the QHeaderView::sectionResized() signal to get notification when a column in one table is resized. Connect this signal to a slot where you call the QHeaderView::resizeSection() method on the other table's header and vice versa.

    I would put the code to do this in the widget that contains the two tables in the VBox. Be careful not to cause infinite recursion by calling the resizeSection() method on the same widget that issued the signal as well as not calling resizeSection() when the old and new sizes are the same. I do not know if the sectionResized() signal is sent only when the column is manually resized (ie, interactively with the mouse) or if it is also sent when the size is changed programmatically (through code). You'll have to test that, because that's where recursion could easily happen.

    You should also check out this previous post on a similar problem.

    You could have performance issues if your tables are large, because the table layout will be recalculated with every mouse move and call to resizeSection(). If this happens, you can decide to call resizeSection() only on every "nth" signal, when the size difference exceeds a certain amount, or only after a certain time has passed after the mouse has stopped moving (using a 250 ms QTimer, for example), or some combination of these to improve performance.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. #3
    Join Date
    Sep 2020
    Posts
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How do I implement column width tracking between 2 QTableViews?

    Thanks! I will give it a try tomorrow.

Similar Threads

  1. How to resize column width
    By anbu01 in forum Newbie
    Replies: 0
    Last Post: 11th August 2014, 14:05
  2. [Solved] Synchronizing column widths between QTableViews
    By Phlucious in forum Qt Programming
    Replies: 0
    Last Post: 7th March 2013, 00:17
  3. Replies: 2
    Last Post: 11th January 2011, 13:15
  4. Fixed column width in QAbstractTableModel
    By hailflex in forum Newbie
    Replies: 1
    Last Post: 11th December 2009, 06:58
  5. set Column Width in QTreeWidget?
    By vishal.chauhan in forum Qt Programming
    Replies: 1
    Last Post: 25th June 2007, 08:12

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.