错误:'operator +'不匹配(操作数类型是'const QString'和'QDate')

时间:2018-05-02 18:24:22

标签: c++ qt-creator qdate

我确实有声明QDate的问题,当我在数据库插入函数中使用声明的单词(值)时,它会继续提示错误。请指导并纠正我。我已尽了自己的责任,但仍然存在问题;我当时不知道:(

我的代码:

#include "cashregister.h"
#include "ui_cashregister.h"


CashRegister::CashRegister(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::CashRegister)
{
    ui->setupUi(this);

    ui->dateEdit->setDate(QDate::currentDate());

    ui->groupBox_sales->hide();

    ui->pushButton_finish->hide();

    ui->tableView->hide();

    ui->label_totalAmount->hide();

    QTime time = QTime::currentTime();
    qsrand((uint)time.msec());
}

CashRegister::~CashRegister()
{
    delete ui;
}

void CashRegister::on_pushButton_createNewReceipt_clicked()
{
    ui->groupBox_sales->show();

    int bigNo, smallNo;
    bigNo = 100;
    smallNo = 0;

    int randomValue = qrand() % ((bigNo + 1) - smallNo) + smallNo;

    ui->label_receiptNum->setText("AST" + QString::number(randomValue));

    QSqlQueryModel *myModel = new QSqlQueryModel();
    QSqlQuery myqry;
    myqry.prepare("SELECT ItemId FROM Item");
    myqry.exec();
    myModel->setQuery(myqry);
    ui->comboBox_itemID->setModel(myModel);

    myqry.prepare("CREATE TABLE IF NOT EXISTS Receipt (ReceiptId VARCHAR(7) UNIQUE PRIMARY KEY, ReceiptDate DATE, ReceiptAmount DOUBLE)" );
    myqry.exec();
    myqry.prepare("CREATE TABLE IF NOT EXISTS Sold (ReceiptId VARCHAR(7), ItemId VARCHAR(7), SoldQuantity INTEGER, TotalPrice DOUBLE)" );
    myqry.exec();
}

void CashRegister::on_pushButton_next_clicked()
{
    ui->tableView->show();
    ui->pushButton_finish->show();
    ui->label_totalAmount->show();

    QString receiptNo = ui->label_receiptNum->text();
    QString itemSelect = ui->comboBox_itemID->currentText();
    QString qtySold = ui->lineEdit_qtySold->text();
    QDate currentDate = QDate::currentDate();   //<--THIS-->//
    QString totalAmount = ui->label_totalAmount->text();


    double price=0, totalPrice=0; 

    QSqlQuery myqry;
    myqry.exec("SELECT ItemPrice FROM Item WHERE ItemId='"+itemSelect+"' ");
    if (myqry.next())
        price = myqry.value(0).toDouble();

    totalPrice = price * qtySold.toInt();

    QString totalPriceString = QString::number(totalPrice);

    myqry.prepare("INSERT INTO Sold (ReceiptId, ItemId, SoldQuantity, TotalPrice) VALUES ('"+receiptNo+"', '"+itemSelect+"', '"+qtySold+"', '"+totalPriceString+"') ");
    myqry.exec();

    ui->comboBox_itemID->setCurrentIndex(0);
    ui->lineEdit_qtySold->clear();

    myqry.prepare("SELECT Item.ItemId, Item.ItemName, Item.ItemPrice, Sold.SoldQuantity FROM Item, Sold WHERE Item.ItemId = Sold.ItemId AND Sold.ReceiptId='"+receiptNo+"' ");
    myqry.exec();

    QSqlQueryModel *myModel = new QSqlQueryModel();
    myModel->setQuery(myqry);
    ui->tableView->setModel(myModel);

    double sum = 0;
    myqry.exec("SELECT SUM(TotalPrice) FROM Sold WHERE ReceiptId='"+receiptNo+"' ");
    if (myqry.next())
        sum += myqry.value(0).toDouble();

    ui->label_totalAmount->setText("TOTAL AMOUNT: RM" + QString::number(sum));

    //<--THIS-->//
    myqry.prepare("INSERT INTO Receipt (ReceiptId, ReceiptDate, ReceiptAmount) VALUES ('"+receiptNo+"', '"+currentDate+"', '"+totalAmount+"') ");
    myqry.exec();

}

void CashRegister::on_pushButton_finish_clicked()
{
    this->close();
}

错误:

C:\Users\joeboy69\Documents\Workshop 1\ASTECHNOWERKZ\cashregister.cpp:100: error: no match for 'operator+' (operand types are 'const QString' and 'QDate')
     myqry.prepare("INSERT INTO Receipt (ReceiptId, ReceiptDate, ReceiptAmount) VALUES ('"+receiptNo+"', '"+currentDate+"', '"+totalAmount+"') ");
                                                                                                           ^

截图:

Screenshot of the Code with Error - View it HERE

1 个答案:

答案 0 :(得分:1)

我认为您应该使用toString()QDate转换为QString

myqry.prepare(
    "INSERT INTO Receipt (ReceiptId, ReceiptDate, ReceiptAmount) "
    "VALUES (:receipt, :date, :amount) ");
query.bindValue(":receipt", receiptNo);
query.bindValue(":date", currentDate.toString());
query.bindValue(":amount", totalAmount);
query.exec();

但是,如果您希望事情变得简单并且不关心SQL注入攻击,那么您可以这样做。你被警告了:

myqry.prepare(
    "INSERT INTO Receipt (ReceiptId, ReceiptDate, ReceiptAmount) "
    "VALUES ('"+receiptNo+"', '"+currentDate.toString()+"', '"+totalAmount+"') ");

此外,您可能需要调整toString()返回的字符串的格式。