PDA

View Full Version : Center a widget in a cell on a QTableWidget



roleroz
24th August 2006, 18:56
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

e8johan
28th August 2006, 08:48
Put it in another widget along with a set of springs that centers it.

THRESHE
22nd February 2008, 15:40
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

jpn
22nd February 2008, 16:31
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:

THRESHE
22nd February 2008, 16:51
I understand how it looks in the Designer :) But I've asked for some code
I've tried to solve it like this


QWidget* wdg = new QWidget;
QVBoxLayout layout(wdg);
layout.addSpacing(6);
layout.addWidget(holder->GetProgressBar());
layout.addSpacing(6);
wdg->setLayout(&layout);
table->setCellWidget(vectIndex_, 4, wdg);
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 :(

jpn
22nd February 2008, 16:55
Spacing is non-stretchable space. Add stretch instead of spacing.

THRESHE
22nd February 2008, 17:17
Spacing is non-stretchable space. Add stretch instead of spacing.
I know but height of the row is also non-stretchable

jpn
22nd February 2008, 17:26
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

THRESHE
22nd February 2008, 17:42
Here is a screenshot and some code


QLabel* pL = new QLabel;

QMovie* movie_ = new QMovie;
movie_->setFileName(":/images/StatusIcons/ico_complete.png");

QHBoxLayout* layout = new QHBoxLayout(pL);
QLabel* ic = new QLabel;
QLabel* f = new QLabel("Hello World");
ic ->setMovie(movie_);
layout ->addWidget(ic, 5, Qt::AlignRight);
movie_->start();
layout ->addWidget(f);

table->setCellWidget(vectIndex_, 3, pL);
I can not understand what's the problem :crying:

jpn
22nd February 2008, 17:44
Layouts have margin by default so try:

layout->setMargin(0);

THRESHE
22nd February 2008, 17:49
Layouts have margin by default so try:

layout->setMargin(0);
Did the trick thanks a lot jpn ;)

THRESHE
22nd February 2008, 18:37
Damn :mad: Layouts are still not so clear to me :confused:
I try to use them like this


pixmap_.load(":/images/StatusIcons/ico_complete.png");
iconLabel_.setPixmap(pixmap_);
layout_.addSpacing(50);
layout_.addWidget(&iconLabel_);
layout_.addSpacing(5);
layout_.addWidget(&textLabel_);
layout_.addSpacing(50);
layout_.setMargin(0);

But I get something like this...:confused:

jpn
22nd February 2008, 18:43
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:

http://www.qtcentre.org/forum/f-qt-programming-2/t-qt-widget-setparent-10561.html
http://www.qtcentre.org/forum/f-qt-programming-2/t-qt-layout-memory-issue-8715.html

Boy
14th August 2008, 13:31
This can be done a lot easier IMO:




//the tablewidget
QTableWidget* tableWidget = new QTableWidget(w);

//some rows and columns
tableWidget->setRowCount(3);
tableWidget->setColumnCount(3);

//first create a new widget
QWidget* wdg = new QWidget;

//lets say we want a checkbox in the cell
QCheckBox *box = new QCheckBox();

// create the layout ON THE HEAP! (else after the function ends, layout is gone)
QHBoxLayout* layout = new QHBoxLayout(wdg);

// add the checkbox to the widget
layout->addWidget(box);

// center align the layout (box ends up in the center horizontally and vertically)
layout->setAlignment( Qt::AlignCenter );

// set the layout on the widget, it takes ownership of the layout (don't need to delete it later)
wdg->setLayout(layout);

// set the widget in the cell
tableWidget->setCellWidget(2, 0, wdg);

drescherjm
4th March 2009, 21:29
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.

Boy
5th March 2009, 07:58
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

drescherjm
5th March 2009, 14:47
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...