Qt Code:
  1. #include "widget.h"
  2. #include "ui_widget.h"
  3.  
  4. Widget::Widget(QWidget *parent) :
  5. QWidget(parent),
  6. ui(new Ui::Widget)
  7. {
  8. ui->setupUi(this);
  9.  
  10. mydb = QSqlDatabase::addDatabase("QSQLITE");
  11. mydb.setDatabaseName("C:/sqlite/etudiant.sqlite");
  12.  
  13. if(!mydb.open())
  14. {
  15. qDebug() << "Fail";
  16. }
  17.  
  18. else
  19. {
  20.  
  21. qDebug() << "connected";
  22.  
  23. QSqlQuery *req = new QSqlQuery(mydb);
  24. req->prepare("SELECT name FROM note");
  25.  
  26. req->exec();
  27. modal->setQuery(*req);
  28. ui->comboBox->setModel(modal);
  29. }
  30. }
  31.  
  32. Widget::~Widget()
  33. {
  34. delete ui;
  35. }
  36.  
  37. void Widget::on_comboBox_currentIndexChanged(const QString &arg1)
  38. {
  39. QString name = ui->comboBox->currentText();
  40.  
  41. QSqlQuery qry;
  42. qry.prepare("SELECT * FROM note WHERE name = '"+name+"'");
  43.  
  44. if(qry.exec())
  45. {
  46. while (qry.next()) {
  47. ui->lineEdit->setText(qry.value(1).toString());
  48. ui->lineEdit_2->setText(qry.value(2).toString());
  49. }
  50. }
  51.  
  52. }
  53.  
  54. CREATE TABLE note(id INTEGER PRIMARY KEY, name VARCHAR(25), surname VARCHAR(25));
  55. INSERT INTO note VALUES(1, 'Jhon', 'Samuel');INSERT INTO note VALUES(2, 'Eddy', 'Ron');INSERT INTO note VALUES(3, 'Jhon', 'Mark');
To copy to clipboard, switch view to plain text mode 


hello everybody , got a small problem with a tutorial was following on youtube, where when the program couldn't display ONLY last name+surname in LineEdit, even when I select the first one in comboBox, just like that:

6796579801.jpg
2365202802.jpg
4030187203.jpg

hope you can gimmy some help to continu my learnings in the right way, thx , I knew by some researches that my code will only call to display last 'Jhon' he got everytime, but how can I correct my code? some code lines will help a lot, thx again !!