PDA

View Full Version : Replacing a Layout



Gekirou
31st July 2011, 21:41
Hey Guys,

i'm pretty new to QT - actually i'm using QT Jambi. I am stuck solving this problem:

i am building a QWidget which gets it's content from a generated QUiForm. This consists mainly of a QGridLayout, which i want to refill on resize. So what i have to do is either clean the QGridLayout and fill it with my new "icons" or swap the old QGridLayout with a new one.

The Problem is - that i can't do either. It seems that QT Jambi has no method for a QLayout to clear all items. Also my attempts to just swap the old with the new one did not work. It seems, that nor can't i remove the old one, but neither can i swap it with giving it a new layout. It says:"QLayout: Attempting to add QLayout "" to QWidget "Overview", which already has a layout"

Here's my code:



private void rearrangeIcons(QSize size){
int anzahlReihen = size.width()/55;
int anzahlZeilen = size.height()/70;
int gesamt = anzahlReihen * anzahlZeilen;
QGridLayout layout = new QGridLayout(install_base.parent);

int row = 0;
int column = 0;
int current = 0;
for(Entry<String, String> entry : icons.entrySet()){
if(column > anzahlReihen){
column = 0;
row++;
}

layout.addLayout(getLabeledIcon(entry.getKey(), entry.getValue()), row, column, 1, 1);
System.out.println("Adding "+entry.getKey()+ " "+ entry.getValue()+" current Index: "+current+ " to Row "+row+" and Column "+column);
column++;
current++;
}

while(current < gesamt){

if(column > anzahlReihen){
column = 0;
row++;
}

layout.addLayout(new QVBoxLayout(), row, column, 1, 1);

column++;
current++;
}

repaint();
}
}



public QGridLayout gridLayout_2;
public QTreeView treeView;
public QLabel label;
public QWidget parent;
public QScrollArea scrollArea;
public QLayout layout;
public QGridLayout currentLayout;

public Ui_GridView() { super(); }

public void setupUi(QWidget Overview)
{
parent = Overview;
Overview.setObjectName("Overview");
Overview.resize(new QSize(261, 592).expandedTo(Overview.minimumSizeHint()));
layout = new QFormLayout(parent);
}

I would be very happy for every help!

Greetings,
Geki

SixDegrees
31st July 2011, 21:48
You'll need to keep a list of all items added to the layout (or iterate over the layout to build the list); then call removeITem() on each item, remove and delete the layout, provide a new layout and add all the items to it again. If the items are to be replaced by new items, you'll have to delete the old ones.

If you simply want a different appearance when a widget's size grows beyond some limit, it would be better to override the paint() method and leave the widgets in place.

Gekirou
31st July 2011, 23:12
I don't really see how i can override the paintEvent(QPaintEvent paint) method to dynamically adjust the position of the icons. The class QPaintEvent has no information about the new size.

In the end all i want to do is dynamically adjust the positions of some labeled icons (QVBoxLayout with one Pixmap Label and one Text Label) inside of a window (as every filemanager does).

Also, how one can remove and delete the old layout - as there is no method to remove a layout in a QWidget?

Thanks for the help!
Geki

Cruz
1st August 2011, 08:52
Why not leave the layout and widgets in place and just replace and possibly resize the pixmaps and labels? Layouts are designed to handle resizing automatically.

MarekR22
1st August 2011, 09:39
IMHO proper solution to your problem is writing new layout. Curently you are trying enforce grid layout to do something else then it was design for.
Read documentation about subclassing QLayout and write new kind of layout.
It would be nice if you specify what you are trying to achieve (this is quite comon mistake when posting new thread in forums, describing how you are trying solve some problem instead describe your primary problem/objective).

Gekirou
3rd August 2011, 11:37
Hey,

thank you for your support.
As mentioned before, i am trying to readjust my labeled icons in terms of their position, as the window filemanager does (e.g.). That means, that if i resize my window to let's say 300 px in width, i want my layout to show 3 icons per row, resizing it to 400px should then show 4 icons per row etc.. In the end, it should never happen - that resizing my window results in margin growth or icon size growth.

I now have written a custom QWidget for every labeled icon. I will try to read something about reimplementing a QLayout, as every try to either delete the widgets or replace the layout however resulted in ghost-icons and/or a horrific computation time.
I am thankful for every help :-)!

Greetings,
Geki

Cruz
3rd August 2011, 12:07
Thats sounds just like what the QFLowLayout does.

Gekirou
3rd August 2011, 12:25
It seems that jambi does not support a QFlowLayout - so i have to reimplement it - or am i missing something?

Greetings,
Gekirou

Gekirou
3rd August 2011, 20:35
Hey,

i now reimplemented a flow layout for jambi and it's working just fine. I only have an issue using a scrollbar with it - as the the flow layout should be resizeable in width, but should use a scrollbar in height. I don't really understand how i can realize this.

Cruz
4th August 2011, 09:21
The scrollbars are handled by a QScrollArea. The QScrollArea has a "widget" that has to be set explicitely. This will be your custom flowlayouted widget. This works. I have done this before, also with a custom flow layout.