Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

how do something on a window before the user open that window?



  • how to disable some parts on a window before it appear ti the user?



  • You could delay load (incubate) or only createComponent when you require, you could set visible: false

    Do you want to give more details on your environment QML / Widgets and clarify your question. There's many solutions - do you know what you want.



  • @6thC

    0_1534372890493_8b468099-ac15-4ecf-a0e0-10354b64e801-image.png
    this work just for the first time
    i want every time the user opens this window it will doAll(false)



  • You have established editUsers CTOR calls correctly doAll(false); , correct?
    Do you know the signal you're interested in? - maybe this http://doc.qt.io/qt-5/qwidget.html#visible-prop ?

    Not really sure what the issue is - seems like you've already done the hard work? Can you elaborate.



  • @6thC forget about disabling some parts
    what i did in the photo runs just one time
    which mean if i open this window it will execute doAll()
    but if i closed the window and open it again it will not execute doAll()
    i will have to exit the whole application to execute it again
    what i want is that every time the user open this window it will execute doAll()
    did you understand me ?



  • I understand you - do you understand me. You've proven it works in the constructor right? So now you just need to call this method again but not at construction time, during another stage of it's life. You currently only have it wired/hooked into object creation time - where you are is the constructor and that only happens when an class is instantiated as an object.

    So. close and open ... probably aren't doing Destruct and Construct. They may be reusing your object and just changing objects states.

    You could:

    • call the method again - hooked into the events that you care about
    • use a new editUsers object

    I don't think you will need to close your whole application at all, that's just crazy. There will be a focus event or something to use, did you go to the link in the last?



  • @6thC yes i understand you,
    but the problem is there is no events related with the window opening
    and doAll() must execute in the window opening before any thing else
    i think that qt have it own methods on opening the windows or showing it



  • did you go to http://doc.qt.io/qt-5/qwidget.html#visible-prop ?

    "See also show(), hide(), isHidden(), isVisibleTo(), isMinimized(), showEvent(), and hideEvent()."

    There's a number of events that may be suitable. I cannot tell from just this definition - could you just use a fresh editUsers instance? I'm sure there's a way to hook this.

    What about http://doc.qt.io/qt-5/qdialog.html#showEvent



  • @6thC thank a lot,
    0_1534415349311_f773974e-c947-403e-b6e5-344c612e8b8e-image.png
    it worked



  • @davidlabib
    You will have read http://doc.qt.io/qt-5/qwidget.html#showEvent & http://doc.qt.io/qt-5/qshowevent.html by now I presume. So just checking you are happy that your doAll(false); will be executed when, say, the window is minimized and then restored? Not just when the user shows the window. So, for example, if your doAll(false) resets widget visibility you will get that on minimize + restore too.



  • @JonB 1. I don't understand you , please explain



  • @davidlabib
    I have explained in the clearest language possible, I cannot think of another way to phrase it.



  • @JonB do you say that in this way if the user minimized the window and then restored it
    it will execute doAll(false) or the reverse ?



  • @davidlabib
    I wrote:

    So just checking you are happy that your doAll(false); will be executed when, say, the window is minimized and then restored?

    How can I explain that more clearly? If the user minimizes your window and then restores it (makes it visible again) showEvent() will be called. And as you have written it that means it will call your doAll(false). Why don't you try it on minimizing and then restoring your window?

    I don't know what your doAll(false) does. Maybe it wants to do that, maybe it does not.



  • @JonB now I understand you ,
    I'm sorry for stupidity ,but english is not my tongue
    I'm using dialog not Mainwondow that's why i didn't try to minimizing and then restoring the window



  • @davidlabib
    I realise about the English, not a problem, I just did not know how to phrase it any better.

    Just check: when the dialog is up, got to your desktop's toolbar (or whatever) and see if you can minimize/restore from there (e.g. Windows allows click on toolbar icon to toggle minimize/restore). If it lets you do that while dialog is showing, see whether your showEvent() in the dialog gets called? If not, you are probably good to go.



  • @JonB you are right
    when i minimize and restore it execute doAll(false)
    is there is another event work when the window opens but not sensitive about minimizing and restoring?


  • Lifetime Qt Champion

    @davidlabib
    Hi
    Do it in constructor as you show - should call it only once and
    then not again on show /minimize etc.
    But normally you create a Dialog show it and delete it
    so constructor is called every time and hence it should be exactly like u want.

    So please show the code where you pop up the editUser Dialog.



  • @davidlabib
    We can look into the minimize/restore if we need to. However for now you should answer @mrjj's comment. Why are you re-showing the dialog? Are you keeping it in existence permanently and hiding/showing it (in which case, why?), or do you destroy and re-create it, in which case you could do your code in constructor instead?



  • @mrjj @JonB
    this is my dialog
    0_1534424096209_336e2a13-d316-471c-8b82-b5c8b2f80d91-image.png

    this is doAll()
    0_1534424167621_f244dbec-bdff-46a1-b9a4-1582fd9e8325-image.png

    it suppose to execute doall(true) when admin password is right .OK?
    so doAll must be false when the window shows
    so the user will be have to fill admin password



  • @davidlabib
    I think you're saying you want all the other other widgets to be enabled/disabled depending on whether Admin Password has or has not been filled in correctly? Or according as it has something/nothing already in it?

    If that is the case you will want to disable/enable as user types into Admin Password? So you will also want to call this during input to that widget? Do you also (sometimes) put something into Admin Password when you display the dialog?

    All in all, why do you only seem to want to call doAll() when window is shown? And you need to answer @mrjj's question: Yes, we can see your dialog, but that does not tell us whether you keep it in existence after showing it or whether you destroy it and construct from scratch?

    I'll give you one tip. I have code which must only be executed the first time a window/dialog is shown, not before then (construction time is too early) and not after then. So my code for showEvent() is like (I don't use C++):

    classMemberVarialbeFirstShown = false;
    
    showEvent()
    {
        if not classMemberVarialbeFirstShown
            doFirstImeStuff();
        classMemberVarialbeFirstShown = true;
    }
    


  • @mrjj no , i don't have to keep it in existence
    i don't mind destroying and re-creating it


  • Lifetime Qt Champion

    @davidlabib
    Ok so im not sure what its not working for you
    If you call doAll(false) in constructor
    then when Dialog is shown they should all be disabled.
    Is that not the case?



  • @davidlabib
    Then you could follow @mrjj's advice to just do your work in the constructor and not worry about show events.

    But you really need to explain precisely when you want those widgets to change between enabled & disabled, because I for one don't know what you intend.



  • the default is disabled
    but when the user input admin password correct it will enabled the widgets
    my problem is to make the widgets disabled by default
    if i put doAll(false) in the constructor when soneone fill admin password correct it will enable
    but if the user closed editUser and open it again it will be enabled without admin password filling



  • @davidlabib

    but when the user input admin password correct it will enabled the widgets

    When? Do you re-verify the password as he types each character into the line edit?

    my problem is to make the widgets disabled by default

    Put the disablement into the constructor.

    if i put doAll(false) in the constructor when soneone fill admin password correct it will enable

    It will not enable unless you tell it to.

    but if the user closed editUser and open it again it will be enabled without admin password filling

    That is the bit where @mrjj was suggesting you destroy the dialog after using it, and re-create a new one each time it's wanted. Then there is no "open again" from wherever it got to, you always start afresh in the constructor.



    1. Yes , verify the password
    2. I don't know how to destroy the dialog after the user close the window


  • @davidlabib

    1.Yes , verify the password

    That does not answer my question. When do you verify the password? As the user types a character at a time (really?)? After he has finished typing (how do you know that?)?

    2.I don't know how to destroy the dialog after the user close the window

    How do you create the dialog in the first place? With new?



  • @JonB

    1. the form will enable after the user type all the character correctly without clicking buttons
      after he type the last char correct it will enable the window

    2. by qt designer form class


  • Qt Champions 2019

    @davidlabib said in how do something on a window before the user open that window?:

    after he type the last char

    how do you know that? What if user enters correct password but then he/she enters one more character?
    "by qt designer form class" - this is not the answer to the question. The question is: how do you create the dialog INSTANCE.
    Like this:

    MyDialog *dialog = new MyDialog(this);
    

    ?



  • @davidlabib
    Further to @jsulm.

    If you do indeed check the password charaacter by character as it's typed, you will need to have a signal/slot for that and call doAll(True) whenever it becomes correct and doAll(False) if it then becomes incorrect. That's how you will do the enablement/disablement dynamically at runtime. I think you asked about that. Note that there's still nothing here about "when the dialog is shown", this is something which must be re-done on each character typed.

    You will create the dialog in one of two ways:

    • If you use new (creates on the heap), you will use delete (or .deleteLater()) to delete it.
    • If you have it as a local variable (not a pointer) in a function it will be on the stack, and it will get destroyed when that function goes out of scope.

    In either of these two cases you will be re-creating/destroying the dialog each time it is used, and so the constructor can be used to reset the initial state without worrying about when the dialog is "shown".



  • @JonB about the last 10 sentences i solved the problem thanks to you.
    But about the first part
    Yes it's signal and slot but
    The widgets is enabled
    And the user will not have to fill any thing in admin password field to do the slot
    So it must disabled from the beginning



  • @davidlabib said in how do something on a window before the user open that window?:

    So it must disabled from the beginning

    Yes, that's why we already said you should call doAll(False) in the contructor so that it starts out disabled, plus call doAll(True)/doAll(False) again as appropriate as each character is typed.



  • @JonB i wasn't know how to do that
    I seted the disable function in the constructor
    and when i was closing the window and open it again doAll wasn't executing
    Iam using "new" by the way
    So it wasn't destroy the dialog
    When i close it
    But now i use delete



  • @davidlabib
    So now you are using new & delete each time you want to show the dialog you should be finding it is correctly calling the constructor to set disabled at the start of every time it gets shown, right?

    More worrying is that you say you were using new but not delete. If you only call the new once and then keep the dialog around and reuse it it's not too bad. But if you keep calling a new and never call a corresponding a delete or .deleteLater() your code will "leak memory" by having separate instances of the dialog sitting around in memory unused forever. If you did not know this it's very important you understand how Qt expects you to code when creating widgets with new. Just saying.


Log in to reply