Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy doesnt the old widget get removed?
    primarykey
    data
    text
    <p>Because of the mouseEvent, I would expect the red and blue widgets to switch places on every click. Instead the red switches to the blue and then it never switches back, why?</p> <pre><code>Frame *red = NULL; Frame *blue = NULL; bool isRed = true; Frame::Frame(QWidget *parent) : QFrame(parent) { } Frame::~Frame(){ printf("deleted.\n"); fflush(0); } void QLayout_clear(QLayout* layout, bool deleteWidgets){ QLayoutItem* item; QLayout* childLayout; while ((item = layout->takeAt(0)) != NULL){ QWidget* widget = item->widget(); if (widget != NULL){ layout->removeWidget(widget); if (deleteWidgets){ delete widget; } } else if (childLayout = item->layout()){ QLayout_clear(childLayout, deleteWidgets); } //delete item; } } Widget::Widget(QWidget *parent) : QWidget(parent) { QVBoxLayout *layout = new QVBoxLayout; this->setLayout(layout); red = new Frame; red->setFixedSize(100,100); red->setStyleSheet("background-color:red"); blue = new Frame; blue->setFixedSize(100,100); blue->setStyleSheet("background-color:blue"); layout->addWidget(red); } void Widget::mouseReleaseEvent(QMouseEvent *){ printf("clicked.\n"); fflush(0); QVBoxLayout *layout = (QVBoxLayout *)this->layout(); if (1){ //it doesnt matter if this is 1 or 0 delete layout; layout = new QVBoxLayout; this->setLayout(layout); } else { QLayout_clear(layout, false); } if (isRed){ layout->addWidget(blue); isRed = false; } else { layout->addWidget(red); isRed = true; } }</code></pre> <p>Note: here I am using a simple QFrame for the widgets to switch, in my application the widgets are much more complicated and I cannot recreate them every time I want to swap.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload