Results 1 to 3 of 3

Thread: Aligning text in QHeaderView

  1. #1
    Join Date
    Oct 2009
    Location
    South Africa
    Posts
    94
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Aligning text in QHeaderView

    I have a QTableWidget with 2 columns, with a QHeaderView.
    I left align all QTableWidgetItems in the left column, center align the right column QTableWidgetItems.
    How do I now do this aligning of text to my QHeaderView? Currently both columns are center aligned, I want the first column header to be left aligned, the right column header to be center aligned.
    This is my code for adding the QHeaderView in my class:

    Qt Code:
    1. setRowCount(0);
    2. setColumnCount(2);
    3. setColumnWidth(0, 400); //set width of 1st column, 2nd column will auto-fit to remaining space
    4.  
    5. QHeaderView *header = this->horizontalHeader();
    6. header->setSectionResizeMode(1, QHeaderView::Stretch); //let last column stretch to fit the table
    7. QStringList headerLabels;
    8. headerLabels << "Start of Stoppage" << "Duration";
    9. setHorizontalHeaderLabels(headerLabels);
    10. QFont *font = new QFont("Helvetica",20);
    11. horizontalHeader()->setFont(*font);
    To copy to clipboard, switch view to plain text mode 

    Thank you, I appreciate any help on this.

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Aligning text in QHeaderView

    Qt Code:
    1. QTableWidgetItem * pItem = pTableWidget->horizontalHeaderItem( 0 );
    2. pItem->setTextAlignment( Qt::AlignLeft | Qt::AlignVCenter );
    3.  
    4. pItem = pTableWidget->horizontalHeaderItem( 1 );
    5. pItem->setTextAlignment( Qt::AlignHCenter | Qt::AlignVCenter );
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Oct 2009
    Location
    South Africa
    Posts
    94
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Aligning text in QHeaderView

    That is 100%, thank you. Worked perfectly.

Similar Threads

  1. Replies: 3
    Last Post: 19th January 2016, 17:22
  2. QHeaderView's text rotation
    By MarkoSan in forum Qt Programming
    Replies: 5
    Last Post: 21st August 2014, 12:31
  3. Replies: 0
    Last Post: 14th January 2014, 19:44
  4. Aligning 2 plots
    By jerrychan in forum Qwt
    Replies: 2
    Last Post: 25th February 2013, 08:12
  5. Aligning a stringlist
    By impeteperry in forum Qt Programming
    Replies: 1
    Last Post: 22nd May 2006, 17:45

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.