PDA

View Full Version : Moving widgets within a layout.



importantman
26th May 2007, 18:41
I am using QVBoxLayout to have a vertical list of custom composite widgets which contain an up and down button, so to move that widget up and down within the list.

It doesn't work.

Do I have to delete and then recreate the widget in order to move it ?

I have tried using layout->remove() and layout->insert() but it doesn't appear to be working.

wysota
26th May 2007, 18:49
I think using QLayout::takeAt() and QBoxLayout::insertWidget() should work.

importantman
26th May 2007, 19:00
Wow ! Fast reply !

IIRC takeAt() is only in qt4, and so far I'm using qt3.

wysota
26th May 2007, 19:58
Oh, sorry for that. In your case use QBoxLayout::insertWidget() and QLayout::remove().

importantman
26th May 2007, 20:13
Just did that, and I am using ::findWidget(*QWidget) to get the widget index number, and it works at first, then it goes haywire.

I have 3 sample widgets included, like this:

1224
and I can move number 2 up to the top by pushing the up button, like this:

1223
but if I try to move number 1 back to the top, I get no response. If I push the up button a second time, it doesn't move. In fact, if a push the up buttons a few times, none of them respond.

I have not implemented the other buttons yet.

Here is the relevant code (it is declared as a slot in the header). PositionElementLine inherits QWidget.


void PositionElementBox::up(PositionElementLine *line)
{
int index = boxLayout->findWidget( line );

if(index > 0 ) //an index of 0 means the widget is at the top !!
{
boxLayout->remove(lineList.at(index));
boxLayout->insertWidget( ( index - 1 ), line );
}
}

importantman
26th May 2007, 20:31
oops! The error is in line 7. the lineList is copied from old code. should be

->remove(line);

wysota
26th May 2007, 21:21
Why not implement it as QTable and QTableItems and simply forget about the problem? :)