Results 1 to 17 of 17

Thread: Center a widget in a cell on a QTableWidget

  1. #1
    Join Date
    Aug 2006
    Posts
    15
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Question Center a widget in a cell on a QTableWidget

    Hi, i have a QTableWidget and set a cellWidget for one particular cell

    The problem is that my widget has a fixed size and want that widget to be centered (horizontally and vertically) within the cell

    Any ideas??

    Best Regards

  2. #2
    Join Date
    Jan 2006
    Location
    Alingsås, Sweden
    Posts
    437
    Thanks
    3
    Thanked 39 Times in 39 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

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

    Put it in another widget along with a set of springs that centers it.

  3. #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

    Quote Originally Posted by e8johan View Post
    Put it in another widget along with a set of springs that centers it.
    Sorry about digging this old thread but could you post here some code that shows how to do it ?
    Thanks
    C++ & AMD forever

  4. #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

    Quote Originally Posted by THRESHE View Post
    Sorry about digging this old thread but could you post here some code that shows how to do it ?
    This is how it looks in Qt Designer:
    Attached Images Attached Images
    J-P Nurmi

  5. #5
    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

    I understand how it looks in the Designer But I've asked for some code
    I've tried to solve it like this

    Qt Code:
    1. QWidget* wdg = new QWidget;
    2. QVBoxLayout layout(wdg);
    3. layout.addSpacing(6);
    4. layout.addWidget(holder->GetProgressBar());
    5. layout.addSpacing(6);
    6. wdg->setLayout(&layout);
    7. table->setCellWidget(vectIndex_, 4, wdg);
    To copy to clipboard, switch view to plain text mode 
    But it worked the way I didn't expect it to work. The progress bar was centered but it became 3 pixels high not 12
    C++ & AMD forever

  6. #6
    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

    Spacing is non-stretchable space. Add stretch instead of spacing.
    J-P Nurmi

  7. #7
    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
    Spacing is non-stretchable space. Add stretch instead of spacing.
    I know but height of the row is also non-stretchable
    C++ & AMD forever

  8. #8
    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

    Quote Originally Posted by THRESHE View Post
    I know but height of the row is also non-stretchable
    Ok, in other words: spacing is a space of fixed amount of pixels.

    <spacing 6 px><widget N px><spacing 6 px> = takes always 12 + N pixels
    <stretch><widget N px><stretch> = takes M + N pixels where M stretches to available space
    J-P Nurmi

  9. #9
    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

    Here is a screenshot and some code

    Qt Code:
    1. QLabel* pL = new QLabel;
    2.  
    3. QMovie* movie_ = new QMovie;
    4. movie_->setFileName(":/images/StatusIcons/ico_complete.png");
    5.  
    6. QHBoxLayout* layout = new QHBoxLayout(pL);
    7. QLabel* ic = new QLabel;
    8. QLabel* f = new QLabel("Hello World");
    9. ic ->setMovie(movie_);
    10. layout ->addWidget(ic, 5, Qt::AlignRight);
    11. movie_->start();
    12. layout ->addWidget(f);
    13.  
    14. table->setCellWidget(vectIndex_, 3, pL);
    To copy to clipboard, switch view to plain text mode 
    I can not understand what's the problem
    Attached Images Attached Images
    • File Type: jpg 1.jpg (8.5 KB, 177 views)
    C++ & AMD forever

  10. #10
    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

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

    THRESHE (22nd February 2008)

  12. #11
    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

  13. #12
    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

  14. #13
    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

  15. #14
    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 

  16. #15
    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

  17. #16
    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

  18. #17
    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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.