main widget's PaintEvent () method not painting custom widget
-
I have created a main widget and a custom widget. I am calling the custom widget from paintEvent() of main widget. so, my custom widget is not getting painted. But if i call the same custom widget from main widget's constructor then custom widget is getting painted. Please help!
void HomeScreen::paintEvent(QPaintEvent *event){ ...... CustomWidget *c1=new CustomWidget(this,resList[j].name,resList[j].department,j*90,resList[j].contactNo,resList[j].emailId,resList[j].globalId,0,"RK"); QListWidgetItem *item = new QListWidgetItem(); lw->insertItem(lw->size().height(),item); item->setSizeHint(QSize(350,90)); lw->setItemWidget(item,c1); this->update(); }
-
I have created a main widget and a custom widget. I am calling the custom widget from paintEvent() of main widget. so, my custom widget is not getting painted. But if i call the same custom widget from main widget's constructor then custom widget is getting painted. Please help!
void HomeScreen::paintEvent(QPaintEvent *event){ ...... CustomWidget *c1=new CustomWidget(this,resList[j].name,resList[j].department,j*90,resList[j].contactNo,resList[j].emailId,resList[j].globalId,0,"RK"); QListWidgetItem *item = new QListWidgetItem(); lw->insertItem(lw->size().height(),item); item->setSizeHint(QSize(350,90)); lw->setItemWidget(item,c1); this->update(); }
-
@Ritz
Hello and welcome.I don't know why you are trying to do as you are, but a widget's
paintEvent()
is surely not the place to be creating new widgets and inserting them into the UI. Paint events should just paint. -
@JonB Thank you!
I was trying to call my custom widget from a method. The method was creating the custom widget but it was not reflected on the main widget UI.@Ritz
You want/need to create your widget --- or any widget --- from a suitable method/function/place. So indeed, for example, the main widget's constructor is suitable, or from some slot if you only want it constructed in response to some signal. But apaintEvent()
is not a suitable place.Given this, do you actually have any question once you have moved your code elsewhere?