qstring assignment crashes the program
hi to all, I've centso7.5 and Qt 5.7 in VM. i am tryng to build a program having following code
problem is that when assigning a query value in to a QString variable, program crashes. how to solve it.
mine function is :-
-------------------
Code:
product_record* product_record::product_search(int numb)
{
QString sql
= "select * from tableProductRecords where number = ?";
qry.prepare(sql);
qry.bindValue(0, numb);
if(qry.exec())
{
if(qry.next())
{
this->name = qry.value(0).toString();// here program crashes
stock = qry.value(1).toInt();
rate = qry.value(2).toFloat();
number = qry.value(3).toInt();
}
}
return this;
}
this is mine header file :-
---------------------------------
Code:
#ifndef PRODUCT_RECORD_H
#define PRODUCT_RECORD_H
#include <QObject>
#include <QString>
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QVariant>
#include <QMessageBox>
#include <QSqlError>
class product_record
: public QObject{
Q_OBJECT
int stock;
float rate;
int number;
public:
product_record();
~product_record();
product_record * product_search(int numb);
void product_modify(int no);
// getters and setters
int text_stock();
float text_rate();
int text_number();
void setText_stcok(int *i);
void setText_rate(float *f);
void setText_number(int *n);
};
#endif // PRODUCT_RECORD_H
Re: qstring assignment crashes the program
Where do you call it? Are you sure the instance is valid?
Re: qstring assignment crashes the program
i've called in different form "dialogAddNewRecord" and a button slot and having this private memeber :- ( product_record *prdRecord )
Code:
void dialogAddNewRecord::on_pushButtonSearchByNumber_clicked()
{
QString str
= ui
->lineEditNumber
->text
().
trimmed();
int s = str.toInt();
if(s)
{
product_record * prd = prdRecord->product_search(s);
ui->lineEditName->setText(prd->text_name());
ui
->lineEditStock
->setText
(QString::number(prd
->text_stock
()));
ui
->lineEditRate
->setText
(QString::number(prd
->text_rate
()));
ui
->lineEditNumber
->setText
(QString::number(prd
->text_number
()));
}
else
{
QMessageBox::information(this,
"on_pushButtonSearchByNumber_clicked",
"number not valid");
}
}
Re: qstring assignment crashes the program
Where do you initialize prdRecord ?
Re: qstring assignment crashes the program
after your queston i initialized prdRedord here but having same problem:-
constructor is :-
----------------
Code:
product_record::product_record()
{
this->name = "";
this->stock = 0;
this->rate = 0.0;
this->number = 0;
}
function is :-
Code:
void dialogAddNewRecord::on_pushButtonSearchByNumber_clicked()
{
QString str
= ui
->lineEditNumber
->text
().
trimmed();
int s = str.toInt();
prdRecord = new product_record();
if(s)
{
product_record * prd = product_record::product_search(s, prdRecord);
ui->lineEditName->setText(prd->text_name());
ui
->lineEditStock
->setText
(QString::number(prd
->text_stock
()));
ui
->lineEditRate
->setText
(QString::number(prd
->text_rate
()));
ui
->lineEditNumber
->setText
(QString::number(prd
->text_number
()));
}
else
{
QMessageBox::information(this,
"on_pushButtonSearchByNumber_clicked",
"number not valid");
}
}
Re: qstring assignment crashes the program
Code:
product_record::product_search(s, prdRecord);
This can't compile according to your code in the first post.
Please show the backtrace of the crash and the correct source code.
Re: qstring assignment crashes the program
after you questioned ( where you initialized prdRecord) then I modify code and added prdRecord as argument of product_search.
after modification :-
Code:
product_record* product_record::product_search(int numb, product_record *prdRecord)
{
QString sql
= "select * from tableProductRecords where number = ?";
qry.prepare(sql);
qry.bindValue(0, numb);
if(qry.exec())
{
if(qry.next())
{
prdRecord->name = qry.value(0).toString().trimmed();// here program crashes
prdRecord->stock = qry.value(1).toInt();
prdRecord->rate = qry.value(2).toFloat();
prdRecord->number = qry.value(3).toInt();
}
}
return prdRecord;
}
and here i called this with initialization of perdRecord
Code:
void dialogAddNewRecord::on_pushButtonSearchByNumber_clicked()
{
QString str
= ui
->lineEditNumber
->text
().
trimmed();
int s = str.toInt();
prdRecord = new product_record();
if(s)
{
QMessageBox::information(this,
"on_pushButtonSearchByNumber_clicked",
QString::number(s
) );
product_record * prd = product_record::product_search(s, prdRecord);
ui->lineEditName->setText(prd->text_name());
ui
->lineEditStock
->setText
(QString::number(prd
->text_stock
()));
ui
->lineEditRate
->setText
(QString::number(prd
->text_rate
()));
ui
->lineEditNumber
->setText
(QString::number(prd
->text_number
()));
}
else
{
QMessageBox::information(this,
"on_pushButtonSearchByNumber_clicked",
"number not valid");
}
}
Re: qstring assignment crashes the program
Please also show the backtrace of the crash
Re: qstring assignment crashes the program
Quote:
after you questioned ( where you initialized prdRecord) then I modify code and added prdRecord as argument of product_search.
After reading many of your posts in this forum, I wonder if you really understand C++. Randomly adding arguments that point to the same type as the class itself, and then returning that same pointer as a result is completely confused and circular logic.
And as ChristianErlicher said, this statement is incorrect and should not compile:
Code:
product_record * prd = product_record::product_search(s, prdRecord);
productSearch() is not a static method of the product_record class, so the only way to correctly call it is using a pointer to a valid instance of the product_record class.
I am also pretty sure that your code has memory leaks. If every time you do a query you create a new instance of product_record, do you delete the old one after you are finished with it, or does the pointer just get lost (and cause a memory leak)?
Re: qstring assignment crashes the program
please tell me "how to get backtrace of crash". and also product_search is static member of class product_record;
Re: qstring assignment crashes the program
Quote:
product_search is static member of class product_record;
Code:
product_record * product_search(int numb);
Not in the code you originally posted. And it makes absolutely no sense to make this method static. Create an instance of your product_record class, and call the product_search() method using that pointer.
Code:
product_record * prd = new product_record( this );
prd->product_search( 42 );
If you are using the debugger to run and test your code, the stack trace is automatically produced when the code crashes. If you are using Qt Creator as your development IDE, the debug output window will be opened when you build and run in Debug mode.
If you aren't using the debugger, then you need to learn how to use it. You cannot find and fix bugs otherwise.
And pay attention to what the compiler is telling you as you build your program. If it warns about uninitialized variables, then that is something you must fix.
Re: qstring assignment crashes the program
as you said I build non-static fuction as follows
Code:
bool product_record::product_search(int numb)//, product_record *prdRecord)
{
QString sql
= "select * from tableProductRecords where number = ?";
qry.prepare(sql);
qry.bindValue(0, numb);
if(qry.exec())
{
if(qry.next())
{
this->name = qry.value(0).toString(); // still program crashes here
this->stock = qry.value(1).toInt();
this->rate = qry.value(2).toInt();
this->number = qry.value(3).toInt();
return true;
}
return false;
}
}
and called in this :-
Code:
void dialogAddNewRecord::on_pushButtonSearchByNumber_clicked()
{
QString str
= ui
->lineEditNumber
->text
().
trimmed();
int s = str.toInt();
prdRecord = new product_record(this);
if(s)
{
prdRecord->product_search(s);
ui->lineEditName->setText(prdRecord->text_name());
ui
->lineEditStock
->setText
(QString::number(prdRecord
->text_stock
()));
ui
->lineEditRate
->setText
(QString::number(prdRecord
->text_rate
()));
ui
->lineEditNumber
->setText
(QString::number(prdRecord
->text_number
()));
}
else
{
QMessageBox::information(this,
"on_pushButtonSearchByNumber_clicked",
"number not valid");
}
}
Re: qstring assignment crashes the program
Change line :
Code:
this->name = qry.value(0).toString(); // still program crashes here
for this lines :
Code:
QString test
= qry.
value(0).
toString();
// still program crashes here this->name = test;
What is happening now ?
Re: qstring assignment crashes the program
thanks a lot you solved my problem i did llike this
Code:
QString name
= qry.
value(0).
toString();
this->name = name;
int n = qry.value(1).toInt();
this->stock = n;
float r = qry.value(2).toInt();
this->rate = r;
int number = qry.value(3).toInt();
this->number = number;
but i don't get the point how is it happening, Please explain.
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 ?