I have a custom QWidget class called VideoWidget
which I used to populate my QListWidget ui->myList
. Doubleclicking on any item of the list should give me its VideoWidget
.
connect(ui->myList,SIGNAL(doubleClicked(QModelIndex)),this,SLOT(playClip(QModelIndex)));
void MainWindow::playClip(QModelIndex index){
QListWidgetItem* item = ui->myList->itemAt(0,index.row());
VideoWidget widget = <dynamic_cast>(VideoWidget*)( ui->myList->itemWidget(item) );
cout << "custom widget data" << widget.getMyData() << endl;
}
It won't let me compile the line VideoWidget widget = <dynamic_cast>(VideoWidget*)( ui->myList->itemWidget(item) );
. I'm not sure what I am missing here.
Syntax of dynamic_cast
is
VideoWidget *widget = dynamic_cast<VideoWidget*>(ui->myList->itemWidget(item));
You should probably use qobject_cast
instead, since that is a QObject:
VideoWidget *widget = qobject_cast<VideoWidget*>(ui->myList->itemWidget(item));
Add code, at least Q_ASSERT(widget);
, after the cast to verify that cast was successful (returns nullptr
for failed cast).