Results 1 to 3 of 3

Thread: Dynamically change position of grid layout items

  1. #1
    Join Date
    Jun 2019
    Location
    France, Pau
    Posts
    35
    Thanks
    20
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Dynamically change position of grid layout items

    Hi,

    I'm using this method to change the position of 2 widgets placed in a grid layout, I took the code from https://stackoverflow.com/questions/...d-layout-items

    Qt Code:
    1. void MyApp::swapWidgets(QGridLayout* grid, QWidget* widgetA, QWidget *widgetB)
    2. {
    3. if (!grid)
    4. {
    5. return;
    6. }
    7.  
    8. int indexA = grid->indexOf(widgetA);
    9. int indexB = grid->indexOf(widgetB);
    10. int row1, column1, rowSpan1, columnSpan1;
    11. int row2, column2, rowSpan2, columnSpan2;
    12.  
    13. grid->getItemPosition(indexA, &row1, &column1, &rowSpan1, &columnSpan1);
    14. grid->getItemPosition(indexB, &row2, &column2, &rowSpan2, &columnSpan2);
    15.  
    16. grid->takeAt(indexA);
    17. grid->takeAt(indexB);
    18.  
    19. grid->addWidget(widgetB, row1, column1, rowSpan1, columnSpan1);
    20. grid->addWidget(widgetA, row2, column2, rowSpan2, columnSpan2);
    21. }
    To copy to clipboard, switch view to plain text mode 

    All my widgets belong to the auto-generated GUI class (from the UI file).

    I ran Valgrind and I noticed leaks when this function gets called, the number of leaks is strangely lower than the calls to the swapWidgets method :

    ==24440== 88 bytes in 1 blocks are definitely lost in loss record 11,181 of 15,003
    ==24440== at 0x4C2A1E3: operator new(unsigned long) (vg_replace_malloc.c:334)
    ==24440== by 0x1E8BA469: QLayoutPrivate::createWidgetItem(QLayout const*, QWidget*) (in /usr/lib64/libQt5Widgets.so.5.9.2)
    ==24440== by 0x1E8B95F0: QGridLayout::addWidget(QWidget*, int, int, int, int, QFlags<Qt::AlignmentFlag>) (in /usr/lib64/libQt5Widgets.so.5.9.2)
    ==24440== by 0x4AA313: Ui_MyApp::setupUi(QWidget*) (==24440== 88 bytes in 1 blocks are definitely lost in loss record 11,181 of 15,003
    ==24440== at 0x4C2A1E3: operator new(unsigned long) (vg_replace_malloc.c:334)
    ==24440== by 0x1E8BA469: QLayoutPrivate::createWidgetItem(QLayout const*, QWidget*) (in /usr/lib64/libQt5Widgets.so.5.9.2)
    ==24440== by 0x1E8B95F0: QGridLayout::addWidget(QWidget*, int, int, int, int, QFlags<Qt::AlignmentFlag>) (in /usr/lib64/libQt5Widgets.so.5.9.2)
    ==24440== by 0x4AA313: Ui_MyApp::setupUi(QWidget*) (ui_MyApp.h:997)
    ==24440== by 0x47D361: MyApp::MyApp(QWidget*) (MyApp.cpp:89)
    ==24440== by 0x47AA51: main (main.cpp:36)
    ==24440== .h:997)
    ==24440== by 0x47D361: MyApp::MyApp(QWidget*) (MyApp.cpp:89)
    ==24440== by 0x47AA51: main (main.cpp:36)
    ==24440==
    ==24440== 88 bytes in 1 blocks are definitely lost in loss record 11,182 of 15,003
    ==24440== at 0x4C2A1E3: operator new(unsigned long) (vg_replace_malloc.c:334)
    ==24440== by 0x1E8BA469: QLayoutPrivate::createWidgetItem(QLayout const*, QWidget*) (in /usr/lib64/libQt5Widgets.so.5.9.2)
    ==24440== by 0x1E8B95F0: QGridLayout::addWidget(QWidget*, int, int, int, int, QFlags<Qt::AlignmentFlag>) (in /usr/lib64/libQt5Widgets.so.5.9.2)
    ==24440== by 0x49746D: MyApp::swapWidgets(QGridLayout*, QWidget*, QWidget*) (MyApp.cpp:3765)
    ==24440== by 0x4807E8: MyApp::ConnectSignalsSlots()::{lambda(bool)#15}:perator()(bool) const (MyApp.cpp:758)
    ==24440== by 0x49ACF8: QtPrivate::FunctorCall<QtPrivate::IndexesList<0>, QtPrivate::List<bool>, void, MyApp::ConnectSignalsSlots()::{lambda(bool)#15}>:: call({lambda(bool)#15}&, void**) (qobjectdefs_impl.h:130)
    ==24440== by 0x49A82C: void QtPrivate::Functor<MyApp::ConnectSignalsSlots()::{ lambda(bool)#15}, 1>::call<QtPrivate::List<bool>, void>({lambda(bool)#15}&, void*, {lambda(bool)#15}&*) (qobjectdefs_impl.h:240)
    ==24440== by 0x49A3DB: QtPrivate::QFunctorSlotObject<MyApp::ConnectSignal sSlots()::{lambda(bool)#15}, 1, QtPrivate::List<bool>, void>::impl(int, QtPrivate::QSlotObjectBase*, QObject*, void**, bool*) (qobject_impl.h:168)
    ==24440== by 0x1F4DB8B2: QMetaObject::activate(QObject*, int, int, void**) (in /usr/lib64/libQt5Core.so.5.9.2)
    ==24440== by 0x1E994F41: QAbstractButton::clicked(bool) (in /usr/lib64/libQt5Widgets.so.5.9.2)
    ==24440== by 0x1E995179: ??? (in /usr/lib64/libQt5Widgets.so.5.9.2)
    ==24440== by 0x1E996732: ??? (in /usr/lib64/libQt5Widgets.so.5.9.2)
    ==24440==
    ==24440== 88 bytes in 1 blocks are definitely lost in loss record 11,183 of 15,003
    ==24440== at 0x4C2A1E3: operator new(unsigned long) (vg_replace_malloc.c:334)
    ==24440== by 0x1E8BA469: QLayoutPrivate::createWidgetItem(QLayout const*, QWidget*) (in /usr/lib64/libQt5Widgets.so.5.9.2)
    ==24440== by 0x1E8B95F0: QGridLayout::addWidget(QWidget*, int, int, int, int, QFlags<Qt::AlignmentFlag>) (in /usr/lib64/libQt5Widgets.so.5.9.2)
    ==24440== by 0x4974A4: MyApp::swapWidgets(QGridLayout*, QWidget*, QWidget*) (MyApp.cpp:3766)
    ==24440== by 0x4807E8: MyApp::ConnectSignalsSlots()::{lambda(bool)#15}:perator()(bool) const (MyApp.cpp:758)
    ==24440== by 0x49ACF8: QtPrivate::FunctorCall<QtPrivate::IndexesList<0>, QtPrivate::List<bool>, void, MyApp::ConnectSignalsSlots()::{lambda(bool)#15}>:: call({lambda(bool)#15}&, void**) (qobjectdefs_impl.h:130)
    ==24440== by 0x49A82C: void QtPrivate::Functor<MyApp::ConnectSignalsSlots()::{ lambda(bool)#15}, 1>::call<QtPrivate::List<bool>, void>({lambda(bool)#15}&, void*, {lambda(bool)#15}&*) (qobjectdefs_impl.h:240)
    ==24440== by 0x49A3DB: QtPrivate::QFunctorSlotObject<MyApp::ConnectSignal sSlots()::{lambda(bool)#15}, 1, QtPrivate::List<bool>, void>::impl(int, QtPrivate::QSlotObjectBase*, QObject*, void**, bool*) (qobject_impl.h:168)
    ==24440== by 0x1F4DB8B2: QMetaObject::activate(QObject*, int, int, void**) (in /usr/lib64/libQt5Core.so.5.9.2)
    ==24440== by 0x1E994F41: QAbstractButton::clicked(bool) (in /usr/lib64/libQt5Widgets.so.5.9.2)
    ==24440== by 0x1E995179: ??? (in /usr/lib64/libQt5Widgets.so.5.9.2)
    ==24440== by 0x1E996732: ??? (in /usr/lib64/libQt5Widgets.so.5.9.2)
    What can I change in swapWidgets() to fix that leak ?

    The first leak above, shows that the GUI object (autogeneratged) is no longer the owner of something (QGridLayout or QWidget, I dunno) manipulated by swapWidgets.

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,055
    Thanks
    233
    Thanked 649 Times in 639 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: Dynamically change position of grid layout items

    I am guessing that the leaks are the two QLayoutItem instances returned by the takeAt() calls. You can simply add a local variable to store the return value then call delete() on it.

    Qt's semantics for most takeAt() type calls is that ownership of whatever is being taken is transferred to the caller, so it is the caller's responsibility to clean up.
    Last edited by d_stranz; 3rd July 2019 at 18:44.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. The following user says thank you to d_stranz for this useful post:

    embeddedmz (4th July 2019)

  4. #3
    Join Date
    Jun 2019
    Location
    France, Pau
    Posts
    35
    Thanks
    20
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: Dynamically change position of grid layout items

    Thank you very much, the leaks are gone !

Similar Threads

  1. Replies: 2
    Last Post: 14th November 2015, 01:15
  2. Replies: 0
    Last Post: 30th April 2013, 17:32
  3. To place images to a grid dynamically
    By athulms in forum Qt Quick
    Replies: 0
    Last Post: 28th July 2011, 07:35
  4. To place images to a grid dynamically
    By athulms in forum Newbie
    Replies: 0
    Last Post: 28th July 2011, 06:46
  5. Dynamically change the layout
    By pippo42 in forum Qt Programming
    Replies: 2
    Last Post: 12th November 2009, 13:01

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.