PDA

View Full Version : QDateEdit in QTableWidget



ceddy
26th February 2012, 14:04
With the following code Put a DateEdit in a Cell of a TableWidget:


QDateEdit *dateEdit = new QDateEdit();
dateEdit->setDate(QDate::currentDate());
ui->twHomeworkList->setCellWidget(ui->twHomeworkList->rowCount()-1,1,dateEdit);


Now I tied to read the Date fromt the Edit:

QString x= static_cast<QDateEdit*>(ui->twHomeworkList->cellWidget(1,y))->date().toString();
But the application just crash.
Can somebody help me?

Greets ceddy

Zlatomir
26th February 2012, 15:32
You need some more parentheses, something like :

QString x = (static_cast<QDateEdit*>(ui->twHomeworkList->cellWidget(1,y)) )->date().toString();
That is because you need to cast what is returned by cellWidget(...) and then call the date() function from a QDateEdit pointer.

//you are safer if you use dynamic_cast first, check to be different then null and only then call functions using the pointer, static_cast should be faster, but you need to be absolutely sure that it doesn't fail

LE: Forget the parenthesis - it should work as you write it... debug your code and make sure that you access and cast the correct coordinates (1, y) that hold the QDateEdit.

Also dynamic_cast with check for returded null pointer is safer.

ceddy
26th February 2012, 15:45
Thanks for the answer, but it doesn't changed anything :(

Zlatomir
26th February 2012, 15:54
Thanks for the answer, but it doesn't changed anything :(
Yes, see the "later edit"

One issue is that you insert DateEdit on last row, and when you try to access it you always get the "QWidget*" from the first row //and the cast fails

ceddy
26th February 2012, 16:13
Sorry doesn't mind that;)
thanks for your help

ceddy