May 30, 2011

ShawnG ShawnG
Lab Rat
14 posts

Alignment of Table Horizontal Headers

 

Hello,
I would like to set the alignment for the horizontal header differently for each column, but it may not be the same as the items in their corresponding column. Help?
Thanks

5 replies

May 30, 2011

peppe peppe
Ant Farmer
1029 posts

Just return the proper alignment from your QAbstractItemModel::headerData implementation for the TextAlignmentRole.

 Signature 

Software Engineer
KDAB (UK) Ltd., a KDAB Group company

May 31, 2011

ShawnG ShawnG
Lab Rat
14 posts

Being a Qt newbie, could you point me in the correct direction of some sample code to review? Thanks!

May 31, 2011

Gerolf Gerolf
Hobby Entomologist
3287 posts

You can read the docu of QAbstractItemModel::headerData. Afaik, there is no official example, doing this.

 Signature 

Nokia Certified Qt Specialist.
Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

May 31, 2011

Eddy Eddy
Area 51 Engineer
1612 posts

you could use something like :

  1. QVariant TableModel::headerData ( int section, Qt::Orientation orientation, int role ) const
  2. {
  3. ...other stuff
  4.  if ( orientation == Qt::Horizontal )
  5.    {
  6. ...
  7.  
  8. if(role == Qt::TextAlignmentRole ){
  9.  
  10.                if(col == 1){
  11.                   return Qt::AlignRight;
  12.                }
  13.                else{
  14.                   return Qt::AlignLeft;
  15.                }
  16.  
  17.    return QVariant();
  18. }

hope this gives you some directions

 Signature 

Moderator
Qt Certified Specialist
Nokia Qt Ambassador

May 31, 2011

Andre Andre
Robot Herder
6420 posts

@ShawnG:

Could you tell us what model and what view you are using? Are you using a QStandardItemModel with a QTableView, or a QTableWidget? Or something else?

 
  ‹‹ How to work with projector and camera?      Added QMenuBar in QDialog it showing but sub menus dosn’t open ››

You must log in to post a reply. Not a member yet? Register here!