c++ - Qt Widget class' parent is always NULL -



c++ - Qt Widget class' parent is always NULL -

i have custom qwidget class called videowidget looks this:

videowidget::videowidget(qwidget *parent) : qwidget(parent) { clickablelabel *smallred = new clickablelabel(this) //... qobject::connect(smallred,signal(clicked()),this,slot(removevideo())); } void videowidget::removevideo(){ //...remove file mainwindow* myparent = qobject_cast<mainwindow*>(this->parent()); qlistwidget* mylist = myparent->getlist(); qlistwidgetitem* item = mylist->currentitem(); mylist->removeitemwidget(item); }

the videowidget widgets created in mainwindow class argument this , added qlistwidget. when clicking on smallred label in videowidget want programme delete file , run code remove widget qlistwidget in mainwindow. problem line mainwindow* myparent = qobject_cast<mainwindow*>(this->parent()); returns null , don't understand why. help much appreciated.

see code, think have similar:

for(int r=0;r<2;r++) { qlistwidgetitem* lwi = new qlistwidgetitem; ui->listwidget->additem(lwi); ui->listwidget->setitemwidget(lwi, new qcheckbox(qstring("checkbox%1").arg(r),this)); qdebug() << ui->listwidget->itemwidget(lwi)->parent() << ui->listwidget->itemwidget(lwi)->parent()->parent() << ui->listwidget->itemwidget(lwi)->parent()->parent()->parent() << ui->listwidget->itemwidget(lwi)->parent()->parent()->parent()->parent(); }

as can see set this parent, first parent qt_scrollarea_viewport too, because qt reparent widget. output of code is:

qwidget(0x27c64260, name = "qt_scrollarea_viewport") qlistwidget(0x27c64240, name = "listwidget") qwidget(0x264bedd8, name = "centralwidget") mainwindow(0x28fdcc, name = "mainwindow")

if have same construction utilize few parent() calling

yes, not beautiful far know qt has no findparent, findchildren

as thuga suggested works not good, videowidget should not know mainwindow. should utilize signals , slots. emit signal videowidget , grab signal in mainwindow (write special slot , remove item in slot). improve magic parent().

c++ qt qwidget

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -