error: no match for 'operator+' (operand types are 'const QString' and 'QDate')
error: no match for 'operator+' (operand types are 'const QString' and '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
我认为您应该使用 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()
返回的字符串的格式。
我在声明 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
我认为您应该使用 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()
返回的字符串的格式。