Equally spaced tables in a layout
-
Hi All,
I want to get equally spaced tables in my design. I've written the following code but it produces incrementally spaced tables.
How can I achieve my desired design?QMap <int,QWidget*>dTWidgets; QMap<int ,QGridLayout*>tGLayout; QMap <int, QLabel*>dNames; QMap <int ,QTableWidget *> dTs; QGridLayout *msDLayout =new QGridLayout(); int n=10; for (int ii=0;ii<n;ii++) { dTWidgets[ii] =new QWidget(); //Parent Widget mSDLayout->addWidget(dTWidgets[ii],0,ii,Qt::AlignCenter); tGLayout[ii]=new QGridLayout(); //Parent Layout dTWidgets[ii]->setLayout(tGLayout[ii]); dNames[ii] =new QLabel(QString("Name %1").arg(ii)); //Child-1 of Parent tGLayout[ii]->addWidget(dNames[ii],0,ii); tGLayout[ii]->setAlignment(dNames[ii],Qt::AlignHCenter); dTs[ii] = new QTableWidget(); //Child-2 of Parent tGLayout[ii]->addWidget(dTs[ii]); ``
It produces tables with unequal spaces between them and the labels for names of each table don't align with each table.Can someone suggest me how to rectify erorrs from the above code?
-
For what purpose are those
QMaps
?If you use layouts, the size is dynamic in shrinks or expands with your parent widget. How should it look like?
Edit:
After your edit, I see your attached image.
If you just want this stucture, you dont need a GridLayout. You can use a verticalLayout and put your widgets below each other -
@Swati777999 said in Equally spaced tables in a layout:
I have used QMap for creating an array of widgets.
But why?! Do you really need that int value mapped to each widget?
If you just need to store multiple widgets in an array, better use containers likeQVector
.Edit:
TheQMap
documentation also states that you should access the map content with.value(i)
or.key(i)
insteas ofmap[i]
.
Especially when you compare things or look for a value in your map.You could create a widget, assign a layout (like vBox) and add your stuff... You can do this multiple time in a loop and add the new widget to a
QVector<QWidget*>
(or QMap, if you really want to). -
@Pl45m4 said in Equally spaced tables in a layout:
@Swati777999 said in Equally spaced tables in a layout:
You could create a widget, assign a layout (like vBox) and add your stuff... You can do this multiple time in a loop and add the new widget to aQVector<QWidget*>
(or QMap, if you really want to).I have just done the other way. I have created the array first, then created widgets and layouts for the individual box widgets. Does sequencing of steps in a particular manner affect the end result?
-
The order when you create what doesn't matter, as long as you assign it correctly after everything is created. Of course you can't assign the widget to your array without creating it first.