Results 1 to 16 of 16

Thread: QlineEdit + Sql Problem

  1. #1
    Join Date
    Jan 2012
    Posts
    25
    Thanks
    10
    Qt products
    Qt4
    Platforms
    Windows

    Default QlineEdit + Sql Problem

    Hello,
    I want create a QlineEdit which shows values from SQL Server Database.
    I try to do it in this way but no result (empty QlineEdit area) :
    Qt Code:
    1. QString sQuery = QString("SELECT Matricule FROM Projet.dbo.Produit WHERE Nom='%1' ").arg(ui->comboBox->currentText());
    2.  
    3. QSqlQuery query;
    4. query.exec( sQuery );
    5.  
    6. QString name = query.value(0).toString();
    7.  
    8. ui->lineEdit_3->setText(name);
    9.  
    10. }
    To copy to clipboard, switch view to plain text mode 
    Any Suggestions ?? Thanks previsiouly

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: QlineEdit + Sql Problem

    You maybe want to have a look at QSqlQuery::prepare() and call QSqlQuery::next() before trying to get values.

    And this is surely not a post for the "Qt Programming" forum. Moved.

  3. #3
    Join Date
    Jan 2012
    Posts
    25
    Thanks
    10
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QlineEdit + Sql Problem

    Hello
    Thank you for answering me. I read the documentation and i try it many times. No result
    This is my first time that programming Qt and also C++.
    Qt Code:
    1. QSqlQuery query;
    2. query.prepare("SELECT Matricule FROM Projet.dbo.Produit WHERE Nom=? ");
    3.  
    4. query.exec( sQuery );
    5.  
    6. QString name = query.value(0).toString();
    7.  
    8.  
    9. ui->lineEdit_3->setText(name);
    To copy to clipboard, switch view to plain text mode 
    Help ??

  4. #4
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: QlineEdit + Sql Problem

    Quote Originally Posted by anouar2002 View Post
    Help ??
    Already given!

  5. The following user says thank you to Lykurg for this useful post:

    anouar2002 (18th January 2012)

  6. #5
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QlineEdit + Sql Problem

    Quote Originally Posted by anouar2002 View Post
    Help ??
    Read QSqlQuery::bindValue(), QSqlQuery::next(), look at the return value from QSqlQuery::exec() and QSqlQuery::prepare(), and look at QSqlQuery::lastError() if needed. The documentation is your friend.

  7. The following user says thank you to ChrisW67 for this useful post:

    anouar2002 (18th January 2012)

  8. #6
    Join Date
    Jan 2012
    Posts
    25
    Thanks
    10
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QlineEdit + Sql Problem

    Hello,
    I found a solution that works perfectly for display but when I change the value of the ComboBox, the value of lineEdit remains the same.
    here is the code for this solution:
    Qt Code:
    1. this->model = new QSqlQueryModel();
    2. model->setQuery("SELECT Nom FROM Projet.dbo.Produit");
    3. ui->comboBox->setModel(model);
    4.  
    5.  
    6. QSqlQuery query;
    7. query.prepare("SELECT Matricule FROM Projet.dbo.Produit WHERE Nom=:nom");
    8. query.bindValue(":nom", ui->comboBox->currentText());
    9. if (query.exec() && query.next()) {
    10. ui->lineEdit_3->setText(query.value(0).toString());
    11. }
    To copy to clipboard, switch view to plain text mode 

    For information, in my database each 'nom' (displayed in comboBox) corresponds to a single 'id' (displayed in a lineEdit).
    Is there a method to the value of lineEdit changes when the ComboBox is changed? Thank you in advance

  9. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QlineEdit + Sql Problem

    Do you rerun this code after changing the combobox value?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  10. #8
    Join Date
    Jan 2012
    Posts
    25
    Thanks
    10
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QlineEdit + Sql Problem

    Quote Originally Posted by wysota View Post
    Do you rerun this code after changing the combobox value?
    Yessssssssssssss

  11. #9
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QlineEdit + Sql Problem

    Set a breakpoint at line 7, change the combo box value, and single step through after it stop at your break oint. Is line 10 being reached? The problem will probably become obvious.

  12. The following user says thank you to ChrisW67 for this useful post:

    anouar2002 (19th January 2012)

  13. #10
    Join Date
    Jan 2012
    Posts
    25
    Thanks
    10
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QlineEdit + Sql Problem

    Thank you for answering me.
    But i didn't understand what you mean by breakpoint and single step. Can u make them in that code please if you don't mind and thanks again

  14. #11
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QlineEdit + Sql Problem

    A breakpoint is something you set when you are running your program in a debugger, not something you do in the actual code. Start your program in a debugger. If you are using Qt Creator then:
    • Build a Debug version of your program
    • Locate the line(s) you want to stop at and press F9 to set/clear a breakpoint.
    • Press F5 to launch it in the debugger.
    • Use F10 and F11 to single step. Look at variables in the Locals and Expressions tab. Other options are on the Debug menu.

  15. The following user says thank you to ChrisW67 for this useful post:

    anouar2002 (19th January 2012)

  16. #12
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: QlineEdit + Sql Problem

    Looks to me that QDataWidgetMapper could be of interest. Combined with QDataWidgetMappersetCurrentIndex() and QComboBox::currentIndexChanged().

  17. The following user says thank you to Lykurg for this useful post:

    anouar2002 (19th January 2012)

  18. #13
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QlineEdit + Sql Problem

    Lorenz, don't be a spoilsport
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  19. #14
    Join Date
    Jan 2012
    Posts
    25
    Thanks
    10
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QlineEdit + Sql Problem

    Thanks everybody for answers.
    Chris, I tried to follow your instructions but it was really difficult because it's my first time I use Qt and my version is in French. Anyway, if there is a tutorial video, it will be great. And Thanks again!

    Lykurg, I'm not really good at programming and this is my first program in Qt Here I tried to do as you have told me but it did not work.
    Qt Code:
    1. this->model = new QSqlQueryModel();
    2. model->setQuery("SELECT Nom FROM Projet.dbo.Produit");
    3. ui->comboBox->setModel(model);
    4.  
    5.  
    6. QSqlQuery query;
    7. query.prepare("SELECT Matricule FROM Projet.dbo.Produit WHERE Nom=:nom");
    8. query.bindValue(":nom", ui->comboBox->currentText());
    9. if (query.exec() && query.next()) {
    10. ui->lineEdit_3->setText(query.value(0).toString());
    11. }
    12. QObject::connect(ui->comboBox, SIGNAL(currentIndexChanged(int)),ui->lineEdit_3, SLOT(setText(ui->comboBox->currentText())));
    To copy to clipboard, switch view to plain text mode 
    What should I do? Thank you in advance

  20. #15
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QlineEdit + Sql Problem

    Your connect statement is invalid. Read about signals and slots, connect() only expects datatypes, not arbitrary expressions. And back to my previous question -- how are you rerunning the code from your post when the combobox current index changes?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  21. The following user says thank you to wysota for this useful post:

    anouar2002 (19th January 2012)

  22. #16
    Join Date
    Jan 2012
    Posts
    25
    Thanks
    10
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QlineEdit + Sql Problem

    Thanks everyone. I used SLOT like you said and it works perfectly !!!

Similar Threads

  1. QLineEdit problem with setValidator
    By dominate in forum Qt Programming
    Replies: 4
    Last Post: 20th January 2011, 14:34
  2. QLineEdit problem
    By kumarpraveen in forum Newbie
    Replies: 5
    Last Post: 12th July 2010, 12:19
  3. QLineEdit Problem
    By newermind in forum Qt Programming
    Replies: 2
    Last Post: 18th August 2009, 14:14
  4. QLineEdit focus problem
    By matteo.cozzaglio in forum Qt Programming
    Replies: 3
    Last Post: 14th July 2009, 14:13
  5. QLineEdit keyPressEvent problem
    By impeteperry in forum Qt Programming
    Replies: 6
    Last Post: 27th November 2007, 16:57

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.