Results 1 to 10 of 10

Thread: Replacing a Layout

  1. #1
    Join Date
    Jul 2011
    Posts
    5
    Qt products
    Qt Jambi
    Platforms
    Windows

    Default Replacing a Layout

    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:


    Qt Code:
    1. private void rearrangeIcons(QSize size){
    2. int anzahlReihen = size.width()/55;
    3. int anzahlZeilen = size.height()/70;
    4. int gesamt = anzahlReihen * anzahlZeilen;
    5. QGridLayout layout = new QGridLayout(install_base.parent);
    6.  
    7. int row = 0;
    8. int column = 0;
    9. int current = 0;
    10. for(Entry<String, String> entry : icons.entrySet()){
    11. if(column > anzahlReihen){
    12. column = 0;
    13. row++;
    14. }
    15.  
    16. layout.addLayout(getLabeledIcon(entry.getKey(), entry.getValue()), row, column, 1, 1);
    17. System.out.println("Adding "+entry.getKey()+ " "+ entry.getValue()+" current Index: "+current+ " to Row "+row+" and Column "+column);
    18. column++;
    19. current++;
    20. }
    21.  
    22. while(current < gesamt){
    23.  
    24. if(column > anzahlReihen){
    25. column = 0;
    26. row++;
    27. }
    28.  
    29. layout.addLayout(new QVBoxLayout(), row, column, 1, 1);
    30.  
    31. column++;
    32. current++;
    33. }
    34.  
    35. repaint();
    36. }
    37. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. public QGridLayout gridLayout_2;
    2. public QTreeView treeView;
    3. public QLabel label;
    4. public QWidget parent;
    5. public QScrollArea scrollArea;
    6. public QLayout layout;
    7. public QGridLayout currentLayout;
    8.  
    9. public Ui_GridView() { super(); }
    10.  
    11. public void setupUi(QWidget Overview)
    12. {
    13. parent = Overview;
    14. Overview.setObjectName("Overview");
    15. Overview.resize(new QSize(261, 592).expandedTo(Overview.minimumSizeHint()));
    16. layout = new QFormLayout(parent);
    17. }
    To copy to clipboard, switch view to plain text mode 

    I would be very happy for every help!

    Greetings,
    Geki

  2. #2
    Join Date
    Apr 2010
    Posts
    769
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11
    Thanks
    1
    Thanked 94 Times in 86 Posts

    Default Re: Replacing a Layout

    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.

  3. #3
    Join Date
    Jul 2011
    Posts
    5
    Qt products
    Qt Jambi
    Platforms
    Windows

    Default Re: Replacing a Layout

    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

  4. #4
    Join Date
    Jan 2009
    Location
    Germany
    Posts
    387
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    101
    Thanked 15 Times in 15 Posts

    Default Re: Replacing a Layout

    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.

  5. #5
    Join Date
    Nov 2010
    Posts
    315
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo
    Thanked 53 Times in 51 Posts

    Default Re: Replacing a Layout

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

  6. #6
    Join Date
    Jul 2011
    Posts
    5
    Qt products
    Qt Jambi
    Platforms
    Windows

    Default Re: Replacing a Layout

    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

  7. #7
    Join Date
    Jan 2009
    Location
    Germany
    Posts
    387
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    101
    Thanked 15 Times in 15 Posts

    Default Re: Replacing a Layout

    Thats sounds just like what the QFLowLayout does.

  8. #8
    Join Date
    Jul 2011
    Posts
    5
    Qt products
    Qt Jambi
    Platforms
    Windows

    Default Re: Replacing a Layout

    It seems that jambi does not support a QFlowLayout - so i have to reimplement it - or am i missing something?

    Greetings,
    Gekirou

  9. #9
    Join Date
    Jul 2011
    Posts
    5
    Qt products
    Qt Jambi
    Platforms
    Windows

    Default Re: Replacing a Layout

    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.

  10. #10
    Join Date
    Jan 2009
    Location
    Germany
    Posts
    387
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    101
    Thanked 15 Times in 15 Posts

    Default Re: Replacing a Layout

    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.

Similar Threads

  1. Replacing polish diacritics in the string
    By kornicameister in forum Qt Programming
    Replies: 3
    Last Post: 8th February 2011, 16:54
  2. Replacing QtScript with QML
    By inpoculis789 in forum Newbie
    Replies: 0
    Last Post: 19th July 2010, 14:59
  3. Problem replacing a value in a QList.
    By bbad68 in forum Newbie
    Replies: 4
    Last Post: 29th January 2010, 18:14
  4. Replies: 0
    Last Post: 4th March 2009, 20:27
  5. Replacing One image with another
    By merry in forum Qt Tools
    Replies: 6
    Last Post: 8th February 2007, 14:22

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
  •  
Qt is a trademark of The Qt Company.