-1

For example I have 2 widgets and I press button on first widget. I need to delete first widget and create new widget. How is it possible? I mean some structure for this. I used stackedwidgets, but pages from stackedwidgets located in memory. I need to avoid this.

void Window::on_registrationButton_clicked(){
    ui->logWindow->hide();
    ui->RegistrWindow->show();
}
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • structure or something for this – Даниил Агеев Nov 05 '17 at 22:26
  • Show your code, questions of this type are not appropriate. You must provide a [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve). I also recommend you read the content of the following link to improve the quality of your question: [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask) – eyllanesc Nov 05 '17 at 22:28
  • what is `weedget`? – eyllanesc Nov 05 '17 at 22:33
  • I read this. I do not understand you. I have code witch stackedwidgets and I can sweetiching between pages, but this pages stored in memory. I asked how avoid this. – Даниил Агеев Nov 05 '17 at 22:42
  • I know about delete widgets. But I need to create new widget, when button was preseed. I do not undertand how to make it. – Даниил Агеев Nov 05 '17 at 22:51
  • [Edit your question](https://stackoverflow.com/posts/47127297/edit) and add that information to help you. – eyllanesc Nov 05 '17 at 22:56
  • What I'm understanding is that you have a QStackedWidget where you have 2 widgets and you want to replace the first with another widget. I am right?. QStackedWidget is used to store all the widgets and show only one so I see the code you are showing unnecessary, that code is replaced `your_QStackedWidget->setCurrentWidget(ui->RegistrWindow)`. – eyllanesc Nov 05 '17 at 23:19
  • I add code for you. You asked. This code is not satisfied me. Look, I have 2 classes: LogWindow, RegWindow. In LogWindow i have button Registation. When I click this button I want to create RegWindow and close + delete ( use destructor ) LogWindow. How to make it? – Даниил Агеев Nov 05 '17 at 23:32
  • In your question you should have indicated that you did not like that method, I have published an answer, test it and if it worked, do not forget to mark it as correct, if it does not work tell me what inconveniences you have had. – eyllanesc Nov 06 '17 at 00:00

1 Answers1

0

As you are going to eliminate the object you can not do it within the same class the object belongs to, you have to do it outside of it, for example in the following code I created a signal that is triggered when the button is pressed, this I have connected it to a lambda function where the new object is created and the object that emits it is eliminated.

class LogWindow: public QWidget{
    Q_OBJECT
public:
    LogWindow(const QString &text, QWidget *parent=Q_NULLPTR):QWidget(parent){
        setLayout(new QVBoxLayout);
        btn = new QPushButton(text, this);
        layout()->addWidget(btn);
        connect(btn, &QPushButton::clicked, this, &LogWindow::customSignal);
    }
signals:
    void customSignal();
private:
    QPushButton *btn;
};

class RegWindow : public QWidget{
    [...]
};

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    LogWindow *log= new LogWindow("LogWindow");
    RegWindow *reg;
    QObject::connect(log, &LogWindow::customSignal, [&reg, &log](){
        reg = new RegWindow("RegWindow");
        reg->show();
        log->deleteLater();
    });
    log->show();
    return a.exec();
}

#include "main.moc"

The complete example can be found in the following link

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • _As you are going to eliminate the object you can not do it within the same class the object belongs to_ Why not? With _Qt::WA_DeleteOnClose_ attribute set the widget could call its own close method essentially deleting itself. – scopchanov Nov 06 '17 at 01:50
  • @scopchanov According to what I understand from the documentation is that the object will be deleted after the event is accepted in QCloseEvent, it does not indicate that it will be inside the class, only that it will be eliminated later. It could indicate the proofs of his affirmation. Also what I wanted to say is that as you are going to create a new window this should not be inside the class that is going to be deleted. – eyllanesc Nov 06 '17 at 02:30
  • Take a look at [that](https://github.com/scopchanov/SwitchWidgets) and tell me what you think. – scopchanov Nov 06 '17 at 04:51
  • @scopchanov I understand your point, and my words are imprecise, what I meant to say that window2 should not be an attribute of the class in window1. Your code is elegant. :P – eyllanesc Nov 06 '17 at 04:55
  • Thank you for the compliment! I appreciate that. But let me ask to clarify the matters for myself (because I am learning as well). Did you mean that window (or widget) 2 should not be parented to widget 1, because in that case widget 2 will become deleted together with window 1? – scopchanov Nov 06 '17 at 05:06