SOLVED: Edit dialog from MainWindow



  • Hello,

    I have been trying to set the text of a dialog/designer form that has no class but is called from the MainWindow class when a dialog button is clicked. If it's possible how would you got about it? The slot change_text() sets the content for ap.lineEdit
    So far the connector won't call change_text() probably because the dialog has been executed already.

    @void MainWindow::launch_dialog()
    {
    QDialog *diag = new QDialog;
    Ui::Dialog ap;
    ap.setupUi(diag);
    diag->setModal(true);
    diag->exec();

    connect(ap.pushButton, SIGNAL(clicked()), this, SLOT(change_text()));
    

    }@


  • Moderators

    You can connect before exec()



  • Connecting before exec() crashes the app. Please look at my change_text() and advise how it's supposed to be

    @void MainWindow::change_text()
    {
    Ui::Dialog av;
    av.pushButton->setText("clicked");
    }@



  • Ok, got it. ap must be global


  • Lifetime Qt Champion

    Hi,
    No ap must not be global.

    What JKSH meant is that you should have moved the connect statement just before calling exec



  • The problem is that declaring another ap inside the slot crashes the application. By global I meant ap should be declared inside the MainWindow class declaration not outside.


  • Moderators

    [quote author="Qkato" date="1376606459"]The problem is that declaring another ap inside the slot crashes the application.[/quote]That crash is not caused by the code you posted. It's probably a bug somewhere else that causes memory corruption.

    [quote]By global I meant ap should be declared inside the MainWindow class declaration not outside.[/quote]Yes, that's what you need to do. The correct term is "member variable", not "global".

    [quote author="Qkato" date="1376519112"]
    @
    void MainWindow::launch_dialog()
    {
    QDialog *diag = new QDialog;
    Ui::Dialog ap;
    ap.setupUi(diag);
    diag->setModal(true);
    diag->exec();

    ...
    }
    @
    [/quote]Your original code has a memory leak -- diag is never deleted. Make sure you clean up properly.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.