A strange problem with Dialog window with Buttons that makes the program crash



  • Hi to all!

    I am new to programming (and so to Qt), but the Qt IDE is helping me understand C++ and Qt itself.

    I was "messing up" with creating windows and make them communicate between themselves, so i made a program with its main window with a "line Edit" and a "text Browser" widget, plus a push button to open a new "Dialog with Buttons" with a "text Browser" widget that takes the test from the "line Edit" in the main window; moreover, in this window i inserted a "line Edit" widget form which the "text Browser" in the main window takes its text when i click "Ok" in the Dialog window...
    Well, i encountered a problem that make the program crash, either when i insert an arbitrary string in the widget or when i don't insert any text; strange problem for me, but maybe because i'm a greenhorn...

    However, here is the code:

    form.h

    #ifndef FORM_H
    #define FORM_H
    
    #include <QDialog>
    #include <QString>
    #include <mainwindow.h>
    
    namespace Ui {
    class form;
    }
    
    class form : public QDialog
    {
        Q_OBJECT
    
    public:
        explicit form(QWidget *parent = 0);
        ~form();
        void trec(QString k);
        QString grev();
        bool valueret;
    
    private slots:
        void on_buttonBox_accepted();
    
        void on_buttonBox_rejected();
    
    private:
        Ui::form *ui;
    };
    
    #endif // FORM_H
    

    form.cpp

    #include "form.h"
    #include "ui_form.h"
    #include "mainwindow.h"
    #include <QDebug>
    
    form::form(QWidget *parent) :
        QDialog(parent),
        ui(new Ui::form)
    {
        ui->setupUi(this);
    }
    
    form::~form()
    {
        delete ui;
    }
    
    void form::trec(QString k)
    {
        ui->textBrowser->setPlainText(k);
    }
    
    QString form::grev()
    {
        return (ui->lineEdit->text());
    }
    
    void form::on_buttonBox_accepted()
    {
        qDebug() << "accepted";
        valueret = true;
    }
    
    void form::on_buttonBox_rejected()
    {
        valueret = false;
        qDebug() << "rejected";
    }
    

    mainwindow.h

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    
    #include <QMainWindow>
    #include <form.h>
    #include <QString>
    
    namespace Ui {
    class MainWindow;
    }
    
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    
    public:
        explicit MainWindow(QWidget *parent = 0);
        ~MainWindow();
        void erty(QString m);
    
    
    private slots:
        void on_pushButton_clicked();
    
    private:
        Ui::MainWindow *ui;
    };
    
    #endif // MAINWINDOW_H
    

    mainwindow.cpp

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include "form.h"
    #include <QDebug>
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    
    void MainWindow::erty(QString m)
    {
        ui->textBrowser->setPlainText(m);
    }
    
    void MainWindow::on_pushButton_clicked()
    {
        form *form1 = new form(this);
        form1->trec(ui->lineEdit->text());
        form1->valueret = false;
        form1->setAttribute(Qt::WA_DeleteOnClose);
        //form1->setModal(true);
        form1->exec();
    
        if(form1->valueret) {
            qDebug() << "setted";
            ui->textBrowser->setPlainText(form1->grev());
        }
    }
    

    main.cpp

    #include "mainwindow.h"
    #include <QApplication>
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        MainWindow w;
        w.show();
    
        return a.exec();
    }
    

    I hope that the request is clear enough, and thanks in advance for all the help you can provide!

    PS i use Qt 5.5 32 bit with MinGW 4.9.2 32 bit on Windows 10 x64…

    [edit: added missing coding tags ``` SGaist]



  • @Nixxer said:

    form1->setAttribute(Qt::WA_DeleteOnClose);
    //form1->setModal(true);
    form1->exec();
    
    if(form1->valueret) {
    

    maybe if you do Qt::WA_DeleteOnClose, it will actually delete form1 when it closes, ex. right after exec() returns.
    and then, after you try to read form1->valuerer it is an invalid memory location.

    try with out form1->setAttribute(Qt::WA_DeleteOnClose);


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Using the Qt::WA_DeleteOnClose attribute means that on exec return form1 is going to be destroyed so you are using a pointer that points to an invalid memory address.

    Since you only use form in on_pushButton_clicked, don't allocate it on the heap, the stack is enough and form1 will be destroyed at the end of on_pushButton_clicked.

    Basically:

    form form1;
    form1.trec(ui->lineEdit->text());
    form1.valueret = false;
    //form1.setModal(true);
    form1.exec();
    
    if(form1.valueret) {
        qDebug() << "setted";
        ui->textBrowser->setPlainText(form1.grev());
    }
    

    Hope it helps



  • Well, many thanks for the help!

    Allocating in the stack with "form form1 ..." and without Qt::WA_DeleteOnClose really solves this problem, but i have also discovered that i can destroy form1 with the "delete" function when i need it...

    I'd like to know, which of these two methods is better? Or does it depends on the situations?

    I'd also to ask few questions about how to use the "connect" function, should i make a new thread?


  • Lifetime Qt Champion

    If your dialog should live only as long as the function then put it on the stack, otherwise you'll have to put it on the heap anyway.

    For other unrelated questions better open a new threads so it keeps the forum clean.

    Note that there has already been several threads on how to use connect, you should check them.



  • Thanks again!

    I think this thread can be marked as solved....


Log in to reply
 

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