Objects organised in a matrix
-
Hello everyone,
I'd like to know if it's possible to organise Widgets as elements of a matrix, by assigning them indices, row /column for example.
I have a large number of 'QTextEdit' in several of my application windows on which I'd like to run content tests. And rather than doing these tests widget by widget, I would have liked to access each of them iteratively through the code.
In fact, what I'm looking for are objects of the form object[i][j].
Is this possible?
And if so, how?Thanks in advance.
-
Hello everyone,
I'd like to know if it's possible to organise Widgets as elements of a matrix, by assigning them indices, row /column for example.
I have a large number of 'QTextEdit' in several of my application windows on which I'd like to run content tests. And rather than doing these tests widget by widget, I would have liked to access each of them iteratively through the code.
In fact, what I'm looking for are objects of the form object[i][j].
Is this possible?
And if so, how?Thanks in advance.
@MortyMars said in Objects organised in a matrix:
In fact, what I'm looking for are objects of the form object[i][j].
In general:
QVector< QVector< QTextEdit * > > m_textEditMatrix;But there might be a better way in your use case.
If you widgets are in a layout at all time (QGridLayoutfor example), you can access them by row/column as well. -
@MortyMars said in Objects organised in a matrix:
In fact, what I'm looking for are objects of the form object[i][j].
In general:
QVector< QVector< QTextEdit * > > m_textEditMatrix;But there might be a better way in your use case.
If you widgets are in a layout at all time (QGridLayoutfor example), you can access them by row/column as well.@Pl45m4
Thanks for your feedbackI'm glad to hear that there are one or more solutions to my problem.
However, I'm going to have to look into the solutions you suggest because, so far, I haven't managed anything, either with QVector or with QGridLayout (but I'm not very good at it...).
By any chance, do you have any more telling examples to suggest?
Thank you
-
@Pl45m4
Thanks for your feedbackI'm glad to hear that there are one or more solutions to my problem.
However, I'm going to have to look into the solutions you suggest because, so far, I haven't managed anything, either with QVector or with QGridLayout (but I'm not very good at it...).
By any chance, do you have any more telling examples to suggest?
Thank you
What examples?
Depends on you graphical representation of your "data" and if you only store them as grid ofQTextEditsor if they are really ordered like that in your GUI.See the
QGridLayoutdocumentation@Pl45m4 said in Objects organised in a matrix:
QVector< QVector< QTextEdit * > > m_textEditMatrix;
This works when you want to store the widgets in your own structure.
So you can do something likem_textEditMatrix[i][j]orm_textEditMatrix.at(i).at(j), which is slightly better. -
What examples?
Depends on you graphical representation of your "data" and if you only store them as grid ofQTextEditsor if they are really ordered like that in your GUI.See the
QGridLayoutdocumentation@Pl45m4 said in Objects organised in a matrix:
QVector< QVector< QTextEdit * > > m_textEditMatrix;
This works when you want to store the widgets in your own structure.
So you can do something likem_textEditMatrix[i][j]orm_textEditMatrix.at(i).at(j), which is slightly better.@Pl45m4
I'm sorry to insist, but I can't manage and I need a basic answer.For the first solution, I created a QGridLayout called gridLtxt1 in a QMainWindow.
This contains 11 rows of 6 columns of QTextEdit.My question is:
What is the code that allows me to modify the content of the Widget below, for example by changing its content from FALSE to TRUE?
Thanks !
-
@Pl45m4
I'm sorry to insist, but I can't manage and I need a basic answer.For the first solution, I created a QGridLayout called gridLtxt1 in a QMainWindow.
This contains 11 rows of 6 columns of QTextEdit.My question is:
What is the code that allows me to modify the content of the Widget below, for example by changing its content from FALSE to TRUE?
Thanks !
@MortyMars said in Objects organised in a matrix:
What is the code that allows me to modify the content of the Widget below, for example by changing its content from FALSE to TRUE?
ui->textEdit_15->setText("TRUE");
-
@MortyMars said in Objects organised in a matrix:
What is the code that allows me to modify the content of the Widget below, for example by changing its content from FALSE to TRUE?
ui->textEdit_15->setText("TRUE");
Thanks for your help
But I was hoping - and this is the whole point of the initial question - to access the textEdit by a reference to its position in the grid rather than by its name, which would allow me to process iteratively rather than object by object.
If that's not possible, I admit I don't see the point of grouping Widgets in these grids... :-(
-
Thanks for your help
But I was hoping - and this is the whole point of the initial question - to access the textEdit by a reference to its position in the grid rather than by its name, which would allow me to process iteratively rather than object by object.
If that's not possible, I admit I don't see the point of grouping Widgets in these grids... :-(
@MortyMars
QLayoutItem *QGridLayout::itemAtPosition(int row, int column) const
followed by
QWidget *QLayoutItem::widget() const
on the returnedQLayoutItem, will give you theQTextEditor whatever is there. Useqobject_cast<QTextEdit *>(widget)on the widget to check if it is/access it as aQTextEdit*. -
Thanks for your help
But I was hoping - and this is the whole point of the initial question - to access the textEdit by a reference to its position in the grid rather than by its name, which would allow me to process iteratively rather than object by object.
If that's not possible, I admit I don't see the point of grouping Widgets in these grids... :-(
@MortyMars ... or use the apporach @Pl45m4 already wrote above and store them in your own container.
-
Thanks for your help
But I was hoping - and this is the whole point of the initial question - to access the textEdit by a reference to its position in the grid rather than by its name, which would allow me to process iteratively rather than object by object.
If that's not possible, I admit I don't see the point of grouping Widgets in these grids... :-(
@MortyMars said in Objects organised in a matrix:
to access the textEdit by a reference to its position in the grid rather than by its name, which would allow me to process iteratively rather than object by object.
Put your
QTextEditsin a container like:@Pl45m4 said in Objects organised in a matrix:
QVector< QVector< QTextEdit * > > m_textEditMatrix;
Either you take them from your Designer file and add them in your code.
Or you code yourQTextEditsdirectly and add them to your layout also by code.QVector< QVector< QTextEdit * > > m_textEditMatrix; QTextEdit * edit_0_0 = new QTextEdit(this); QTextEdit * edit_0_1 = new QTextEdit(this); QTextEdit * edit_0_2 = new QTextEdit(this); QTextEdit * edit_1_0 = new QTextEdit(this); QTextEdit * edit_1_1 = new QTextEdit(this); QTextEdit * edit_1_2 = new QTextEdit(this); // Add a loop that fills your matrix QVector<QTextEdit*> tmpRow; tmpRow.push_back(edit_0_0); tmpRow.push_back(edit_0_1); tmpRow.push_back(edit_0_2); m_textEditMatrix.push_back(tmpRow); // test with: int row, col; row = 0; col = 1; m_textEditMatrix.at(row).at(col)->setText("True"); -
Thanks for your help
But I was hoping - and this is the whole point of the initial question - to access the textEdit by a reference to its position in the grid rather than by its name, which would allow me to process iteratively rather than object by object.
If that's not possible, I admit I don't see the point of grouping Widgets in these grids... :-(
@MortyMars
...Or :) ... If you all want to do is iterate theQTextEdits, not know/care which one is which (e.g. you're setting some attribute on them all)QList<QTextEdit *> allTextEdits = parentWidget.findChildren<QTextEdit *>(); ``` -
@MortyMars said in Objects organised in a matrix:
to access the textEdit by a reference to its position in the grid rather than by its name, which would allow me to process iteratively rather than object by object.
Put your
QTextEditsin a container like:@Pl45m4 said in Objects organised in a matrix:
QVector< QVector< QTextEdit * > > m_textEditMatrix;
Either you take them from your Designer file and add them in your code.
Or you code yourQTextEditsdirectly and add them to your layout also by code.QVector< QVector< QTextEdit * > > m_textEditMatrix; QTextEdit * edit_0_0 = new QTextEdit(this); QTextEdit * edit_0_1 = new QTextEdit(this); QTextEdit * edit_0_2 = new QTextEdit(this); QTextEdit * edit_1_0 = new QTextEdit(this); QTextEdit * edit_1_1 = new QTextEdit(this); QTextEdit * edit_1_2 = new QTextEdit(this); // Add a loop that fills your matrix QVector<QTextEdit*> tmpRow; tmpRow.push_back(edit_0_0); tmpRow.push_back(edit_0_1); tmpRow.push_back(edit_0_2); m_textEditMatrix.push_back(tmpRow); // test with: int row, col; row = 0; col = 1; m_textEditMatrix.at(row).at(col)->setText("True");@Pl45m4 said in Objects organised in a matrix:
@Pl45m4 @Christian-Ehrlicher @JonB :
Thank you all for trying to help me, especially as I'm clearly not at the right level to understand everything instantly, but I'll keep at it, I promise.Either you take them from your Designer file and add them in your code.
How can I get the QTextEdit from my form?
-
@Pl45m4 said in Objects organised in a matrix:
@Pl45m4 @Christian-Ehrlicher @JonB :
Thank you all for trying to help me, especially as I'm clearly not at the right level to understand everything instantly, but I'll keep at it, I promise.Either you take them from your Designer file and add them in your code.
How can I get the QTextEdit from my form?
How can I get the QTextEdit from my form?
When you use Designer all the widgets you create and name go into
ui->, e.g.ui->textEdit1. -
@Pl45m4 said in Objects organised in a matrix:
@Pl45m4 @Christian-Ehrlicher @JonB :
Thank you all for trying to help me, especially as I'm clearly not at the right level to understand everything instantly, but I'll keep at it, I promise.Either you take them from your Designer file and add them in your code.
How can I get the QTextEdit from my form?
@MortyMars said in Objects organised in a matrix:
How can I get the QTextEdit from my form?
In your
mainWindowwithui->objectname
(the name you see in QtDesigner, liketextEdit_10).
I recommend to rename them, so you know what widget is where. -
@MortyMars said in Objects organised in a matrix:
How can I get the QTextEdit from my form?
In your
mainWindowwithui->objectname
(the name you see in QtDesigner, liketextEdit_10).
I recommend to rename them, so you know what widget is where.@Pl45m4
Here's the code I've come up with.
The compilation runs smoothly, but I can't see any changes to the content of the QTextEdit in my form...`QVector< QVector< QTextEdit* >> m_textEditMatrix; ui->edit_0_0 = new QTextEdit(this); ui->edit_0_1 = new QTextEdit(this); ui->edit_0_2 = new QTextEdit(this); ui->edit_0_3 = new QTextEdit(this); ui->edit_0_4 = new QTextEdit(this); ui->edit_0_5 = new QTextEdit(this); // Add a loop that fills your matrix QVector<QTextEdit*> tmpRow; tmpRow.push_back(ui->edit_0_0); tmpRow.push_back(ui->edit_0_1); tmpRow.push_back(ui->edit_0_2); tmpRow.push_back(ui->edit_0_3); tmpRow.push_back(ui->edit_0_4); tmpRow.push_back(ui->edit_0_5); m_textEditMatrix.push_back(tmpRow); // test with: //int row, col; //row = 0; //col = 0; m_textEditMatrix.at(0).at(0)->setText("True"); m_textEditMatrix.at(0).at(1)->setText("True"); m_textEditMatrix.at(0).at(2)->setText("True"); m_textEditMatrix.at(0).at(3)->setText("True"); m_textEditMatrix.at(0).at(4)->setText("True"); m_textEditMatrix.at(0).at(5)->setText("True");> `` -
@Pl45m4
Here's the code I've come up with.
The compilation runs smoothly, but I can't see any changes to the content of the QTextEdit in my form...`QVector< QVector< QTextEdit* >> m_textEditMatrix; ui->edit_0_0 = new QTextEdit(this); ui->edit_0_1 = new QTextEdit(this); ui->edit_0_2 = new QTextEdit(this); ui->edit_0_3 = new QTextEdit(this); ui->edit_0_4 = new QTextEdit(this); ui->edit_0_5 = new QTextEdit(this); // Add a loop that fills your matrix QVector<QTextEdit*> tmpRow; tmpRow.push_back(ui->edit_0_0); tmpRow.push_back(ui->edit_0_1); tmpRow.push_back(ui->edit_0_2); tmpRow.push_back(ui->edit_0_3); tmpRow.push_back(ui->edit_0_4); tmpRow.push_back(ui->edit_0_5); m_textEditMatrix.push_back(tmpRow); // test with: //int row, col; //row = 0; //col = 0; m_textEditMatrix.at(0).at(0)->setText("True"); m_textEditMatrix.at(0).at(1)->setText("True"); m_textEditMatrix.at(0).at(2)->setText("True"); m_textEditMatrix.at(0).at(3)->setText("True"); m_textEditMatrix.at(0).at(4)->setText("True"); m_textEditMatrix.at(0).at(5)->setText("True");> ``@MortyMars said in Objects organised in a matrix:
ui->edit_0_0 = new QTextEdit(this); ui->edit_0_1 = new QTextEdit(this); ui->edit_0_2 = new QTextEdit(this); ui->edit_0_3 = new QTextEdit(this); ui->edit_0_4 = new QTextEdit(this); ui->edit_0_5 = new QTextEdit(this);Why? They are already created within ui.setupUi().
-
@MortyMars said in Objects organised in a matrix:
ui->edit_0_0 = new QTextEdit(this); ui->edit_0_1 = new QTextEdit(this); ui->edit_0_2 = new QTextEdit(this); ui->edit_0_3 = new QTextEdit(this); ui->edit_0_4 = new QTextEdit(this); ui->edit_0_5 = new QTextEdit(this);Why? They are already created within ui.setupUi().
I confess, I've stupidly copied and adapted the code provided for objects that don't yet exist.
By commenting out the unnecessary lines, IT WORKS !Thank you all very much for your support and patience, I'll be able to move forward, and if I need to I'll know where to find you :-)
-
M MortyMars has marked this topic as solved on