Important: Please read the Qt Code of Conduct -

Have Form, now what?

  • I doing my very 1st QT project. I started the project as a QT Widgets application. After adding a few widgets to the form, I decided to add another form to use as a non-modal dialog. Now I want to use a button from the starting form to open the 2nd (non-modal dialog). (C code for the original form was created when the project began, and I've got a 'clicked' slot function setup for my button). My question is: How is the class for the 2nd form created, and/or how do I get the 2nd form and the code for it to be associated? (I know this is QT-101, but its surprisingly not obvious...

  • For anybody trying to add a 2nd dialog, the solution that worked for me was to do the following from inside QT Creator:

    1. Open or create the project
    2. Right click on the project (top line in 'Projects' view)
    3. Select 'Add New...'
    4. In the 'New File' dialog, Choose Qt -> QT Designer Form Class. This will generate ( *.ui, *.h, and *.cpp) for the new form.
    5. From there you open your mainwindow ui, and select the button you want to open the modal dialog and right click on 'Goto slot'.
    6. Select the 'clicked' slot, and this will add a 'clicked' function to your mainwindow code. Then startup the new modal dialog by editing the button 'clicked' function as follows:

    void MainWindow::on_Button_clicked()
    NewDlg *Dlg = new NewDlg;

    [edit: Added missing coding tags @ SGaist]

  • Lifetime Qt Champion

    Hi and welcome to devnet,

    It's the right technique. However you have now a memory leak with your dialog

  • Thanks for the tip. I'll change the code to make NewDlg a member variable of MainWindow, and then have the WinWindow destructor delete the pointer. (I guess another way would be to have the dialog delete its own space in its destructor?

  • Lifetime Qt Champion

    It depends on how you use your dialog. Is it meant to stay ? Or does the user just answer a question and close it ?

Log in to reply