[SOLVED] Access a cellwidget of a tablewidget..
-
wrote on 30 Sept 2013, 10:46 last edited by
So lets say we have a tablewidget and we have added a qtimeedit to a cellwidget of the tablewidget via
@QTimeEdit *end_time=new QTimeEdit(this);
end_time->setTime(QTime(12,04));
ui->tableWidget->setCellWidget(0, 3, end_time);@How can i access it later to see the current qtime?
-
wrote on 30 Sept 2013, 10:52 last edited by
First option is to keep track of the widgets yourself, of course. But you can also access them using something like:
@
QModelIndex index = tableWidget->model()->index(row, column);
QWidget* theCellWidget = tableWidget->indexWidget(index);
@ -
wrote on 30 Sept 2013, 12:20 last edited by
[quote author="Andre" date="1380538325"]First option is to keep track of the widgets yourself, of course. But you can also access them using something like:
@
QModelIndex index = tableWidget->model()->index(row, column);
QWidget* theCellWidget = tableWidget->indexWidget(index);
@
[/quote]Yes but u can't use thecellwidget->time() because we didn't declared that the cellwidget it a qtimeedit..
I tried someting like
@QModelIndex index = ui->tableWidget->model()->index(i, 2);
QTimeEdit* theCellWidget = ui->tableWidget->indexWidget(index);@but gives an error of
@C2440: 'initializing' : cannot convert from 'QWidget *' to 'QTimeEdit *'
Cast from base to derived requires dynamic_cast or static_cast@ -
wrote on 30 Sept 2013, 12:56 last edited by
Well, the error is quite clear, I'd say. It even proposes a solution... Note that you won't have this problem if you keep track of the widgets yourself.
-
Hi,
You need to use qobject_cast, (since you are using QObject derived classes) you can't just assign a pointer from one type to another like that
-
wrote on 30 Sept 2013, 13:16 last edited by
Yep this worked
@QModelIndex index = ui->tableWidget->model()->index(i, 2);
QTimeEdit* theCellWidget = qobject_cast<QTimeEdit *>(ui->tableWidget->indexWidget(index));
qDebug () << theCellWidget->time().toString();@;)
-
wrote on 30 Sept 2013, 13:31 last edited by
Line 3 will cause a crash if line 2 failed. Always check the result of a dynamic cast (and qobject_cast is a dynamic cast)!
1/7