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. [SOLVED] can't delete the dialog on dialog close
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] can't delete the dialog on dialog close

Scheduled Pinned Locked Moved General and Desktop
7 Posts 3 Posters 6.7k Views 1 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.
  • K Offline
    K Offline
    kalster
    wrote on last edited by
    #1

    i have a small app that has a dialog and two buttons on the dialog. one button when clicked will write "test" to the dialogs widget's textedit, while the other button closes the dialog. the problem is when "test" is displayed in the textedit and i close and reopen the dialog, the word "test" is still displayed in the textedit. when i try to delete the dialog on close, the program crashes when i try to reopen the dialog. below is the code that i am using to close the dialog. i am trying to clear the word "test" from the textedit by trying to delete the dialog when it is closed.

    mainwindow.cpp
    @#include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include "dialog.h"

    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);
    dia = new Dialog(this);
    dia->setAttribute(Qt::WA_DeleteOnClose);
    }

    MainWindow::~MainWindow()
    {
    delete ui;
    }

    void MainWindow::on_pushButton_clicked()
    {

    dia->show();
    

    }@

    dialog.cpp
    @#include "dialog.h"
    #include "ui_dialog.h"

    Dialog::Dialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Dialog)
    {
    ui->setupUi(this);
    }

    Dialog::~Dialog()
    {
    delete ui;
    }

    void Dialog::on_pushButton_clicked()
    {
    ui->textEdit->append("test");
    }

    void Dialog::on_pushButton_2_clicked()
    {
    this->close();
    }@

    1 Reply Last reply
    0
    • F Offline
      F Offline
      fluca1978
      wrote on last edited by
      #2

      [quote author="kalster" date="1323414648"]the problem is when "test" is displayed in the textedit and i close and reopen the dialog, the word "test" is still displayed in the textedit. when i try to delete the dialog on close, the program crashes when i try to reopen the dialog. below is the code that i am using to close the dialog. i am trying to clear the word "test" from the textedit by trying to delete the dialog when it is closed.
      @[/quote]

      The word is still there because you haven't re-initialized the dialog and its widgets status. You have to clear all the widgets, if this is your aim, before you make the dialog visible.
      And if you delete the dialog on close it is normal that the program crashes if you don't re-allocate the dialog.

      1 Reply Last reply
      0
      • K Offline
        K Offline
        kalster
        wrote on last edited by
        #3

        the problem seems to be that the Dialog::Dialog function is only read once. i need it to be read each time the dialog is opened. i guess i can use a showevent function.

        1 Reply Last reply
        0
        • F Offline
          F Offline
          fluca1978
          wrote on last edited by
          #4

          Of course it is read once, it is a class constructor and it is read each time you create a new object. You have to modify your code something like this:

          @
          Dialog::clear(){ ui->textEdit->setText(""); }
          @

          and

          @void MainWindow::on_pushButton_clicked()
          {
          dia->clear();
          dia->show();
          }@

          if you want to use the same dialog over and over. Or with something like this:

          @void MainWindow::on_pushButton_clicked()
          {
          dia = new Dialog( this );
          dia->show();
          }@

          if you want to use each time a new dialog. Of course in such case you have to remove other dialog creations in the constructor of your main window.

          1 Reply Last reply
          0
          • K Offline
            K Offline
            kalster
            wrote on last edited by
            #5

            thank you fluca1978. that helped. i marked this topic as solved. :)

            1 Reply Last reply
            0
            • G Offline
              G Offline
              goetz
              wrote on last edited by
              #6

              The design is massively wrong. You have a class member pointing to a Dialog - or not pointing to a Dialog but to a bunch of garbage bytes. It depends whether the Dialog is deleted.

              If you click on the MainWindow's button a second time while the dialog is open, you do not show that dialog, but open a new one - I doubt that this is what you want.

              So, either keep dia as member of your MainWindow, but do not destroy it on close in order to be able to reuse it on a second click while it is open, or remove the dia member - as you do not need the pointer anywhere else there's no reason to keep it in the class.

              http://www.catb.org/~esr/faqs/smart-questions.html

              1 Reply Last reply
              0
              • K Offline
                K Offline
                kalster
                wrote on last edited by
                #7

                thank you Volker. i will have a look into that.

                1 Reply Last reply
                0

                • Login

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