Results 1 to 7 of 7

Thread: QGridLayout -problem with widget placements

  1. #1
    Join Date
    May 2013
    Location
    Melbourne
    Posts
    36
    Thanks
    2
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default QGridLayout -problem with widget placements

    Hallo,
    I am pretty new to Qt and trying to develop a page as shown in the attached figure. Basically I would like all 3 pies to be uniformly separated at the first row, and the table to appear at the second row.


    Screenshot.jpg

    With QGridLayout it seems like the multiple widgets are not automatically re-sized and placed. Initially without lines 12-17, only the table was displayed (at the top).
    With line 15 commented, I get the page as shown in figure i.e the first pie is displayed partially. I also tried to use QHBoxLayout for the 3 pies, and then use QVBoxLayout- still the same problem.

    Could anybody help me please:
    #1 To sort out the above problem
    #2 Also, is there any way to place a border around each widget (setSpacing allows some gap)?

    Thank you in advance.


    Qt Code:
    1. TabSix_PageSix::TabSix_PageSix(QWidget *parent): QWidget(parent)
    2. {
    3. PaintWidgetPie *pie1 = new PaintWidgetPie;
    4. PaintWidgetPie *pie2 = new PaintWidgetPie;
    5. PaintWidgetPie *pie3 = new PaintWidgetPie;
    6.  
    7. QSizePolicy spVertical(QSizePolicy::Preferred, QSizePolicy::Preferred);
    8. spVertical.setVerticalStretch(1);
    9. QSizePolicy spHorizontal(QSizePolicy::Preferred, QSizePolicy::Preferred);
    10. spHorizontal.setHorizontalStretch(1);
    11.  
    12. pie1->setSizePolicy(spVertical);
    13. pie2->setSizePolicy(spVertical);
    14. pie3->setSizePolicy(spVertical);
    15. //pie1->setSizePolicy(spHorizontal);
    16. pie2->setSizePolicy(spHorizontal);
    17. pie3->setSizePolicy(spHorizontal);
    18.  
    19. //QLabel *label = new QLabel (tr("Web Traffic Breakdown by Protocol Graph"));
    20.  
    21. QGroupBox *gbox = new QGroupBox;
    22. gbox->setTitle(tr("Web Traffic Breakdown by Protocol Table"));
    23.  
    24. gbox->setStyleSheet("QGroupBox::title {"
    25. "subcontrol-position:top-center;"
    26. "color: #000000;"
    27. "background-color: #C2DFFF;"
    28. "padding:2 560 2 565;"
    29. "} QGroupBox {font-size: 20px;}"
    30. ); //font doesn't work with QGroupBox::title, all parameters need to be set in one go
    31.  
    32. QTableWidget *tableWidget = new QTableWidget(this);
    33. tableWidget->setRowCount(2);
    34. tableWidget->setColumnCount(8);
    35. tableWidget->setItem(0,0, new QTableWidgetItem("25:08:2010"));
    36. tableWidget->setItem(0,1, new QTableWidgetItem("10.131.344 kByte"));
    37.  
    38. QStringList labels;
    39. labels <<tr("Date:Time") << tr("FWI Traffic");
    40. tableWidget->setHorizontalHeaderLabels(labels);
    41. tableWidget->horizontalHeader()->show();
    42. tableWidget->verticalHeader()->hide();
    43. tableWidget->setShowGrid(true);
    44. tableWidget->setStyleSheet("border-color: 10px blue;");
    45.  
    46. QGridLayout *gLayout = new QGridLayout;
    47. gLayout->addWidget(pie1, 0, 0);
    48. gLayout->addWidget(pie2, 0, 1);
    49. gLayout->addWidget(pie3, 0, 2);
    50. gLayout->addWidget(tableWidget, 1,0);
    51. this->setLayout(gLayout);
    52. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QGridLayout -problem with widget placements

    Line 7 through 17 are unnecessary unless you have done something to make the PaintWidgetPie policies default differently. It looks like PaintWidgetPie might be returning a sizeHint() that is too small.

    You create gbox but never use it.

    You need the table widget to span columns 0, 1, and 2. Either of these should do it:
    Qt Code:
    1. gLayout->addWidget(tableWidget, 1, 0, 1, 3);
    2. // or
    3. gLayout->addWidget(tableWidget, 1, 0, -1, -1);
    To copy to clipboard, switch view to plain text mode 

    You can put each PaintWidgetPie instance inside QFrame instance before adding the QFrame instance to the layout.

  3. #3
    Join Date
    Jan 2013
    Posts
    25
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QGridLayout -problem with widget placements

    Vertical text on those tabs broke my neck btw

  4. #4
    Join Date
    May 2013
    Location
    Melbourne
    Posts
    36
    Thanks
    2
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QGridLayout -problem with widget placements

    @ChrisW67- Thank you so much for your suggestion. I have now commented lines 7-17 & used gbox instead of tableWidget- the output is as shown in fig i.e all pies look perfect but the table appears very small at top left corner. I will take a look at docs to understand sizeHint() and also about QFrame.
    Screenshot-5.jpg

    @Nyte- I would love to have them horizontal to save your neck! I tried before but couldn't succeed. If you have a solution please let me know- Thank you.

  5. #5
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QGridLayout -problem with widget placements

    The gbox stuff in your post did nothing useful, I was not suggesting you do anything other than delete it or use it correctly.

    You have put neither the table widget nor the gbox widget into the layout that you applied to the TabSix_PageSix widget.

  6. #6
    Join Date
    May 2013
    Location
    Melbourne
    Posts
    36
    Thanks
    2
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QGridLayout -problem with widget placements

    Yes, both sizeHint() and minimumSizeHint() for PaintWidgetPie are (-1,-1) whereas for gbox it is (286,232) and (310,124) resp.. The following code now gives me either 3 pies nicely (with gLayout->addWidget(gbox, 1,0, 1, 3) ) or the table (gLayout->addWidget(gbox, 1,0, -1, -1);).

    I then tried to change (with return QSize) the sizeHint() & minimumSizeHint() of PaintWidgetPie to values close to gbox (and also to 1/3 the values of gbox) but no way I can get all of them together. The new values look correct as displayed (I can see from the qDebug() ). Some settings give partial pies etc. but not all widgets correctly. What is the rule of thumb?

    Thanks again,
    PS: Sorry the "wink" icon appeared because of the punctuations.

    Qt Code:
    1. TabSix_PageSix::TabSix_PageSix(QWidget *parent): QWidget(parent)
    2. {
    3. PaintWidgetPie *pie1 = new PaintWidgetPie;
    4. PaintWidgetPie *pie2 = new PaintWidgetPie;
    5. PaintWidgetPie *pie3 = new PaintWidgetPie;
    6.  
    7. QGroupBox *gbox = new QGroupBox;
    8. gbox->setTitle(tr("Web Traffic Breakdown by Protocol Table"));
    9.  
    10. gbox->setStyleSheet("QGroupBox::title {"
    11. "subcontrol-position:top-center;"
    12. "color: #000000;"
    13. "background-color: #C2DFFF;"
    14. "padding:2 560 2 565;"
    15. "} QGroupBox {font-size: 20px;}"
    16. ); //font doesn't work with QGroupBox::title, all parameters need to be set in one go
    17.  
    18. QTableWidget *tableWidget = new QTableWidget;
    19. tableWidget->setRowCount(2);
    20. tableWidget->setColumnCount(8);
    21. tableWidget->setItem(0,0, new QTableWidgetItem("25:08:2010"));
    22. tableWidget->setItem(0,1, new QTableWidgetItem("10.131.344 kByte"));
    23.  
    24. QStringList labels;
    25. labels <<tr("Date:Time") << tr("FWI Traffic");
    26. tableWidget->setHorizontalHeaderLabels(labels);
    27. tableWidget->horizontalHeader()->show();
    28. tableWidget->verticalHeader()->hide();
    29. tableWidget->setShowGrid(true);
    30. //tableWidget->setStyleSheet("background-color: red");
    31. tableWidget->setStyleSheet("border-color: 10px blue;");
    32.  
    33. QVBoxLayout *vbox = new QVBoxLayout;
    34. vbox->addWidget(tableWidget);
    35. gbox->setLayout(vbox);
    36.  
    37. QGridLayout *gLayout = new QGridLayout(this);
    38.  
    39. gLayout->addWidget(pie1, 0, 0);
    40. gLayout->addWidget(pie2, 0, 1);
    41. gLayout->addWidget(pie3, 0, 2);
    42.  
    43. qDebug()<<pie1->sizeHint()<<pie1->minimumSizeHint();
    44. qDebug()<<gbox->sizeHint()<<gbox->minimumSizeHint();
    45.  
    46. gLayout->addWidget(gbox, 1,0, 1, 3); //shows table only
    47. //gLayout->addWidget(gbox, 1,0, -1, -1); //shows 3 pies only
    48. this->setLayout(gLayout);
    49. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by GG2013; 27th June 2013 at 05:24.

  7. #7
    Join Date
    May 2013
    Location
    Melbourne
    Posts
    36
    Thanks
    2
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QGridLayout -problem with widget placements

    Hi ChrisW67, just to let you know that with the proper sizeHint() all the widgets are now showing fine. Also, I have been able to use QFrame to have border around each widget.
    Thank you very much for your help and time.

Similar Threads

  1. QGridLayout: force all widget to the same size
    By Spooky in forum Qt Programming
    Replies: 2
    Last Post: 23rd March 2011, 13:58
  2. QVBoxLayout and QGridLayout Problem
    By eLancaster in forum Newbie
    Replies: 2
    Last Post: 6th December 2010, 20:33
  3. QGridlayout problem
    By raju@123 in forum Qt Programming
    Replies: 2
    Last Post: 24th September 2010, 22:03
  4. QGridLayout problem
    By cae in forum Newbie
    Replies: 2
    Last Post: 30th November 2009, 09:53
  5. Adjusting Widget size in a QGridLayout
    By Max Yaffe in forum Qt Programming
    Replies: 1
    Last Post: 30th July 2007, 22:03

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.