0
votes

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.

2

2 Answers

4
votes
  1. Syntax of dynamic_cast is

    VideoWidget *widget = dynamic_cast<VideoWidget*>(ui->myList->itemWidget(item));
    
  2. You should probably use qobject_cast instead, since that is a QObject:

    VideoWidget *widget = qobject_cast<VideoWidget*>(ui->myList->itemWidget(item));
    
  3. Add code, at least Q_ASSERT(widget);, after the cast to verify that cast was successful (returns nullptr for failed cast).

2
votes

Your line does not make sense. <dynamic_cast> is not valid C++. It is an invalid name, and can't be a template parameter, since no template expecting function/class is before it.

It would have been dynamic_cast<VideoWidget*> (ui->myList->itemWidget(item) ) in C++

However, Qt defines it own casting function, so you should use qobject_cast<VideoWidget*>(ui->myList->itemWidget(item) )