0

I have found a similar question but the solution does not resolve my doubt.

I have a QDialog with different items, among them a pointer to a QWidget. This is adequately integrated in the dialog. I would like to know if I can replace the QWidget in the same location. I do not want to hide it, I want to delete the current widget to replace it with the new one.

Community
  • 1
  • 1
Antonio
  • 851
  • 2
  • 8
  • 17

1 Answers1

0

The current approach that I took:

I used a pointer to my current widget (m_pCurrentWdgt). I were using the widget inside of a simple layout (few elements), hence I could control the position of the widget manually. Otherwise I would use a additional support layout (m_pLayoutWidget) to place the widget where I want. The replacement worked as follows: When I want to replace the current widget by a new one (pWidget) I delete the current widget from the support layout and insert the new one in the previous position (e.g. N_WIDGET).

void MyDialog::replaceWidget(QWidget *pWidget) {
     this->m_pLayoutWidget->removeWidget(m_pCurrentWidget);
     delete m_pCurrentWidget;
     m_pCurrentWidget = pWidget;
     this->insertWidget(N_WIDGET, m_CurrentWidget);
}

I would like to avoid using Layout here, but at least it worked for me.

Antonio
  • 851
  • 2
  • 8
  • 17