Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. General talk
  3. Qt 6
  4. Calling the parent ui crashes the application

Calling the parent ui crashes the application

Scheduled Pinned Locked Moved Solved Qt 6
7 Posts 3 Posters 841 Views
  • 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.
  • A Offline
    A Offline
    akshaybabloo
    wrote on 28 Dec 2020, 11:43 last edited by
    #1

    I have a main window widget as - Ui_WelcomeScreen.h that's called in WelcomeScreen.h and .cpp. In the welcome screen I am using QListWidget to which I have added a custom widget called Ui_RecentFiles.cpp. I have a button in the custom widget which should get the current row count, I tried something like this:

    auto count = ((WelcomeScreen*)(parent()))->ui->recentFilesView->currentRow();
    qDebug() << count;
    

    whenever I do this, the application crashes. I have no idea what's happening. I also tried doing something like:

    qobject_cast<WelcomeScreen*>(this->parent())->ui->recentFilesView->currentRow()
    

    Still the same issue. Any idea how to do this. Also, I've checked the children of the current parent from the Ui_RecentFiles using - ((WelcomeScreen*)(parent()))->children() - it's giving me a list of QList(RecentFiles(0x7ff2ade63750, name = "RecentFiles").......), I don't think I am actually accessing WelcomeScreen here. Unless I am wrong.

    Any idea how to do this?

    J 1 Reply Last reply 28 Dec 2020, 11:52
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 28 Dec 2020, 12:15 last edited by
      #6

      As I wrote before, you are putting the responsibility on the wrong side.

      Use a signal that requests the deletion of the item and do the handling in that parent class which is responsible for managing your RecentFiles.

      As an analogy, you have an automatic train door issue. The doors do not ask to be closed, they will be when the controller decides it's the right time.

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

      A 1 Reply Last reply 28 Dec 2020, 12:19
      1
      • S Offline
        S Offline
        SGaist
        Lifetime Qt Champion
        wrote on 28 Dec 2020, 11:48 last edited by
        #2

        Hi,

        There are several levels of evil in what you are doing.
        The first being to use parent. A child widget should not care about its parent. If there's something that you try to get from the parent it means that you reverted the order of responsibility which is bad because you are creating a tight coupling that should not exist in the first place.
        Then static casting like that in that situation is bad as well but that one you fixed with qobject_cast.
        You do not check that parent nor the return value of qobject_cast is a non null pointer.

        Now the question is: what do you do in that child widget that requires information from it's parent ?

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

        A 2 Replies Last reply 28 Dec 2020, 11:56
        2
        • A akshaybabloo
          28 Dec 2020, 11:43

          I have a main window widget as - Ui_WelcomeScreen.h that's called in WelcomeScreen.h and .cpp. In the welcome screen I am using QListWidget to which I have added a custom widget called Ui_RecentFiles.cpp. I have a button in the custom widget which should get the current row count, I tried something like this:

          auto count = ((WelcomeScreen*)(parent()))->ui->recentFilesView->currentRow();
          qDebug() << count;
          

          whenever I do this, the application crashes. I have no idea what's happening. I also tried doing something like:

          qobject_cast<WelcomeScreen*>(this->parent())->ui->recentFilesView->currentRow()
          

          Still the same issue. Any idea how to do this. Also, I've checked the children of the current parent from the Ui_RecentFiles using - ((WelcomeScreen*)(parent()))->children() - it's giving me a list of QList(RecentFiles(0x7ff2ade63750, name = "RecentFiles").......), I don't think I am actually accessing WelcomeScreen here. Unless I am wrong.

          Any idea how to do this?

          J Offline
          J Offline
          JonB
          wrote on 28 Dec 2020, 11:52 last edited by
          #3

          @akshaybabloo
          As @SGaist says. Most likely either parent() is nullptr or it's not a WelcomeScreen*. Or something else is wrong. Not too difficult to divide line up and check intermediate results.

          1 Reply Last reply
          1
          • S SGaist
            28 Dec 2020, 11:48

            Hi,

            There are several levels of evil in what you are doing.
            The first being to use parent. A child widget should not care about its parent. If there's something that you try to get from the parent it means that you reverted the order of responsibility which is bad because you are creating a tight coupling that should not exist in the first place.
            Then static casting like that in that situation is bad as well but that one you fixed with qobject_cast.
            You do not check that parent nor the return value of qobject_cast is a non null pointer.

            Now the question is: what do you do in that child widget that requires information from it's parent ?

            A Offline
            A Offline
            akshaybabloo
            wrote on 28 Dec 2020, 11:56 last edited by
            #4

            @SGaist All I want the child widget is to call takeItem() of QListWIdget which is from the parent, aka remove itself.

            1 Reply Last reply
            0
            • S SGaist
              28 Dec 2020, 11:48

              Hi,

              There are several levels of evil in what you are doing.
              The first being to use parent. A child widget should not care about its parent. If there's something that you try to get from the parent it means that you reverted the order of responsibility which is bad because you are creating a tight coupling that should not exist in the first place.
              Then static casting like that in that situation is bad as well but that one you fixed with qobject_cast.
              You do not check that parent nor the return value of qobject_cast is a non null pointer.

              Now the question is: what do you do in that child widget that requires information from it's parent ?

              A Offline
              A Offline
              akshaybabloo
              wrote on 28 Dec 2020, 12:09 last edited by
              #5

              @SGaist I tried doing something like this

              void RecentFiles::on_deleteButton_clicked() {
              
                  this->setParent(nullptr);
                  delete this;
              }
              

              But the UI looks like this:

              Screen Shot 2020-12-29 at 1.08.08 AM.png

              1 Reply Last reply
              0
              • S Offline
                S Offline
                SGaist
                Lifetime Qt Champion
                wrote on 28 Dec 2020, 12:15 last edited by
                #6

                As I wrote before, you are putting the responsibility on the wrong side.

                Use a signal that requests the deletion of the item and do the handling in that parent class which is responsible for managing your RecentFiles.

                As an analogy, you have an automatic train door issue. The doors do not ask to be closed, they will be when the controller decides it's the right time.

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

                A 1 Reply Last reply 28 Dec 2020, 12:19
                1
                • S SGaist
                  28 Dec 2020, 12:15

                  As I wrote before, you are putting the responsibility on the wrong side.

                  Use a signal that requests the deletion of the item and do the handling in that parent class which is responsible for managing your RecentFiles.

                  As an analogy, you have an automatic train door issue. The doors do not ask to be closed, they will be when the controller decides it's the right time.

                  A Offline
                  A Offline
                  akshaybabloo
                  wrote on 28 Dec 2020, 12:19 last edited by
                  #7

                  @SGaist Ya I was looking at this -> https://stackoverflow.com/questions/50083278/in-qt-how-to-remove-child-widget-from-parent-after-receiving-the-signal-generat. I think this is what I have to do.

                  1 Reply Last reply
                  0

                  1/7

                  28 Dec 2020, 11:43

                  • Login

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