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] The program has unexpectedly finished.
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] The program has unexpectedly finished.

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 2 Posters 2.1k 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.
  • R Offline
    R Offline
    Renn
    wrote on last edited by Renn
    #1

    Please check my code does something wrong? The problem is that when I open the file and close it, and I wanted to try to re-open the file appears this notification "The program has unexpectedly finished."

    getFile = QFileDialog::getOpenFileName(
    0,
    tr("Open your file"),
    "/home/rnd",
    "All File (**);;Text(*.txt)"
    );
    if(!getFile.isEmpty())
    {
    QList <QMdiSubWindow *> subWindows = ui->mdiArea->subWindowList();
    if (subWindows.count() > 0) {
    for (int i = 0; i < subWindows.count(); i++) {
    if (subWindows.at(i)->property("filePath").toString() == getFile) {
    }
    }
    }
    QString cache;
    QFile file(getFile);
    if (file.open(QFile::ReadOnly)) {
    QTextStream in(&file);
    while (!file.atEnd()) {
    cache = in.readAll();
    }
    }
    file.close();

        QFileInfo finfo(file);
        mdiWindow2 = ui->mdiArea->addSubWindow(widget);
        mdiWindow2->setGeometry(5, 5, 300, 250);
        mdiWindow2->setWindowTitle(finfo.baseName());
        mdiWindow2->layout()->addWidget(TextEdit);
        TextEdit->setPlainText(cache);
        mdiWindow2->show();
    
    1 Reply Last reply
    0
    • R Offline
      R Offline
      Renn
      wrote on last edited by
      #2

      Full Code, there are changes a little

      MultipleDocument.pro
      QT += core gui

      greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

      TARGET = MultipleDocument
      TEMPLATE = app

      SOURCES += main.cpp
      multipledocument.cpp

      HEADERS += multipledocument.h

      FORMS += multipledocument.ui

      RESOURCES +=
      Resource.qrc

      QMAKE_CXXFLAGS += -std=c++0x

      multipledocument.h

      #ifndef MULTIPLEDOCUMENT_H
      #define MULTIPLEDOCUMENT_H

      #include <QMainWindow>
      #include <QDebug>
      #include <QMdiSubWindow>
      #include <QWidget>
      #include <QTextEdit>
      #include <QFileDialog>

      namespace Ui {
      class MultipleDocument;
      }

      class MultipleDocument : public QMainWindow
      {
      Q_OBJECT

      public:
      explicit MultipleDocument(QWidget *parent = nullptr);
      ~MultipleDocument();

      private slots:
      void newFile();
      void openFile();

      private:
      Ui::MultipleDocument ui;
      QWidget
      widget;
      QTextEdit* TextEdit;
      QMdiSubWindow* mdiWindow;
      QMdiSubWindow* mdiWindow2;

      QString getFile;
      

      };

      #endif // MULTIPLEDOCUMENT_H

      main.cpp

      #include "multipledocument.h"
      #include <QApplication>

      int main(int argc, char *argv[])
      {
      QApplication a(argc, argv);
      MultipleDocument w;
      w.show();

      return a.exec();
      

      }

      multipledocument.cpp

      #include "multipledocument.h"
      #include "ui_multipledocument.h"

      MultipleDocument::MultipleDocument(QWidget *parent) :
      QMainWindow(parent),
      ui(new Ui::MultipleDocument)
      {
      ui->setupUi(this);
      widget = new QWidget(this);
      //TextEdit = new QTextEdit();
      //TextEdit->setObjectName("textEdit");
      connect(ui->actionNew_File, SIGNAL(triggered()), this, SLOT(newFile()));
      connect(ui->actionOpen_File, SIGNAL(triggered()), this, SLOT(openFile()));
      }

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

      void MultipleDocument::newFile()
      {
      TextEdit = new QTextEdit(widget);
      static int nomer = 1;
      QString nameFile = tr("document %1.txt").arg(nomer++);
      mdiWindow = ui->mdiArea->addSubWindow(widget);
      mdiWindow->setWindowTitle("[*]" + nameFile);
      mdiWindow->setGeometry(5, 5, 300, 250);
      mdiWindow->layout()->addWidget(TextEdit);
      mdiWindow->show();
      }

      void MultipleDocument::openFile()
      {
      getFile = QFileDialog::getOpenFileName(
      0,
      tr("Open your file"),
      "/home/rnd/renaldi",
      "All File (**);;Text(*.txt)"
      );
      if(!getFile.isEmpty())
      {
      QString cache;
      QFile file(getFile);
      if (file.open(QFile::ReadOnly | QFile::Text)) {
      QTextStream in(&file);
      while (!in.atEnd()) {
      cache = in.readAll();
      }
      }

          QFileInfo fileInfo(file);
          mdiWindow2 = ui->mdiArea->addSubWindow(widget);
          mdiWindow2->setGeometry(5, 5, 300, 250);
          TextEdit = new QTextEdit(widget);
          mdiWindow2->setWindowTitle(fileInfo.baseName());
          mdiWindow2->layout()->addWidget(TextEdit);
          TextEdit->setPlainText(cache);
          mdiWindow2->show();
      }
      

      }

      1 Reply Last reply
      0
      • Chris KawaC Offline
        Chris KawaC Offline
        Chris Kawa
        Lifetime Qt Champion
        wrote on last edited by Chris Kawa
        #3

        That's cause you're reusing the widget variable, which is destroyed after you close the first window. Also you're overwriting the TextEdit variable.

        You don't need that extra widget there at all. It only does harm. Also you can have multiple sub-windows opened so you shouldn't put a pointer in a single TextEdit and mdiWindow variables. You don't need these either.
        Change this in newFile():

        TextEdit = new QTextEdit(widget);
        mdiWindow = ui->mdiArea->addSubWindow(widget);
        mdiWindow->layout()->addWidget(TextEdit);
        

        to

        ui->mdiArea->addSubWindow(new QTextEdit());
        

        and accordingly in the openFile().

        You can then access a list of all sub-windows with ui->mdiArea->subWindowList() and get the text edit from the widget() method.

        Some other unrelated pointers:

        • instead of QMAKE_CXXFLAGS += -std=c++0x in the .pro file use CONFIG += c++11, it's compiler agnostic
        • make up your mind about your variable naming scheme. Using both lower case (mdiWindow) an upper case (TextEdit) is confusing. I suggest sticking to Qt's conventions in this area.
        • When posting code surround it with ```, it makes it format better.
        R 1 Reply Last reply
        0
        • Chris KawaC Chris Kawa

          That's cause you're reusing the widget variable, which is destroyed after you close the first window. Also you're overwriting the TextEdit variable.

          You don't need that extra widget there at all. It only does harm. Also you can have multiple sub-windows opened so you shouldn't put a pointer in a single TextEdit and mdiWindow variables. You don't need these either.
          Change this in newFile():

          TextEdit = new QTextEdit(widget);
          mdiWindow = ui->mdiArea->addSubWindow(widget);
          mdiWindow->layout()->addWidget(TextEdit);
          

          to

          ui->mdiArea->addSubWindow(new QTextEdit());
          

          and accordingly in the openFile().

          You can then access a list of all sub-windows with ui->mdiArea->subWindowList() and get the text edit from the widget() method.

          Some other unrelated pointers:

          • instead of QMAKE_CXXFLAGS += -std=c++0x in the .pro file use CONFIG += c++11, it's compiler agnostic
          • make up your mind about your variable naming scheme. Using both lower case (mdiWindow) an upper case (TextEdit) is confusing. I suggest sticking to Qt's conventions in this area.
          • When posting code surround it with ```, it makes it format better.
          R Offline
          R Offline
          Renn
          wrote on last edited by
          #4

          @Chris-Kawa Thank you in advance, this problem has been solved

          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