Results 1 to 8 of 8

Thread: How to center QTableView itself

  1. #1
    Join Date
    Feb 2013
    Posts
    4
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    4

    Default How to center QTableView itself

    Hello, I have five days of experience with Qt.
    I've completed my program for the most part, but there's something I just can't get to work.

    I want to place QTableView (which has 6 columns) to the middle of the window.
    I've tried layouts but that's apparently not what I want.
    I've also tried QTableView's move method but that doesn't work, either.
    Here's my latest try:

    Qt Code:
    1. model = new customTable(999, 6, this);
    2.  
    3. model->setHorizontalHeaderItem(0, new QStandardItem(tr("Manufacturer")));
    4. model->setHorizontalHeaderItem(1, new QStandardItem(tr("Model")));
    5. model->setHorizontalHeaderItem(2, new QStandardItem(tr("Type")));
    6. model->setHorizontalHeaderItem(3, new QStandardItem(tr("Available")));
    7. model->setHorizontalHeaderItem(4, new QStandardItem(tr("Misc")));
    8. model->setHorizontalHeaderItem(5, new QStandardItem(tr("Cost")));
    9.  
    10. model->table->setModel(model);
    11. model->table->horizontalHeader()->setResizeMode(QHeaderView::ResizeToContents);
    12.  
    13.  
    14. model->table->adjustSize();
    15. model->table->move(QApplication::desktop()->screen()->rect().center() - model->table->rect().center());
    16.  
    17. setCentralWidget(model->table);
    To copy to clipboard, switch view to plain text mode 

    Doesn't work... the table just isn't moved. (customTable is a class I made for my own needs - like custom SLOTs and SIGNALs. etc; it inherits from QStandardItemModel and has a public QTableView *table.)
    What do I have in mind?
    Well, I want my table to be in the middle (ala Wordpad), with a blue background.
    I've tried skimming through a lot of classes and methods, but nothing I've tried so far worked. (I also tried QHBoxLayout, but that's obviously not something I want?)

    Thank you in advance... and sorry if the solution is easy, I'm just not experienced enough with Qt.
    (I'm using Qt 4.8; C++.)
    Last edited by Sir_of_bad_coding; 3rd February 2013 at 15:36.

  2. #2
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,540
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanked 284 Times in 279 Posts

    Default Re: How to center QTableView itself

    Use horizontal layout with two horizontal spacer on both sides of table view. Like on picture.
    Attached Images Attached Images

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

    Sir_of_bad_coding (3rd February 2013)

  4. #3
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Thanks
    3
    Thanked 453 Times in 435 Posts
    Wiki edits
    15

    Default Re: How to center QTableView itself

    - Add the table to a layout (QGridLayout/QHBoxLayout/QVBoxLayout)
    - Set the required widget allignment for the table (Qt::AlignCenter/Qt::AlignHCenter/Qt::AlignVCenter)
    - Then set the layout onto a QWidget
    - Set this QWidget as the central widget of QMainWindow

    Here is how the code would look

    Qt Code:
    1. {
    2. ...
    3. model = new customTable(999, 6, this);
    4.  
    5. model->setHorizontalHeaderItem(0, new QStandardItem(tr("Manufacturer")));
    6. model->setHorizontalHeaderItem(1, new QStandardItem(tr("Model")));
    7. model->setHorizontalHeaderItem(2, new QStandardItem(tr("Type")));
    8. model->setHorizontalHeaderItem(3, new QStandardItem(tr("Available")));
    9. model->setHorizontalHeaderItem(4, new QStandardItem(tr("Misc")));
    10. model->setHorizontalHeaderItem(5, new QStandardItem(tr("Cost")));
    11.  
    12. model->table->setModel(model);
    13. model->table->horizontalHeader()->setResizeMode(QHeaderView::ResizeToContents);
    14. model->table->adjustSize();
    15.  
    16. QWidget * widget = new QWidget;
    17. QGridLayout * layout = new QGridLayout;
    18.  
    19. layout->addWidget(model->table);
    20. layout->setAlignment(table, Qt::AlignCenter);
    21.  
    22. widget->setLayout(layout);
    23.  
    24. setCentralWidget(widget);
    25. ...
    26. }
    To copy to clipboard, switch view to plain text mode 
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  5. The following user says thank you to Santosh Reddy for this useful post:

    Sir_of_bad_coding (3rd February 2013)

  6. #4
    Join Date
    Feb 2013
    Posts
    4
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    4

    Default Re: How to center QTableView itself

    Thanks, Santosh Reddy!
    There's still one little problem, though.
    http://i.imgur.com/bUP1TwG.png

    (I already figured out how to set margins.)
    But, I don't want the table to have its own sliders.
    Is there any easy way to do it or do I have to re-implement methods?
    ty

    (Man... Qt is really overwhelmingly big)

  7. #5
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Thanks
    3
    Thanked 453 Times in 435 Posts
    Wiki edits
    15

    Default Re: How to center QTableView itself

    You can turn off the scroll bars
    Qt Code:
    1. table->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    2. table->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    To copy to clipboard, switch view to plain text mode 
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  8. The following user says thank you to Santosh Reddy for this useful post:

    Sir_of_bad_coding (3rd February 2013)

  9. #6
    Join Date
    Feb 2013
    Posts
    4
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    4

    Default Re: How to center QTableView itself

    Hm thanks ... but that's still not entirely it...
    I don't want the QWidget to have the sliders but I do want the MainWindow to have sliders which would control qwidget scrolling

  10. #7
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Thanks
    3
    Thanked 453 Times in 435 Posts
    Wiki edits
    15

    Default Re: How to center QTableView itself

    MainWindow cannot have sliders, only widgets in side MainWindow can have sliders...
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  11. The following user says thank you to Santosh Reddy for this useful post:

    Sir_of_bad_coding (3rd February 2013)

  12. #8
    Join Date
    Feb 2013
    Posts
    4
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    4

    Default Re: How to center QTableView itself

    Ah thanks for clearing this up

Similar Threads

  1. QTableView checkbox center with stylesheet
    By quiet in forum Qt Programming
    Replies: 2
    Last Post: 10th March 2011, 14:52
  2. Hello Qt Center
    By Achraf-BH in forum General Discussion
    Replies: 0
    Last Post: 24th November 2009, 10:28
  3. Show in center
    By lisa in forum Qt Programming
    Replies: 3
    Last Post: 22nd November 2009, 12:42
  4. QT training center
    By Liana in forum General Discussion
    Replies: 0
    Last Post: 18th November 2009, 13:11
  5. How to center text in a QTableView
    By dougbroadwell in forum Qt Programming
    Replies: 9
    Last Post: 26th March 2009, 12:17

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
  •  
Qt is a trademark of The Qt Company.