Results 1 to 12 of 12

Thread: QTableView moving horizontal header to bottom -- possible?

  1. #1
    Join Date
    Dec 2011
    Posts
    60
    Thanks
    12
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default QTableView moving horizontal header to bottom -- possible?

    Hi all,

    I have a QTableView and would like to have the headers on the LEFT and BOTTOM, such as you would see in an x/y plot.

    I've looked through the various APIs and haven't found anything that seems to make this option available...

    Does anyone know if this is possible?

  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: QTableView moving horizontal header to bottom -- possible?

    Sure. Use QAbstractScrollArea::setViewportMargins() to make some space below the viewport and reimplement resizeEvent to position the headers where you want them.
    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. #3
    Join Date
    Dec 2011
    Posts
    60
    Thanks
    12
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QTableView moving horizontal header to bottom -- possible?

    Sure. Use QAbstractScrollArea::setViewportMargins() to make some space below the viewport and reimplement resizeEvent to position the headers where you want them.
    Hi wysota,

    Thank you. I see how to set the viewport margins to create some space, but can you provide a little more detail on how to "position" the headers?

    Does positioning the headers mean:

    1.) Hiding the current headers
    2.) Creating new headers
    3.) Putting the new headers into a custom layout to fill the viewport space?

    If that's the process, how will the new headers align with the cell edges?

    Or, is there a simpler way?

    Thank you again.

  4. #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: QTableView moving horizontal header to bottom -- possible?

    Positioning the headers mean using setGeometry() calls on existing headers to change their position and size to fit your needs.
    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.


  5. #5
    Join Date
    Dec 2011
    Posts
    60
    Thanks
    12
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QTableView moving horizontal header to bottom -- possible?

    Quote Originally Posted by wysota View Post
    Positioning the headers mean using setGeometry() calls on existing headers to change their position and size to fit your needs.
    Thanks wysota.

    Inside my QTableView class, I'm trying:

    Qt Code:
    1. void PlotTableView::resizeEvent(QResizeEvent *event)
    2. {
    3. QRect horizGeo = horizontalHeader()->geometry();
    4. QRect vertGeo = verticalHeader()->geometry();
    5. // Set the viewport margins for the left & bottom
    6. setViewportMargins(vertGeo.width(), 0, 0, horizGeo.height());
    7.  
    8. autoSizeCells();
    9.  
    10. horizontalHeader()->setGeometry(QRect(200,0,horizGeo.width(),horizGeo.height()));
    11.  
    12. QTableView::resizeEvent(event);
    13. }
    To copy to clipboard, switch view to plain text mode 
    With this, I don't see the horizontal header move at all. In the case above, I was just trying to slide it right by 200px.

    Does the header view need to be subclassed for this to work??
    Last edited by alketi; 15th October 2014 at 23:27.

  6. #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: QTableView moving horizontal header to bottom -- possible?

    You are calling the base class implementation thus it overrides whatever positioning you did yourself. If you change the order of calls then it should be fine.
    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.


  7. #7
    Join Date
    Dec 2011
    Posts
    60
    Thanks
    12
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QTableView moving horizontal header to bottom -- possible?

    wysota, thank you. Re-ordering the calls did work, though there are still issues.

    The horizontal header is at the bottom, but as I resize the TableView (it's in a splitter), most of the time the horizontal headers stays put, but sometimes it back to the top of the viewport, and sometimes disappears altogether.

    I suppose there are some other issues at play, but I have not figured out how to solve them yet.

  8. #8
    Join Date
    Dec 2011
    Posts
    60
    Thanks
    12
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QTableView moving horizontal header to bottom -- possible?

    wysota, I could really use a pointer in the right direction

    My setup is the following:

    1. Table view is inside of a horizontal/vertical splitter
    2. Table view setup includes both resizeColumnsToContents() and resizeRowsToContents()

    The behavior I'm seeing:

    1. When I drag the splitter, the horizontal header jumps to the bottom and mostly stays there (sometimes flickering between the bottom and the top).
    2. When I stop dragging the splitter, the horizontal header jumps back to the top.

    My code:

    Qt Code:
    1. void PlotTableView::resizeEvent(QResizeEvent *event)
    2. {
    3. QTableView::resizeEvent(event);
    4.  
    5. QRect horizGeo = horizontalHeader()->geometry();
    6. QRect vertGeo = verticalHeader()->geometry();
    7.  
    8. // Set the viewport margins for the left & bottom
    9. setViewportMargins(
    10. vertGeo.width(),
    11. horizGeo.height(),
    12. 0,
    13. horizGeo.height());
    14.  
    15. // Move the horizontal header to the bottom
    16. horizontalHeader()->setGeometry(QRect(
    17. vertGeo.width(),
    18. this->viewport()->geometry().height() + horizGeo.height(),
    19. horizGeo.width(),
    20. horizGeo.height()));
    21.  
    22. }
    To copy to clipboard, switch view to plain text mode 

    The culprit in question is QTableView::resizeEvent() -- which if commented out, results in the horizontal header staying nicely at the bottom. However, without it, the cells do not resize to fill the available space.

    Any ideas what is missing to keep the header at the bottom, and not have it flicker and jump back to the top when the resize event finishes?

  9. #9
    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: QTableView moving horizontal header to bottom -- possible?

    It would help if you provided a minimal compilable example reproducing the problem.
    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.


  10. #10
    Join Date
    Dec 2011
    Posts
    60
    Thanks
    12
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QTableView moving horizontal header to bottom -- possible?

    wysota, the problem of not being able to move the horizontal header to the bottom is caused by:

    Qt Code:
    1. view.verticalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    2. view.horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    To copy to clipboard, switch view to plain text mode 
    - I want the rows/columns to both automatically fill the space, and I also want the horizontal header on the bottom.
    - Do I need to manually implement the "setSectionResizeMode(QHeaderView::Stretch)" code in my application?

    Here is the full duplication to see the issue (drag the window larger and smaller to see the flicker):
    Qt Code:
    1. #include <QtWidgets>
    2. #include <QTableView>
    3.  
    4. class PlotTableView : public QTableView
    5. {
    6.  
    7. public:
    8. PlotTableView(QWidget *parent = NULL) : QTableView(parent) { };
    9. virtual ~PlotTableView() {}
    10.  
    11. protected:
    12. virtual void resizeEvent(QResizeEvent *event)
    13. {
    14. QTableView::resizeEvent(event);
    15.  
    16. QRect verticalHeaderRect = verticalHeader()->geometry();
    17. QRect horizontalHeaderRect = horizontalHeader()->geometry();
    18.  
    19. setViewportMargins(
    20. verticalHeaderRect.width(), // left
    21. horizontalHeaderRect.height(), // top
    22. 0, // right
    23. horizontalHeaderRect.height()); // bottom
    24.  
    25. // Move the horizontal header to the bottom
    26. horizontalHeader()->setGeometry(QRect(
    27. verticalHeaderRect.width(),
    28. this->viewport()->geometry().height() + horizontalHeaderRect.height(),
    29. horizontalHeaderRect.width(),
    30. horizontalHeaderRect.height()));
    31. }
    32. };
    33.  
    34. int main(int argc, char **argv) {
    35. QApplication app(argc, argv);
    36.  
    37. QStandardItemModel model(4,4);
    38.  
    39. QMainWindow *window = new QMainWindow();
    40. QWidget *topWidget = new QWidget(window);
    41. QWidget *bottomRightWidget = new QWidget(window);
    42. bottomRightWidget->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
    43.  
    44. QWidget * q = new QWidget();
    45. window->setCentralWidget(q);
    46.  
    47. PlotTableView view;
    48. view.setModel(&model);
    49. view.resizeColumnsToContents();
    50. view.resizeRowsToContents();
    51. view.setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
    52.  
    53. // These two lines cause the horizontalHeader()->setGeometry request to flicker and fail
    54. view.verticalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    55. view.horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    56.  
    57. QHBoxLayout *hBox = new QHBoxLayout();
    58. hBox->addWidget(&view);
    59. hBox->addWidget(bottomRightWidget);
    60.  
    61. QSplitter *hSplitter = new QSplitter(Qt::Horizontal);
    62. hSplitter->addWidget(&view);
    63. hSplitter->addWidget(bottomRightWidget);
    64.  
    65. QList <int> sizes;
    66. sizes << 1 << 1;
    67. hSplitter->setSizes(sizes);
    68.  
    69. QVBoxLayout *vBox = new QVBoxLayout();
    70. vBox->addWidget(topWidget);
    71. vBox->addWidget(hSplitter);
    72.  
    73. q->setLayout(vBox);
    74.  
    75. window->show();
    76.  
    77. return app.exec();
    78. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by alketi; 8th May 2015 at 23:07.

  11. #11
    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: QTableView moving horizontal header to bottom -- possible?

    Are you sure this value:

    Qt Code:
    1. this->viewport()->geometry().height() + horizontalHeaderRect.height()
    To copy to clipboard, switch view to plain text mode 

    is correct for the top edge of the horizontal header? Shouldn't it be - instead of +?
    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.


  12. #12
    Join Date
    Dec 2011
    Posts
    60
    Thanks
    12
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QTableView moving horizontal header to bottom -- possible?

    No, that's correct as is...

    The horizontal header wants to move to the right by the width of the vertical header.
    The horizontal header wants to move down by the height of the viewport + the height of the horizontal header (so it's below the viewport).

    But, regardless, that's not the issue -- the issue is that horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch) apparently fights the desire to move the header.

    You can run the the complete example provided to see the behavior.

    Thank you for any assistance.

Similar Threads

  1. Replies: 21
    Last Post: 13th August 2013, 13:38
  2. Replies: 8
    Last Post: 17th July 2012, 21:20
  3. QTableView Horizontal Header's Width
    By araglin in forum Newbie
    Replies: 3
    Last Post: 21st December 2008, 09:54
  4. How to set QTableView width to width of horizontal header?
    By martinb0820 in forum Qt Programming
    Replies: 0
    Last Post: 2nd December 2008, 21:51
  5. How to customize horizontal header (diagonal header view)
    By vairamuthu.g in forum Qt Programming
    Replies: 4
    Last Post: 4th September 2008, 16:59

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.