Results 1 to 17 of 17

Thread: Center a widget in a cell on a QTableWidget

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Center a widget in a cell on a QTableWidget

    Layouts have margin by default so try:
    Qt Code:
    1. layout->setMargin(0);
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  2. The following user says thank you to jpn for this useful post:

    THRESHE (22nd February 2008)

  3. #2
    Join Date
    Mar 2007
    Location
    Ukraine, Odessa
    Posts
    140
    Thanks
    15
    Thanked 11 Times in 10 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Center a widget in a cell on a QTableWidget

    Quote Originally Posted by jpn View Post
    Layouts have margin by default so try:
    Qt Code:
    1. layout->setMargin(0);
    To copy to clipboard, switch view to plain text mode 
    Did the trick thanks a lot jpn
    C++ & AMD forever

  4. #3
    Join Date
    Mar 2007
    Location
    Ukraine, Odessa
    Posts
    140
    Thanks
    15
    Thanked 11 Times in 10 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Center a widget in a cell on a QTableWidget

    Damn Layouts are still not so clear to me
    I try to use them like this

    Qt Code:
    1. pixmap_.load(":/images/StatusIcons/ico_complete.png");
    2. iconLabel_.setPixmap(pixmap_);
    3. layout_.addSpacing(50);
    4. layout_.addWidget(&iconLabel_);
    5. layout_.addSpacing(5);
    6. layout_.addWidget(&textLabel_);
    7. layout_.addSpacing(50);
    8. layout_.setMargin(0);
    To copy to clipboard, switch view to plain text mode 
    But I get something like this...
    Attached Images Attached Images
    • File Type: jpg 1.jpg (13.8 KB, 106 views)
    C++ & AMD forever

  5. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Center a widget in a cell on a QTableWidget

    Sorry, from that snippet it's hard to understand which is which. Anyway, it's a very bad idea to allocate widgets on the stack like that. Here are a couple of threads explaining why:
    J-P Nurmi

  6. #5
    Join Date
    Mar 2008
    Posts
    25
    Thanks
    9
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Unhappy Re: Center a widget in a cell on a QTableWidget

    This can be done a lot easier IMO:

    Qt Code:
    1. //the tablewidget
    2. QTableWidget* tableWidget = new QTableWidget(w);
    3.  
    4. //some rows and columns
    5. tableWidget->setRowCount(3);
    6. tableWidget->setColumnCount(3);
    7.  
    8. //first create a new widget
    9. QWidget* wdg = new QWidget;
    10.  
    11. //lets say we want a checkbox in the cell
    12. QCheckBox *box = new QCheckBox();
    13.  
    14. // create the layout ON THE HEAP! (else after the function ends, layout is gone)
    15. QHBoxLayout* layout = new QHBoxLayout(wdg);
    16.  
    17. // add the checkbox to the widget
    18. layout->addWidget(box);
    19.  
    20. // center align the layout (box ends up in the center horizontally and vertically)
    21. layout->setAlignment( Qt::AlignCenter );
    22.  
    23. // set the layout on the widget, it takes ownership of the layout (don't need to delete it later)
    24. wdg->setLayout(layout);
    25.  
    26. // set the widget in the cell
    27. tableWidget->setCellWidget(2, 0, wdg);
    To copy to clipboard, switch view to plain text mode 

  7. #6
    Join Date
    Apr 2008
    Location
    Pittsburgh,PA,USA
    Posts
    48
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Center a widget in a cell on a QTableWidget

    Thanks. The last example worked for me however the checkbox appears slightly off center in the cell. Any ideas what causes that?

    This is in Qt 4.3 on windows XP.
    John

  8. #7
    Join Date
    Mar 2008
    Posts
    25
    Thanks
    9
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Center a widget in a cell on a QTableWidget

    Quote Originally Posted by drescherjm View Post
    Thanks. The last example worked for me however the checkbox appears slightly off center in the cell. Any ideas what causes that?

    This is in Qt 4.3 on windows XP.
    Hi,

    I'm just guessing here, but maybe you could try layout->setSpacing( 0 ) and check if this changes anything

  9. #8
    Join Date
    Apr 2008
    Location
    Pittsburgh,PA,USA
    Posts
    48
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Center a widget in a cell on a QTableWidget

    Thanks, I will try that.

    My first guess for the reason was that there was some space auto allocated for the label that can appear next to the checkbox.

    If you have not guessed I am a former MFC programmer...
    John

Similar Threads

  1. repaint a cell or row in QTableWidget
    By alphaqt in forum Newbie
    Replies: 6
    Last Post: 26th June 2012, 11:21
  2. Pin/Unpin Dock Widget
    By charlesD in forum Newbie
    Replies: 1
    Last Post: 21st June 2006, 06:57
  3. Replies: 4
    Last Post: 24th March 2006, 22:50
  4. QTableWidget
    By campana in forum Qt Programming
    Replies: 7
    Last Post: 24th February 2006, 08:45
  5. [Qt 4.1.0] Split a widget on demand
    By Townk in forum Qt Programming
    Replies: 3
    Last Post: 17th February 2006, 14:16

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
  •  
Qt is a trademark of The Qt Company.