ce_nort
ce_nort

Reputation: 168

Crash on QString assignment

I'm pretty new to Qt and C++ so I'm probably missing something obvious, but for the life of me I can't figure out what is going wrong. One of the classes in a program I wrote is causing a crash whenever I try to do a basic string assignment. See below (slightly pared down).

class AutochargeData : public QWidget
{

public:
    AutochargeData(appData *appInfo, QWidget *parent = 0);

    QString orderNum;
    QString paymentMethod;
    QString total;

    void setPayment(QString payment);
    void setOrderID(QString orderID);
    void setTotal(QString total);

    void getPaymentData(QString orderID);

private:
    appData *appInfo;

};

So in the below code, I have tried many variations. I know the query is fine because I've used qDebug() to check the output. I have tried skipping the set functions and doing a direct assignment, I have tried using the "this" keyword, and I have even tried just assigning a random string directly to the variables.

AutochargeData::AutochargeData(appData *appInfo, QWidget *parent) :
    QWidget(parent)
{
    this->appInfo = appInfo;
}

void AutochargeData::getPaymentData(QString orderID)
{    
    QString queryString = "SELECT order_payment.method, order.increment_id, order.grand_total FROM order JOIN order_payment ON sales_flat_order.entity_id = order_payment.parent_id WHERE order.increment_id = '" + orderID + "';";

    QSqlQuery query(queryString, QSqlDatabase::database("db"));
    query.exec();

    while (query.next()) {
        setOrderID(query.value("increment_id").toString());
        setPayment(query.value("method").toString());
        setTotal(query.value("grand_total").toString());
    }
}

void AutochargeData::setPayment(QString payment)
{
    paymentMethod = payment;
}

void AutochargeData::setOrderID(QString orderID)
{
    orderNum = orderID;
}

void AutochargeData::setTotal(QString grandTotal)
{
    total = grandTotal;
}

Any time I tried to assign a string to the orderNum, paymentMethod, or total variables, the program crashes. The debugger is ending on QString::operator=(const char*) in the qstring.h file. As I mentioned, I am relatively new at this so it may be something obvious that I am missing. But after multiple hours of googling and trying different variations, I am stumped. Any thoughts would be much appreciated, and please let me know if there's more information that would be helpful.

Upvotes: 0

Views: 2106

Answers (2)

ce_nort
ce_nort

Reputation: 168

As predicted, it was something obvious that I was missing. I had created an instance of AutochargeData in the Mainwindow class, but was trying to use it in a different class without creating another instance of it. So it was invalid, as was guessed in the comments.

Upvotes: 1

Amr Jer
Amr Jer

Reputation: 55

try to assign the query values to QString like this :

QString str=query.value(0).toString();

 setOrderID(str);

Upvotes: 0

Related Questions