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. Executable crashed!
Forum Updated to NodeBB v4.3 + New Features

Executable crashed!

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 3 Posters 726 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.
  • Rizlan AnilR Offline
    Rizlan AnilR Offline
    Rizlan Anil
    wrote on last edited by
    #1

    Hi everyone,

    Here's the code :

    title.png

    So after finishing the time the button got deleted but it crashes after that and doesn't show the label!
    Can you guide me where's the problem?

    Thank you.

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

      The timer is not triggered just once, but every 2000ms. The first time you delete the button, but you don't null the pointer, so the second time the timer is triggered it tries to again delete that same button. The pointer now points to garbage memory, so the app crashes.
      In short null the pointer after you delete it:

      delete button;
      button = nullptr;
      

      This will prevent the crash, but keep in mind that you will get another new label every 2000ms.
      If you want the timer to trigger only once, use static QTimer::singleShot instead.

      1 Reply Last reply
      3
      • Rizlan AnilR Offline
        Rizlan AnilR Offline
        Rizlan Anil
        wrote on last edited by
        #3

        Eum i see now the issue .. because i thought the timer stops when the time ends i didn't know it keeps restarting!!

        Thank you Chris Kawa :]

        1 Reply Last reply
        0
        • Rizlan AnilR Offline
          Rizlan AnilR Offline
          Rizlan Anil
          wrote on last edited by
          #4

          ah problem the label doesn't show if i create it in a function:

          one.png

          and if i initialize it in the constructor it shows only the first letter from the label !!

          two.png

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

            When you add a widget in the constructor it is shown along with its parent when you call show() on the parent (typically in your main() function for top level widgets).

            When you add a widget to already visible parent (like in your replaceButtonByLabel() function), the widget is not visible by default. You need to call show() on it explicitly.

            As for the first letter only - the label is probably either too small to show everything or covered by something else. For a simple test to see where it is exactly you can give it some visible border:

            label->setStyleSheet("border: 1px solid red");
            

            You can also call it on a parent and it will apply to all the children so you'll see clearly where everything is.

            Anyway, instead of manually managing size and position of the widgets You can look into layouts. They take care of many aspects of positioning, resizing and showing of child widgets within a parent.

            1 Reply Last reply
            2
            • Rizlan AnilR Offline
              Rizlan AnilR Offline
              Rizlan Anil
              wrote on last edited by
              #6

              Yah now the label shows by calling show() in the function... for the first letter only i applied the border and it shows it has the size of the letter only no covering !

              About using layouts instead of manually managing things .. because i'm new to programming so i need to understand first you know!

              Thank you for your explanation :]

              jsulmJ 1 Reply Last reply
              0
              • Rizlan AnilR Rizlan Anil

                Yah now the label shows by calling show() in the function... for the first letter only i applied the border and it shows it has the size of the letter only no covering !

                About using layouts instead of manually managing things .. because i'm new to programming so i need to understand first you know!

                Thank you for your explanation :]

                jsulmJ Offline
                jsulmJ Offline
                jsulm
                Lifetime Qt Champion
                wrote on last edited by
                #7

                @Rizlan-Anil Just a tip for the future: it is better to post your code as text instead of screen-shots. This way others can copy and adjust it if they have suggestions.

                https://forum.qt.io/topic/113070/qt-code-of-conduct

                Rizlan AnilR 1 Reply Last reply
                0
                • jsulmJ jsulm

                  @Rizlan-Anil Just a tip for the future: it is better to post your code as text instead of screen-shots. This way others can copy and adjust it if they have suggestions.

                  Rizlan AnilR Offline
                  Rizlan AnilR Offline
                  Rizlan Anil
                  wrote on last edited by
                  #8

                  @jsulm Ah ok thank you :]

                  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