Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
Here's the code :
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?
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
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 :]
ah problem the label doesn't show if i create it in a function:
and if i initialize it in the constructor it shows only the first letter from the label !!
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.
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 :]
@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.
@jsulm Ah ok thank you :]