Results 1 to 15 of 15

Thread: qstring assignment crashes the program

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jun 2014
    Posts
    48
    Thanks
    6
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Question Re: qstring assignment crashes the program

    as you said I build non-static fuction as follows
    Qt Code:
    1. bool product_record::product_search(int numb)//, product_record *prdRecord)
    2. {
    3. QString sql = "select * from tableProductRecords where number = ?";
    4. QSqlQuery qry;
    5. qry.prepare(sql);
    6.  
    7. qry.bindValue(0, numb);
    8.  
    9. if(qry.exec())
    10. {
    11. if(qry.next())
    12. {
    13. this->name = qry.value(0).toString(); // still program crashes here
    14. this->stock = qry.value(1).toInt();
    15. this->rate = qry.value(2).toInt();
    16. this->number = qry.value(3).toInt();
    17. return true;
    18. }
    19. return false;
    20. }
    21.  
    22.  
    23. }
    To copy to clipboard, switch view to plain text mode 

    and called in this :-
    Qt Code:
    1. void dialogAddNewRecord::on_pushButtonSearchByNumber_clicked()
    2. {
    3. QString str = ui->lineEditNumber->text().trimmed();
    4. int s = str.toInt();
    5. prdRecord = new product_record(this);
    6.  
    7. if(s)
    8. {
    9. prdRecord->product_search(s);
    10. ui->lineEditName->setText(prdRecord->text_name());
    11. ui->lineEditStock->setText(QString::number(prdRecord->text_stock()));
    12. ui->lineEditRate->setText(QString::number(prdRecord->text_rate()));
    13. ui->lineEditNumber->setText(QString::number(prdRecord->text_number()));
    14. }
    15. else
    16. {
    17. QMessageBox::information(this, "on_pushButtonSearchByNumber_clicked", "number not valid");
    18. }
    19.  
    20. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,540
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: qstring assignment crashes the program

    Change line :
    Qt Code:
    1. this->name = qry.value(0).toString(); // still program crashes here
    To copy to clipboard, switch view to plain text mode 
    for this lines :
    Qt Code:
    1. QString test = qry.value(0).toString(); // still program crashes here
    2. this->name = test;
    To copy to clipboard, switch view to plain text mode 
    What is happening now ?

  3. The following user says thank you to Lesiok for this useful post:

    rahulvishwakarma (4th April 2020)

  4. #3
    Join Date
    Jun 2014
    Posts
    48
    Thanks
    6
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: qstring assignment crashes the program

    thanks a lot you solved my problem i did llike this
    Qt Code:
    1. QString name = qry.value(0).toString();
    2. this->name = name;
    3.  
    4. int n = qry.value(1).toInt();
    5. this->stock = n;
    6.  
    7. float r = qry.value(2).toInt();
    8. this->rate = r;
    9.  
    10. int number = qry.value(3).toInt();
    11. this->number = number;
    To copy to clipboard, switch view to plain text mode 
    but i don't get the point how is it happening, Please explain.

  5. #4
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,540
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: qstring assignment crashes the program

    It makes no sense to me.
    By the way.
    1. You are not guaranteed that a SELECT * query will return the record columns in the specified order. Either construct the query by explicitly entering the column names or retrieve the values later via QSqlRecord and field names.

    2. There should be no toFloat() on line number 7 ?

Similar Threads

  1. Crash on QString assignment
    By ce_nort in forum Newbie
    Replies: 5
    Last Post: 25th March 2016, 16:08
  2. QString exception while executing assignment operation
    By Giox79 in forum Qt Programming
    Replies: 1
    Last Post: 2nd March 2015, 12:30
  3. Replies: 6
    Last Post: 29th December 2011, 18:37
  4. QString assignment
    By valgaba in forum Qt Programming
    Replies: 4
    Last Post: 25th April 2010, 17:31
  5. QMap <int, QGuardedPtr<Employee> > Crashes on Assignment ???
    By sunil.thaha in forum Qt Programming
    Replies: 2
    Last Post: 16th March 2006, 07:09

Tags for this Thread

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
  •  
Qt is a trademark of The Qt Company.