Solved How to get data from URL/.php file and display in my fields
-
When I click a button I need to fetch data from URL(or .php file for exact ) and retrieve as a JSON array and display to my lineEdit or to comboBox , I've done this in Android Studio but no idea in Qt, as a request from newbie.
-
@Vineela You should start here: http://doc.qt.io/qt-5/qnetworkaccessmanager.html
For JSON: http://doc.qt.io/qt-5/qjsondocument.html -
@jsulm Thank you I'll try now
-
Thanks @jsulm it worked I've gone through the first link but not tried with the json one but i got what i wanted.
-
-
@Vineela said in How to get data from URL/.php file and display in my fields:
array strings
What do you mean by this? Please explain better what you want to achieve.
To display a string you can use QLabel.
To add an entry to QComboBox use http://doc.qt.io/qt-5/qcombobox.html#addItem -
@jsulm From my .php link I'll get 2 or more arrays so how do i get them and add it to my combo box.
-
@Vineela said in How to get data from URL/.php file and display in my fields:
2 or more arrays
Are you talking about Json?
-
@jsulm yep, I thought I may not use JSON, do I have to use JSON if yes then help me with JSON.
-
@Vineela If the server sends Json then you have to deal with Json. I gave you a link to documentation for accessing Json data using Qt, you wrote you didn't read it. Now it is time to read that documentation.
-
@jsulm this is the output of my link , well from this i need to get those "GNL0001 & GNL002" and add them to my combo box ,so i hope you get what I'm saying please help me out with this.
[
{
"LeaderCode": "GNL0001"
},
{
"LeaderCode": "GNL0002"
}
] -
@Vineela Did you read the documentation?
AByteArray data = "YOUR JSON DATA"; QJsonDocument doc = QJsonDocument::fromJson(data); QJsonArray array = doc.array(); QJsonObject object = array.at(0).toObject(); QString code = object["LeaderCode"].toString();
To add to QComboBox please refer to the documentation: http://doc.qt.io/qt-5/qcombobox.html#insertItem
-
@jsulm well yes i've done and read the documentation but small thing is item which i added is blank.
-
@Vineela First, you need to make sure you parsed Json correctly. The code I posted does not do that!
Then please show your current code (reading Json and putting entries into combo box).
One more question: are you sure you actually got the reply from server with Json? -
void MainWindow::datainDaHouse2(QByteArray data) { //first qDebug()<< "Jason value" +data; QJsonDocument doc = QJsonDocument::fromJson(data); QJsonArray array = doc.array(); QJsonObject object = array.at(0).toObject(); QString code = object["LeaderCode"].toString(); ui->comboBox_54->insertItem(3,code); //second qDebug()<< "InsertCode" +code; }
well this is my current code and my output for qDebug is,
//first
"Jason value<pre>[\n {\n "LeaderCode": "GNL0001"\n },\n {\n "LeaderCode": "GNL0002"\n }\n]</pre>"
//second here where my output getting blank
"InsertCode" -
@Vineela Your Json string is invalid as it contains <pre></pre>. You need to remove these HTML tags first.
data = data.replace("<pre>", "").replace("</pre>", ""); QJsonDocument doc = QJsonDocument::fromJson(data); QJsonArray array = doc.array(); QJsonObject object = array.at(0).toObject(); QString code = object["LeaderCode"].toString(); ui->comboBox_54->insertItem(3,code); //second qDebug()<< "InsertCode" +code;
-
@jsulm yes i got this "InsertCodeGNL0001" but wat abt GNL0002??
-
-
-
object = array.at(1).toObject(); QString code = object["LeaderCode"].toString(); ui->comboBox_54->insertItem(3,code); //second qDebug()<< "InsertCode" +code;