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
    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

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.