Reputation: 183
I have a custom QWidget class called VideoWidget
. Its source file looks something like this:
VideoWidget::VideoWidget(QWidget *parent, string test) :
QWidget(parent)
{
pathname=test;
QLabel *label= new QLabel(pathname.c_str(), this);
//...
}
string VideoWidget::getFilePath(){
return pathname;
}
In my MainWindow
class I add the VideoWidget
to a QListWidget
through looping through a xml file and getting the string argument from that file like this:
QDomNode node = rootXML.firstChild();
while( !node.isNull() )
{
if( node.isElement() )
{
QDomElement element = node.toElement();
VideoWidget* mytest = new VideoWidget(this, element.attribute( "Pathname", "not set").toStdString());
QListWidgetItem* item = new QListWidgetItem;
item->setSizeHint(QSize(150,100));
ui->myList->addItem(item);
ui->myList->setItemWidget(item,mytest);
}
node = node.nextSibling();
}
This correctly fills my QListWidget
with the VideoWidget
where all the labels have a different value.
Now I'd like to get the pathname
variable everytime I doubleclick on a item in the QListWidget
like this:
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 << widget->getFilePath() << endl;
}
My problem is that widget->getFilePath()
always returns the same value for every clicked widget. It is the value of the first time I set pathname=test;
. What am I missing here?
Upvotes: 0
Views: 359
Reputation: 1177
This is probably mistake:
QListWidgetItem* item = ui->myList->itemAt(0,index.row());
Method "itemAt" takes x and y coordinates, not indexes. Use "takeItem" instead. Next thing I want to say is that this part:
ui->myList->itemWidget(item)
is useless. You can convert "item" directly. And last - use qobject_cast since you use Qt. And never use dynamic_case (especially when you anyway do not check result against NULL).
Upvotes: 1