Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. A strange problem with Dialog window with Buttons that makes the program crash
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 3 Posters 2.8k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    Nixxer
    wrote on 19 Aug 2015, 16:54 last edited by A Former User
    #1

    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]

    1 Reply Last reply
    0
    • Y Offline
      Y Offline
      yoavmil
      wrote on 19 Aug 2015, 19:31 last edited by
      #2

      @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);

      1 Reply Last reply
      0
      • S Offline
        S Offline
        SGaist
        Lifetime Qt Champion
        wrote on 19 Aug 2015, 19:39 last edited by
        #3

        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

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        0
        • N Offline
          N Offline
          Nixxer
          wrote on 20 Aug 2015, 12:15 last edited by
          #4

          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?

          1 Reply Last reply
          0
          • S Offline
            S Offline
            SGaist
            Lifetime Qt Champion
            wrote on 20 Aug 2015, 13:34 last edited by
            #5

            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.

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            0
            • N Offline
              N Offline
              Nixxer
              wrote on 20 Aug 2015, 18:10 last edited by
              #6

              Thanks again!

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

              1 Reply Last reply
              0

              3/6

              19 Aug 2015, 19:39

              • Login

              • Login or register to search.
              3 out of 6
              • First post
                3/6
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved