2014-11-09 9 views
0

VideoWidget이라는 사용자 정의 QWidget 클래스가 있습니다.QListWidget에서 위젯에서 변수 가져 오기

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(); 
    } 

:이 같은 해당 파일에서 문자열 인수를 XML 파일을 통해 반복하고 점점 통해 QListWidgetVideoWidget을 추가 내 MainWindow 클래스에서

VideoWidget::VideoWidget(QWidget *parent, string test) : 
QWidget(parent) 
{ 

    pathname=test; 
    QLabel *label= new QLabel(pathname.c_str(), this); 
    //... 
} 
string VideoWidget::getFilePath(){ 
    return pathname; 
} 

: 그것의 소스 파일은 다음과 같이 보입니다 이렇게하면 QListWidget이 모든 레이블의 값이 다른 VideoWidget으로 올바르게 채워집니다. 지금 나는이 같은 QListWidget의 항목에 내가 더블 클릭 pathname 변수 매번 좀하고 싶습니다 :

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; 
} 

내 문제는 widget->getFilePath() 항상 위젯을 클릭 모든에 대해 동일한 값을 반환한다는 것입니다. 내가 처음으로 pathname=test;을 설정 한 값입니다. 내가 여기서 무엇을 놓치고 있니?

QListWidgetItem* item = ui->myList->itemAt(0,index.row()); 

방법 "itemAt는"하지 인덱스 좌표 x와 y를 취합니다

답변

1

이것은 아마도 실수입니다. 대신에 "takeItem"을 사용하십시오.

ui->myList->itemWidget(item) 

쓸모 : 내가 말하고 싶은 다음 것은이 부분이다. "항목"을 직접 변환 할 수 있습니다. Qt를 사용하기 때문에 마지막으로 qobject_cast를 사용하십시오. 그리고 dynamic_case를 사용하지 마십시오 (특히 어쨌든 NULL에 대한 결과를 확인하지 않는 경우).