Results 1 to 10 of 10

Thread: How to center text in a QTableView

  1. #1
    Join Date
    Dec 2007
    Posts
    16
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default How to center text in a QTableView

    Qt 4.5.0

    I'm trying to center the text in a QTableView, have been looking through the Deligate Classes, QItemDelegate Class and QStyledItemDelegate Class documentation, along with ItemData Role's and TextAlignmentRole but still cannot understand what exactly I need to do center the text in a column.

    Thanks,
    Doug

  2. #2
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to center text in a QTableView

    did you try to do this
    Qt Code:
    1. ...
    2. const QModelIndex index = table->model()->index(0, 0);
    3. table->model()->setData(index, Qt::AlignCenter, Qt::TextAlignmentRole);
    4. table->model()->setData(index, "hello world");
    5. ...
    To copy to clipboard, switch view to plain text mode 
    ?
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  3. #3
    Join Date
    Dec 2007
    Posts
    16
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to center text in a QTableView

    Nope, didn't work. Here's my code:

    Qt Code:
    1. void MainWindow::SetupDetailWidget()
    2. {
    3. // Set up the ForecastDetail (database) table Model //
    4.  
    5. DetailModel = new QSqlRelationalTableModel(this);
    6. DetailModel->setTable ("ForecastDetail");
    7. DetailModel->setSort (Detail_LineNmb_Col, Qt::AscendingOrder);
    8. DetailModel->setHeaderData(Detail_itemId_Col, Qt::Horizontal, "Item");
    9. DetailModel->setHeaderData(Detail_Qua_Col, Qt::Horizontal, "Qua");
    10.  
    11. // Set up the ForecastDetail (database) table View //
    12.  
    13. ui.tvForecastDetails->setModel(DetailModel);
    14. ui.tvForecastDetails->setColumnHidden(Detail_Agency_Col, true);
    15. ui.tvForecastDetails->setColumnHidden(Detail_LineNmb_Col, true);
    16. ui.tvForecastDetails->setSelectionMode (QAbstractItemView::SingleSelection);
    17. ui.tvForecastDetails->setSelectionBehavior(QAbstractItemView::SelectRows);
    18. ui.tvForecastDetails->setEditTriggers (QAbstractItemView::AllEditTriggers);
    19. ui.tvForecastDetails->setColumnWidth(Detail_itemId_Col, 70);
    20. ui.tvForecastDetails->setColumnWidth(Detail_Qua_Col, 50);
    21. const QModelIndex index = ui.tvForecastDetails->model()->index(0, Detail_Qua_Col);
    22. ui.tvForecastDetails->model()->setData(index, Qt::AlignCenter, Qt::TextAlignmentRole);
    23. }
    To copy to clipboard, switch view to plain text mode 

    The actual reading in if the data into the model is done elsewhere.

    Thanks

  4. #4
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to center text in a QTableView

    I would make subclass of QSqlRelationalTableModel and reimplement data method like this:
    Qt Code:
    1. QVariant MySqlRelationalModel::data ( const QModelIndex & index, int role ) const
    2. {
    3. if (role == Qt::TextAlignmentRole)
    4. return Qt::AlignCenter;
    5. return QSqlRelationalTableModel::data(index, role);
    6. }
    To copy to clipboard, switch view to plain text mode 
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

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

    Stank (11th May 2016)

  6. #5
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: How to center text in a QTableView

    Quote Originally Posted by faldżip View Post
    I would make subclass of QSqlRelationalTableModel and reimplement data
    This code will center all colums. If you only want center a specific column you have to interpret index, and then the model is the "wrong" place for that. So better use a custom delegate. But normally such an easy and normal thing should work with Qt. Have you a minimal compilable example?

  7. #6
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to center text in a QTableView

    Quote Originally Posted by Lykurg View Post
    This code will center all colums. If you only want center a specific column you have to interpret index, and then the model is the "wrong" place for that. So better use a custom delegate. But normally such an easy and normal thing should work with Qt. Have you a minimal compilable example?
    Of course, that the best way is to split data from it's presentation, and use some delegate for custom presentation, but, as there was no word about centering just one column, I thought that it would be the fastest way. :]
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

  8. #7
    Join Date
    Dec 2007
    Posts
    16
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to center text in a QTableView

    This is the simplified code I tried:

    Qt Code:
    1. void MainWindow::SetupHeaderWidget()
    2. {
    3. HeaderModel = new QSqlTableModel(this);
    4. HeaderModel->setTable ("ForecastHeader");
    5. HeaderModel->select();
    6. const QModelIndex index = HeaderModel->index(0, 0);
    7. HeaderModel->setData(index, Qt::AlignCenter, Qt::TextAlignmentRole);
    To copy to clipboard, switch view to plain text mode 

    I would think that this would not work as I want the whole column 0 to be centered, not just row 0, but even row 0 column 0 is not centered. It doesn't matter if setting the TextAlignmentRole is done before or after the select()

    Thanks
    Doug
    Last edited by dougbroadwell; 24th March 2009 at 20:09. Reason: updated contents

  9. #8
    Join Date
    Dec 2007
    Posts
    16
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to center text in a QTableView

    I tried subclassing QSqlTableModel and reimplementing data() as recommended above but that didn't work either.

  10. #9
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to center text in a QTableView

    in this case, create your own delegate and reimplement QItemDelegate::drawDisplay.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  11. #10
    Join Date
    Feb 2009
    Location
    Noida, India
    Posts
    517
    Thanks
    21
    Thanked 66 Times in 62 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to center text in a QTableView

    perhaps u can try calling setData on model before you have called setModel() on the view..that might be the issue

Similar Threads

  1. Unhandled exception in qatomic
    By NewGuy in forum Qt Programming
    Replies: 14
    Last Post: 23rd July 2013, 09:49
  2. QTableView checkbox center with stylesheet
    By quiet in forum Qt Programming
    Replies: 2
    Last Post: 10th March 2011, 13:52
  3. Replies: 1
    Last Post: 21st November 2009, 20:38
  4. Lists/multiline text in QTableView cells
    By dv_ in forum Qt Programming
    Replies: 1
    Last Post: 22nd June 2008, 23:43
  5. Text in QTableView cells not visible
    By juliarg in forum Newbie
    Replies: 2
    Last Post: 22nd March 2007, 15:49

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.