PDA

View Full Version : QGridLayout -problem with widget placements



GG2013
26th June 2013, 06:32
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.


9186

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.




TabSix_PageSix::TabSix_PageSix(QWidget *parent): QWidget(parent)
{
PaintWidgetPie *pie1 = new PaintWidgetPie;
PaintWidgetPie *pie2 = new PaintWidgetPie;
PaintWidgetPie *pie3 = new PaintWidgetPie;

QSizePolicy spVertical(QSizePolicy::Preferred, QSizePolicy::Preferred);
spVertical.setVerticalStretch(1);
QSizePolicy spHorizontal(QSizePolicy::Preferred, QSizePolicy::Preferred);
spHorizontal.setHorizontalStretch(1);

pie1->setSizePolicy(spVertical);
pie2->setSizePolicy(spVertical);
pie3->setSizePolicy(spVertical);
//pie1->setSizePolicy(spHorizontal);
pie2->setSizePolicy(spHorizontal);
pie3->setSizePolicy(spHorizontal);

//QLabel *label = new QLabel (tr("Web Traffic Breakdown by Protocol Graph"));

QGroupBox *gbox = new QGroupBox;
gbox->setTitle(tr("Web Traffic Breakdown by Protocol Table"));

gbox->setStyleSheet("QGroupBox::title {"
"subcontrol-position:top-center;"
"color: #000000;"
"background-color: #C2DFFF;"
"padding:2 560 2 565;"
"} QGroupBox {font-size: 20px;}"
); //font doesn't work with QGroupBox::title, all parameters need to be set in one go

QTableWidget *tableWidget = new QTableWidget(this);
tableWidget->setRowCount(2);
tableWidget->setColumnCount(8);
tableWidget->setItem(0,0, new QTableWidgetItem("25:08:2010"));
tableWidget->setItem(0,1, new QTableWidgetItem("10.131.344 kByte"));

QStringList labels;
labels <<tr("Date:Time") << tr("FWI Traffic");
tableWidget->setHorizontalHeaderLabels(labels);
tableWidget->horizontalHeader()->show();
tableWidget->verticalHeader()->hide();
tableWidget->setShowGrid(true);
tableWidget->setStyleSheet("border-color: 10px blue;");

QGridLayout *gLayout = new QGridLayout;
gLayout->addWidget(pie1, 0, 0);
gLayout->addWidget(pie2, 0, 1);
gLayout->addWidget(pie3, 0, 2);
gLayout->addWidget(tableWidget, 1,0);
this->setLayout(gLayout);
}

ChrisW67
26th June 2013, 06:58
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:


gLayout->addWidget(tableWidget, 1, 0, 1, 3);
// or
gLayout->addWidget(tableWidget, 1, 0, -1, -1);


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

Nyte
26th June 2013, 16:07
Vertical text on those tabs broke my neck btw

GG2013
27th June 2013, 00:32
@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.
9195

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

ChrisW67
27th June 2013, 00:37
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.

GG2013
27th June 2013, 04:50
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.



TabSix_PageSix::TabSix_PageSix(QWidget *parent): QWidget(parent)
{
PaintWidgetPie *pie1 = new PaintWidgetPie;
PaintWidgetPie *pie2 = new PaintWidgetPie;
PaintWidgetPie *pie3 = new PaintWidgetPie;

QGroupBox *gbox = new QGroupBox;
gbox->setTitle(tr("Web Traffic Breakdown by Protocol Table"));

gbox->setStyleSheet("QGroupBox::title {"
"subcontrol-position:top-center;"
"color: #000000;"
"background-color: #C2DFFF;"
"padding:2 560 2 565;"
"} QGroupBox {font-size: 20px;}"
); //font doesn't work with QGroupBox::title, all parameters need to be set in one go

QTableWidget *tableWidget = new QTableWidget;
tableWidget->setRowCount(2);
tableWidget->setColumnCount(8);
tableWidget->setItem(0,0, new QTableWidgetItem("25:08:2010"));
tableWidget->setItem(0,1, new QTableWidgetItem("10.131.344 kByte"));

QStringList labels;
labels <<tr("Date:Time") << tr("FWI Traffic");
tableWidget->setHorizontalHeaderLabels(labels);
tableWidget->horizontalHeader()->show();
tableWidget->verticalHeader()->hide();
tableWidget->setShowGrid(true);
//tableWidget->setStyleSheet("background-color: red");
tableWidget->setStyleSheet("border-color: 10px blue;");

QVBoxLayout *vbox = new QVBoxLayout;
vbox->addWidget(tableWidget);
gbox->setLayout(vbox);

QGridLayout *gLayout = new QGridLayout(this);

gLayout->addWidget(pie1, 0, 0);
gLayout->addWidget(pie2, 0, 1);
gLayout->addWidget(pie3, 0, 2);

qDebug()<<pie1->sizeHint()<<pie1->minimumSizeHint();
qDebug()<<gbox->sizeHint()<<gbox->minimumSizeHint();

gLayout->addWidget(gbox, 1,0, 1, 3); //shows table only
//gLayout->addWidget(gbox, 1,0, -1, -1); //shows 3 pies only
this->setLayout(gLayout);
}

GG2013
28th June 2013, 01:38
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.