Solved How to get a query result into LineEdit widget?
-
@Pl45m4 ok i think i got it now. it just hard for me to understand things just from a documentation, because it always seems that my problems are too specific.
after trying to put stuff into a field now.
ui->lineEdit_1->setText(query.value(1).toString()); //this syntax is at least correcti get this output message: "QSqlQuery::value: not positioned on a valid record"
so i guess that there is nothing in my query? maybe because i have a wrong "SELECT FROM WHERE" command? -
@qtnoob420 said in How to get a query result into LineEdit widget?:
because it always seems that my problems are too specific
There is nothing specific. This is basic
QSql..
stuff usage.so i guess that there is nothing in my query?
You are trying to access index 1, which means, you need to have at least two valid results here, which is unlikely, when picking a specific ID:
If you expect only one record, you need to pick index 0 (like almost everything in C++ or most programming languages starts at 0)Ah made a mistake here.
The index is the field of your current record. But same rules apply: You need to have at least two fields in your record.Using SELECT * is not recommended because the order of the fields in the query is undefined.
If your query is correct and your result is not empty, are you sure, that the second field contains a string?
You could check
query.isValid()
(https://doc.qt.io/qt-5/qsqlquery.html#isValid) before you continue to process the result. -
: "QSqlQuery::value: not positioned on a valid record"
Please do look at the actual example given at https://doc.qt.io/qt-5/qsqlquery.html#details. You likely haven't called
query.next()
? -
@JonB thanks for the hint. i will look into it.
-
@Pl45m4 ok i got it to work now. it atleast gives me the first value. probably i am mistaken here again.
int i=0; while (query.next()) { ui->bem_1->insertPlainText(query.value(i).toString()); i++; }
it does not give other values than the id. i thought, that now all the values from the first row would be put into my plainText
-
@qtnoob420
You are not doing the right thing here. You usei
to access a column in the query, yet you incrementi
for each new row fetched fromquery.next()
. So you ask for:row 0, column 0 row 1, column 1 row 2 column 2
And if you get only one row back, as per your query
WHERE id = 1
, you will only get one row and therefore only the first output from above.To iterate all columns in rows, you mean something more like:
while (query.next()) // next *row* { int col = 0; QVariant v = query().value(col); // first *column* while (v.isValid()) // column exists, will return invalid QVariant once `col` goes beyond last column { ui->bem_1->insertPlainText(v.toString()); col++; v = query().value(col); // next *column* } }
-
Hi,
Did you try to first draw a suitable UI before diving into the SQL parts ?
As @jsulm noted, it looks like you are trying to implement a UI that might well be really difficult to use.
Depending on what your user needs to get you might want to consider a light page with only the basic information and then a second one with more details etc. You might even be better served by a simple table.
-
@qtnoob420 said in How to get a query result into LineEdit widget?:
i could provide a picture of my ui if that helps.
Hi
Please do as lineEdits and checkboxes can easily be replaced by a table, making it much easier to load
and edit data and save it back to the database.While it is possible to loop the QSqlQuery and assign each column to a given pre-placed widget, its
also likely to become a burden over time.
Just saving the data back will be "involving" as the widgets have no idea what column they belong to and
Qt already contains other data-aware classes to help with these tasks.
Like as @JonB mentions, using QSqlQueryModel + QDataWidgetMapperAlso, a QSqlTableModel + TableView would allow to directly edit a table with only a few lines of code, so
please show us the GUI so we can see if we can find a better way than manually updating and saving
50 LineEdits and 200 CheckBoxes. -
@mrjj @SGaist here the pictures of my ui. https://imgur.com/a/NulxYJe
-
@JonB thanks sir. this is what i needed :)
-
@qtnoob420
You are probably referring to the code for correctly reading the value in that column loop. And that's fine, but now that I see your form and how many fields there are I do think you would benefit from changing over to aQSqlTableModel
and being able to use aQDataWidgetMapper
. It looks like you are going to allow the user to edit the fields (not just display them), and the data widget mapper will allow you to do this very easily, and propagate the changes back to the database. I realize it is something new to learn, you don't have to do it but you might want to move that way if you have the time. -
@JonB i could try this too, if i get everything to work and have enough time left. but i need to finalize it 3 weeks and must write a paper about it too. it is part of my studies
-
@qtnoob420
I quite understand! :)