Results 1 to 9 of 9

Thread: Repositioning widgets in QGridLayout

  1. #1
    Join Date
    Jan 2006
    Location
    Third rock from the sun
    Posts
    106
    Thanks
    17
    Thanked 6 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question Repositioning widgets in QGridLayout

    I am trying to create a dynamic (read: at runtime) layout for some custom plots, using a QGridLayout. On initialization, a single plot is displayed. The user selects to add other plots through menus, changing the overall layout. Adding the first object works fine, however adding the second object causes the first object to occupy/span both widgets with the second object in the correct position. Each subsequent object added is in the correct position, but the first object still spans the entire layout (can "see" the widget in the grid spacing/background).
    Qt Code:
    1. void RawDataWindow::layoutPlots()
    2. {
    3. QWidget *ui_plotAreaWidget = qFindChild<QWidget*>( this, "plotAreaWidget" );
    4. QGridLayout *plotLayout = (QGridLayout*) ui_plotAreaWidget->layout();
    5.  
    6. // Remove all plots from the layout to adjust spacing and setup reparenting
    7. for( int p = 0; p < mPlots.count(); p++ )
    8. {
    9. plotLayout->removeWidget( mPlots.at( p ).plotObject );
    10. mPlots.at( p ).plotObject->setParent( 0 );
    11. }
    12.  
    13. if( mPlots.count() == 1 )
    14. {
    15. plotLayout->addWidget( mPlots.at( 0 ).plotObject );
    16. }
    17. else if( mPlots.count() == 2 )
    18. {
    19. // Create a 1x2 grid
    20. plotLayout->addWidget( mPlots.at( 0 ).plotObject, 0, 0 );
    21. plotLayout->addWidget( mPlots.at( 1 ).plotObject, 0, 1 );
    22. }
    23. else if( mPlots.count() == 3 )
    24. {
    25. // 2x2 grid with the top plot spanning across both columns
    26. plotLayout->addWidget( mPlots.at( 0 ).plotObject, 0, 0, 1, 2 );
    27. plotLayout->addWidget( mPlots.at( 1 ).plotObject, 1, 0 );
    28. plotLayout->addWidget( mPlots.at( 2 ).plotObject, 1, 1 );
    29. }
    30. else if( mPlots.count() == 4 )
    31. {
    32. // 2x2 grid
    33. plotLayout->addWidget( mPlots.at( 0 ).plotObject, 0, 0 );
    34. plotLayout->addWidget( mPlots.at( 1 ).plotObject, 0, 1 );
    35. plotLayout->addWidget( mPlots.at( 2 ).plotObject, 1, 0 );
    36. plotLayout->addWidget( mPlots.at( 3 ).plotObject, 1, 1 );
    37. }
    38. }
    To copy to clipboard, switch view to plain text mode 

    I believe this is causing further issues in that whichever object I add last does not recieve mousePress/Release/Move events, but this is just speculation until I resolve the layout issue.

    Any ideas?

    Windows 7
    VS 2008
    Qt 4.6.3
    Last edited by Rayven; 17th March 2011 at 17:58.
    Every little child knows
    if you cant see dreams
    your eyes are blind

    Moxy Fruvous

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Repositioning widgets in QGridLayout

    Do you mean that the subsequent widgetadded are overlapping with first widget?

  3. #3
    Join Date
    Jan 2006
    Location
    Third rock from the sun
    Posts
    106
    Thanks
    17
    Thanked 6 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Repositioning widgets in QGridLayout

    Quote Originally Posted by Santosh Reddy View Post
    Do you mean that the subsequent widgetadded are overlapping with first widget?
    Yes, but only overlapping the very first widget added. The positions of widgets 2 - 4 are in the correct positions.
    Every little child knows
    if you cant see dreams
    your eyes are blind

    Moxy Fruvous

  4. #4
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Repositioning widgets in QGridLayout

    try specifying the row and col span as 1 for the first and all the widgets, it worked for me.

  5. #5
    Join Date
    Jan 2006
    Location
    Third rock from the sun
    Posts
    106
    Thanks
    17
    Thanked 6 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Repositioning widgets in QGridLayout

    Did not change anything. However, if I take out the line
    Qt Code:
    1. mPlots.at( p ).plotObject->setParent( 0 );
    To copy to clipboard, switch view to plain text mode 
    the very first plot draws behind the second plot. Once I add the third plot, however the layout "fixes" itself. If I remove all plot (toggle them off) and re-show two plots, the exact same issue occurs.

    So now the code looks like this:
    Qt Code:
    1. void RawDataWindow::layoutPlots()
    2. {
    3. QWidget *ui_plotAreaWidget = qFindChild<QWidget*>( this, "plotAreaWidget" );
    4. QGridLayout *plotLayout = (QGridLayout*) ui_plotAreaWidget->layout();
    5.  
    6. // Remove all plots from the layout to adjust spacing and setup reparenting
    7. for( int p = 0; p < mPlots.count(); p++ )
    8. {
    9. plotLayout->removeWidget( mPlots.at( p ).plotObject );
    10.  
    11. // Note this line was "causing" the first plot to ALWAYS draw behind the others
    12. // Removing this "fixes" the layout AFTER adding a third plot object
    13. //mPlots.at( p ).plotObject->setParent( 0 );
    14. }
    15.  
    16. if( mPlots.count() == 1 )
    17. {
    18. plotLayout->addWidget( mPlots.at( 0 ).plotObject, 0, 0, 1, 1 );
    19. }
    20. else if( mPlots.count() == 2 )
    21. {
    22. // Create a 1x2 grid
    23. plotLayout->addWidget( mPlots.at( 0 ).plotObject, 0, 0, 1, 1 );
    24. plotLayout->addWidget( mPlots.at( 1 ).plotObject, 0, 1, 1, 1 );
    25. }
    26. else if( mPlots.count() == 3 )
    27. {
    28. // 2x2 grid with the top plot spanning across both columns
    29. plotLayout->addWidget( mPlots.at( 0 ).plotObject, 0, 0, 1, 2 );
    30. plotLayout->addWidget( mPlots.at( 1 ).plotObject, 1, 0, 1, 1 );
    31. plotLayout->addWidget( mPlots.at( 2 ).plotObject, 1, 1, 1, 1 );
    32. }
    33. else if( mPlots.count() == 4 )
    34. {
    35. // 2x2 grid
    36. plotLayout->addWidget( mPlots.at( 0 ).plotObject, 0, 0, 1, 1 );
    37. plotLayout->addWidget( mPlots.at( 1 ).plotObject, 0, 1, 1, 1 );
    38. plotLayout->addWidget( mPlots.at( 2 ).plotObject, 1, 0, 1, 1 );
    39. plotLayout->addWidget( mPlots.at( 3 ).plotObject, 1, 1, 1, 1 );
    40. }
    41. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by Rayven; 17th March 2011 at 19:20.
    Every little child knows
    if you cant see dreams
    your eyes are blind

    Moxy Fruvous

  6. #6
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Repositioning widgets in QGridLayout

    You need not reparent the widget, while repositioning the widget in the same layout, the layout manager will take care of it, also check the example project attached. I tried with QPushButton, you may replace with the plot.

    try removing "plotLayout->removeWidget( mPlots.at( p ).plotObject );" this may also be not required.
    Attached Files Attached Files

  7. #7
    Join Date
    Jan 2006
    Location
    Third rock from the sun
    Posts
    106
    Thanks
    17
    Thanked 6 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Repositioning widgets in QGridLayout

    Quote Originally Posted by Santosh Reddy View Post
    You need not reparent the widget, while repositioning the widget in the same layout, the layout manager will take care of it, also check the example project attached. I tried with QPushButton, you may replace with the plot.

    try removing "plotLayout->removeWidget( mPlots.at( p ).plotObject );" this may also be not required.
    The removeWidget was not needed, but the same issue occurs.


    Added after 15 minutes:


    Quote Originally Posted by Rayven View Post
    The removeWidget was not needed, but the same issue occurs.
    Also, could not open your example code. ZIP was corrupted.
    Last edited by Rayven; 17th March 2011 at 20:01.
    Every little child knows
    if you cant see dreams
    your eyes are blind

    Moxy Fruvous

  8. #8
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Repositioning widgets in QGridLayout

    Rename this to .zip DynamicWidget.txt

  9. The following user says thank you to Santosh Reddy for this useful post:

    Rayven (21st March 2011)

  10. #9
    Join Date
    Jan 2006
    Location
    Third rock from the sun
    Posts
    106
    Thanks
    17
    Thanked 6 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Lightbulb [SOLVED] Repositioning widgets in QGridLayout

    Well, after much head scratching I (with the help of a colleague) was able to figure out that it had nothing to do with the creation of the layout, but rather with the creation of the plotObject, specifically the passing of the parent object. I was not passing ui_plotAreaWidget as the parent when creating the object, thinking that plotLayout->addWidget(...) reparented the object, which is not the case. Setting the plotObject parent as ui_plotAreaWidget fixed all the issues!

    Thank for all your assistance!
    Every little child knows
    if you cant see dreams
    your eyes are blind

    Moxy Fruvous

Similar Threads

  1. Replies: 2
    Last Post: 16th December 2010, 11:52
  2. Function for repositioning the file pointer
    By psantofe in forum Qt Programming
    Replies: 7
    Last Post: 9th September 2010, 13:36
  3. Widgets in QGridLayout placed at top left
    By anarion in forum Newbie
    Replies: 2
    Last Post: 2nd September 2010, 15:06
  4. qgridlayout and hiding widgets
    By Michael_Levin in forum Newbie
    Replies: 3
    Last Post: 21st May 2010, 17:47
  5. space between widgets in qgridlayout
    By demol in forum Qt Programming
    Replies: 2
    Last Post: 6th January 2010, 20:07

Tags for this Thread

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.