Results 1 to 4 of 4

Thread: Centering An Icon In A QTableWidgetItem?

  1. #1
    Join Date
    Aug 2006
    Location
    Madison, WI USA
    Posts
    153
    Thanks
    35
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Question Centering An Icon In A QTableWidgetItem?

    My application (using 4.2.2) has a QTableWidget in which one of the cells displays an icon. This cell contains no text or other widgets. The icon is left-justified but would look better if it were centered.

    What must I do to center the icon in the QTableWidgetItem?

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Centering An Icon In A QTableWidgetItem?

    First thing to try would be to set Qt::TextAlignmentRole for the item to Qt::AlignCenter.

  3. #3
    Join Date
    Aug 2006
    Location
    Madison, WI USA
    Posts
    153
    Thanks
    35
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Centering An Icon In A QTableWidgetItem?

    Quote Originally Posted by wysota View Post
    ... set Qt::TextAlignmentRole for the item to Qt::AlignCenter.
    Using Qt::AlignCenter doesn't center the icon.

    This is my code; note that TableCell subclasses QTableWidgetItem (it only adds the '<' operater to accomplish a sort via tooltip text).

    Qt Code:
    1. setItem( m_nCurrRow, COL_Status, new TableCell( "" ) );
    2. item( m_nCurrRow, COL_Status )->setTextAlignment( Qt::AlignCenter );
    To copy to clipboard, switch view to plain text mode 

    It appears that there is no easy way to do this.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Centering An Icon In A QTableWidgetItem?

    In that case you need to provide a custom delegate for the table and paint the image centered (you can have a custom role for deciding whether the image should be centered - you might call it DecorationAlignmentRole or reuse TextAlignmentRole if you are sure there will be no text in that cell).

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

    mclark (14th November 2007)

Similar Threads

  1. Mac application and the Dock Icon
    By sekelsenmat in forum Qt Programming
    Replies: 3
    Last Post: 26th September 2007, 10:23
  2. QTableWidgetItem: Headers and centering items
    By thebra in forum Qt Programming
    Replies: 3
    Last Post: 6th August 2007, 00:00
  3. Interesting little Segfault w/r to signal/slot connection
    By Hydragyrum in forum Qt Programming
    Replies: 24
    Last Post: 12th September 2006, 19:22

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.