This question already has answers here:
Convert QDate to Qstring?
(2个答案)
去年关闭。
我确实在声明QDate时遇到问题,当我在DB插入函数中将声明的word(value)与该相关值一起使用时,它会继续提示错误。请指导并纠正我。我已经尽力了,但仍然给我带来了问题。那我不知道:(
我的代码:
错误:
屏幕截图:
Screenshot of the Code with Error - View it HERE
但是,如果您希望事情变得简单并且不关心SQL注入攻击,则可以这样做。您已被警告:
另外,您可能需要调整
(2个答案)
去年关闭。
我确实在声明QDate时遇到问题,当我在DB插入函数中将声明的word(value)与该相关值一起使用时,它会继续提示错误。请指导并纠正我。我已经尽力了,但仍然给我带来了问题。那我不知道:(
我的代码:
#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
最佳答案
我认为您应该使用QDate
将QString
转换为toString()
。
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()
返回的字符串的格式。关于c++ - 错误:与“operator +”不匹配(操作数类型为“const QString”和“QDate”),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50141198/
10-11 16:07