PDA

View Full Version : Alignment of HORIZONTAL HEADERS in a QTableView



sattu
11th January 2012, 08:47
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/gallery-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

ChrisW67
11th January 2012, 09:06
QHeaderView::setDefaultAlignment() not what you want?

sattu
11th January 2012, 10:20
Hei chris, that is all i wanted. Thanks a ton for it. I did it like this:


ui->tableview->horizontalHeaderview->setDefaultAlignment(Qt::AlignLeft);

Thanks once again. :)

Spitfire
11th January 2012, 10:21
QTableWidget->horizontalHeader()->setDefaultAlignment() in case you don't know how to get the header view.

sattu
11th January 2012, 10:36
QHeaderView::setDefaultAlignment() not what you want?

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?

ChrisW67
11th January 2012, 21:56
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.

sattu
12th January 2012, 04:47
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?

ChrisW67
12th January 2012, 06:16
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?

Spitfire
12th January 2012, 09:26
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 (http://developer.qt.nokia.com/doc/qt-4.8/stylesheet-examples.html#customizing-qheaderview).

sattu
12th January 2012, 16:21
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. :)