Setting fixed values for height and width of array of widgets
-
I want to set a fixed height and width to the tables so that it occupies a particular space in the window.
For example, if n=1; it need not cover the entire window, instead of that it should cover a particular space.
for (int ii=0;ii<n;ii++) { tableLayout->addWidget(myTables[ii],0,ii); myTable[ii]->setFixedHeight(20); myTable[ii]->setFixedWidth(20); }
My program crashes at n>=2. For n=1, the widget takes the central space of the window.
Any suggestion about which function to select for fixing min height and width of the widgets.
Thanks
-
@Swati777999 said in Setting fixed values for height and width of array of widgets:
Any suggestion about which function to select for fixing min height and width of the widgets.
If your app crashes then the first thing is to find out why instead of searching for something else.
Use debugger to see where exactly it crashes and why.
How many entries does myTable have? -
@jsulm
It crashes for any value greater than 2.
I checked it in the debugger and found out the faulty point ,i.e n=2.As mentioned above, for n=1, the widget covers the central portion instead of the top left part.
Any suggestion about how to achieve it?
-
@Swati777999 said in Setting fixed values for height and width of array of widgets:
Any suggestion about how to achieve it?
Can you please answer my question I asked above?
-
@Swati777999 said in Setting fixed values for height and width of array of widgets:
Any suggestion about how to achieve it?
Check if your myTable - container really contains more than two entries.
-
@Swati777999 said in Setting fixed values for height and width of array of widgets:
tableLayout->addWidget(myTables[ii],0,ii);
My program crashes at n>=2.So clearly, when you access
myTables[2]
that must be out-of-range --- I would guess yourmyTables
only has 2 elements? -
@JonB said in Setting fixed values for height and width of array of widgets:
I would guess your myTables only has 2 elements?
I asked that two times already, then @Christian-Ehrlicher asked same question, now you - let's see how long it takes to get this basic information :-)
It is really hard to help people who do not read answers properly and do not answer questions... -
@jsulm
I wrote similar to same OP in https://forum.qt.io/topic/132497/how-to-get-the-dimensions-of-the-widgets/5. I hope that user will take time to read and act on people's questions/answers/suggestions.@Swati777999 wrote:
I checked it in the debugger and found out the faulty point ,i.e n=2.
It is good that you are using the debugger. When you were there did you look at the size/dimension/number of elements in
myTables
? There are windows in the debugger which display information like how many elements there are in a list/array. By the way, what type is yourmyTables
(show its declaration)? -
Declaration of myTables
QMap<int, QTableWidget *>myTables;
Actually, I want to create tables with 7 rows and 2 columns inside a QGridLayout tableLayout . I have performed
inside a loop ,
tableLayout->addwidgets(myTables[ii],0,ii)QWidget *myWidget = new QWidget(); QGridLayout *tableLayout = new QGridLayout();
In the debugger, for the value of myTable it says <not accessible >
I hope that I made my point more clear.Let me write the code again:
This code is working absolutely fine butfor (int ii=0;ii<n;ii++) { tableLayout->addWidget(myTables[ii],0,ii); }
when I add the following, it makes myTable[ii] in accessible, don't know why.
for (int ii=0;ii<n;ii++) { tableLayout->addWidget(myTables[ii],0,ii); //for adding table.............. myTables[ii]->setRowCount(6); myTables[ii]->setColumnCount(2) }
-
@Swati777999 said in Setting fixed values for height and width of array of widgets:
but why would it have 2 entries?
Why are you asking me?! I did not write your code.
Why don't you simply check how many entries you have there?!
If you can't see it in debugger you can simply add a debug output:qDebug() << myTable.size(); for (int ii=0;ii<n;ii++) ...
-
Yes, I checked the size of table with qDebug() << myTable.size() which came out to be 1.
Actually the fact is that
QWidget *myWidget = new QWidget(); QGridLayout *tableLayout = new QGridLayout(); tableLayout->setMargin(10); myWidget->setLayout(tableLayout); QMainWindow::setCentralWidget(myWidget); int n=5; QMap<int, QTableWidget *>myTables; for (int ii=0;ii<n;ii++) { myTables[ii]=new QTableWidget(); tableLayout->addWidget(myTables[ii],0,ii); } qDebug()<<"Size of the table is="<<myTables.size();
The above code works fine with the size of myTable as 5, but
whenever I want to do some operations on the elements of myTable [ii] , it crashes from there.
What could be the reason of it? -
@Swati777999 said in Setting fixed values for height and width of array of widgets:
What could be the reason of it?
Use debugger to find the reason.
It will tell you in which line it crashes and you will have the stack trace which you can post here so others can see what is happening instead of guessing. -
@Swati777999
After you have done as @jsulm suggests and tracked down and solved the reason for your crash here.Unless you have some reason in code we have not seen, I doubt you want the
QMap<int, QTableWidget *>myTables
you have chosen. It will just lead to complications. For an array why don't you declare it asQVector<QTableWidget *>myTables
instead? -
@JonB
Thanks for the suggestion. Let me use QVector and check its impact on the code.Edit: QMap works fine whereas QVector behaves in a weird way. At least, I am able to add myTable[ii] elements to the layout with the help of QMap container.
-
@Swati777999 said in Setting fixed values for height and width of array of widgets:
whereas QVector behaves in a weird way
Then it is your code.
Without code and more information not possible to say more than that. -
@Swati777999 said in Setting fixed values for height and width of array of widgets:
Edit: QMap works fine whereas QVector behaves in a weird way. At least, I am able to add myTable[ii] elements to the layout with the help of QMap container.
For an array/
QVector
you must actively create enough room in the array to accommodate elements, through one of:myTables.resize(5); // allocates room for 5 elements in the vector, numbered 0..4 // or myTables.append(new QTableWidget()); // appends one element to the vector
I did say to deal with why your code with
QMap
does not work in the first place before worrying about this....