Results 1 to 10 of 10

Thread: Alignment of HORIZONTAL HEADERS in a QTableView

  1. #1
    Join Date
    Sep 2010
    Location
    Bangalore
    Posts
    169
    Thanks
    59
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Alignment of HORIZONTAL HEADERS in a QTableView

    Hi everyone,

    Whenever I am setting a QSqlQueryModel in a QTableView, the column names by default are CENTER-ALIGNED. Like, suppose I have an user table with fields UserID, Name, and Address. Then this field names in the header section are by default center-aligned. But i want to make them left aligned.
    For example from google i found out this link: http://doc.qt.nokia.com/4.7-snapshot...plastique.html .
    In that link the tableview's style is set to "Plastique" and we can clearly see that the horizontal headers are left aligned. Though i tried some codes, but still I just am not able to figure out how to do it from my code.

    I would really appreciate any help that i would get regarding this.

    -sattu

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Alignment of HORIZONTAL HEADERS in a QTableView


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

    sattu (11th January 2012)

  4. #3
    Join Date
    Sep 2010
    Location
    Bangalore
    Posts
    169
    Thanks
    59
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Alignment of HORIZONTAL HEADERS in a QTableView

    Hei chris, that is all i wanted. Thanks a ton for it. I did it like this:

    Qt Code:
    1. ui->tableview->horizontalHeaderview->setDefaultAlignment(Qt::AlignLeft);
    To copy to clipboard, switch view to plain text mode 

    Thanks once again.

  5. #4
    Join Date
    Sep 2011
    Location
    Manchester
    Posts
    538
    Thanks
    3
    Thanked 106 Times in 103 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Alignment of HORIZONTAL HEADERS in a QTableView

    QTableWidget->horizontalHeader()->setDefaultAlignment() in case you don't know how to get the header view.

  6. #5
    Join Date
    Sep 2010
    Location
    Bangalore
    Posts
    169
    Thanks
    59
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Alignment of HORIZONTAL HEADERS in a QTableView

    Quote Originally Posted by ChrisW67 View Post
    One more thing Chris. As i mentioned above, now i am able to change the alignment from my code. But is the same thing possible using the style-sheet of the table view?
    I mean, in the stylesheet i mentioned the following properties:
    "alignment: left;" or "default-align: left;" or "horizontal align: left;" . But none of them working. So do you know the correct statement to be put in the stylesheet?

  7. #6
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Alignment of HORIZONTAL HEADERS in a QTableView

    If it is going to work (and I don't know... I haven't tried) I expect it would be the style sheet of the QHeaderView, not the QTableView, that you want to change.

  8. #7
    Join Date
    Sep 2010
    Location
    Bangalore
    Posts
    169
    Thanks
    59
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Alignment of HORIZONTAL HEADERS in a QTableView

    Quote Originally Posted by ChrisW67 View Post
    If it is going to work (and I don't know... I haven't tried) I expect it would be the style sheet of the QHeaderView, not the QTableView, that you want to change.
    Well, actually i meant changing the style sheet from the Gui rather than the code. Because from the code anyway i am able to do it (as per the hint given by you). From the Gui i drag and drop a Table View on the widget. Then i right click on the tableview and click "Change Stylesheet". So, this way i cant separately do it for QHeaderView, right?

  9. #8
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Alignment of HORIZONTAL HEADERS in a QTableView

    If you attach a style sheet to the table view the QHeaderView, as a child widget, should inherit it. All you need to do is target the header view in the style sheet.
    Why don't you try it?

  10. #9
    Join Date
    Sep 2011
    Location
    Manchester
    Posts
    538
    Thanks
    3
    Thanked 106 Times in 103 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Alignment of HORIZONTAL HEADERS in a QTableView

    You can change style of QHeaderView from Designer.

    Edit stylesheet of the table and use QHeaderView { background-color:red; } to manipulate row and column headres are on, or QHeaderView:section { background-color:blue; } to manilulate header elements.
    Unfortunatley as far as I know you can't change text alignment of QHeaderView's text using stylesheets.

    More info here.

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

    sattu (12th January 2012)

  12. #10
    Join Date
    Sep 2010
    Location
    Bangalore
    Posts
    169
    Thanks
    59
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Alignment of HORIZONTAL HEADERS in a QTableView

    Thanks Chris and Spitfire,

    Today i tried that way by giving QHeaderView {}. Whatever i gave regarding the spacing or colour or font, it worked. But whatever i gave for alignment, it didn't work. If sometime, any of you happen to do it from stylesheet, then please do inform me. For the moment it's fine as i am able to do it from code.

    Thanks once again.

Similar Threads

  1. QTableView Alignment
    By AlexSudnik in forum Newbie
    Replies: 2
    Last Post: 22nd January 2011, 12:12
  2. Replies: 0
    Last Post: 5th July 2010, 09:05
  3. Grid QTableView horizontal only
    By estanisgeyer in forum Qt Programming
    Replies: 1
    Last Post: 11th September 2009, 16:14
  4. QTextTable horizontal alignment of a cell
    By hafwil in forum Qt Programming
    Replies: 5
    Last Post: 14th October 2008, 14:26
  5. Replies: 4
    Last Post: 27th September 2006, 13: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.