简体   繁体   中英

read QDate from Qtablewidget and store it on QDate

I have a problem with qdate and qtablewidget.

When I update an item on a qtablewdiget through a connect, I call a function "updateProdotto". I have a problem in reading the new qdate that I insert and in storing it in a new qdate variable.

I have already searched in the web, but without results because nobody does the operation I neeed with the qdate type.

connect(ui->tableViewProdotti,SIGNAL(itemChanged(QTableWidgetItem*)),this,SLOT(updateProdotto())); 

void UserInterface::updateProdotto() {
int colonna = ui->tableViewProdotti->currentColumn();
int riga = ui->tableViewProdotti->currentRow();

if(colonna == 1)
    art[riga]->setNome(ui->tableViewProdotti->item(riga,1)->text().toStdString());
if(colonna == 2)
    art[riga]->setCategoria(ui->tableViewProdotti->item(riga,2)->text().toStdString());
 if(colonna == 5) { // this is for date
   QDate date= // read date and store it
   art[riga]->setDate(date);
 }
} 

How can I do this?

use QDate::fromString and don't forget to specify correct format:

http://doc.qt.io/qt-5/qdate.html#fromString-1

As far as I understand your problem, you would have a date in string and now you would need to use it as a QDate object. In this case, you should parse the string using QDate:fromString static method.

Method signature:

QDate QDate::fromString(const QString & string, const QString & format)

Example:

QDate date = QDate::fromString("1MM12car2003", "d'MM'MMcaryyyy");
// date is 1 December 2003

Documentation: http://doc.qt.io/qt-5/qdate.html#fromString-1

thank you, I already know about "QDate::fromstring" but in my case it doesn't work. I use QDate::fromString in other function of my project but in this case the right solution ( 2 houres for it after read your answers) its:

    QString format="yyyy-MM-dd";
    QTableWidgetItem* date= ui->tableViewProdotti->item(riga,5);
    QString text=date->text();
    QDate date1=QDate::fromString(text,format);
    art[riga]->setDataAcquisto(date1);

My error was on QTableWidgetItem pointer and the format is yyyy-MM-dd ( like standard) and in the other function I used dd.MM.yyyy ( I'm european!)

thank you for your help, I hope this solution can help other people!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM