Important: Please read the Qt Code of Conduct -

Refresh entire form upon pressing a button

  • Hi all,

    I have two forms - form1, form2.
    form1 has a QTableWidget which reads and shows xml entries in folder.

    form2 is a dialogue form, collect user data and save as xml.

    I need to implement the following:
    Pressing button1 in form2 save and close the window and immediately update Qtabletwidget in form1 with new values.

    I did the following in form2:

    void FormTwo::on_pushButton_clicked()
    here read user data and save as xml file
    FormOne *updatewindow = new FormOne(this);

    So everytime a "new" FormOne will be created with updated contents. But I think this is a wrong way to proceed this

    Is there a way to implement this with signals and slots?

    Thanks in advance

  • Lifetime Qt Champion

    @russjohn834 What is not clear from your description: should form1 always be there or only after closing form2?
    Who creates form2? If you say "form" do you mean a window or a widget in a window?
    If it is like this: form1 is always there and it creates form2 ,then you can use signals/slots. In form1 when you create form2 instance connect the "update" signal from form2 to a slot in form1. In that slot you then do what needs to be done in form1.

  • @jsulm Thank you.
    I just figured out a way do this without signal & slot.

    I close form 1(btw , it is QMainwindow) when form 2 is created. Then when I press button form 2 will be closed and a new form 1 will be created. Like this:

    this->close(); ---->form 2
    MainWindow *back = new MainWindow(this);

    I hope this is fine.

  • @russjohn834

    MainWindow *back = new MainWindow(this);

    Although permissible, this creates a brand new main window instance, which is "unusual". It will also lose any state you had previously. You're also parenting the new main window off a "form" you have already close()d.

    If you really want to swap windows this way (QStackedWidget may be preferable), consider using show() & hide() instead, rather than total destruction & re-creation. If all you really want is to have one window/form/whatever at a time visible and user clicks to go between them, I think that QStackedWidget is going to be simpler & better for you. And you don't have to have your multiple main windows as the stack: you could have one main window, with its furniture, and have a stacked widget area on it, which is what gets switched.

  • Thank you @JonB for your feedback

Log in to reply