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

How to make the "CLOSE" button invisible



  • How can I make the "CLOSE" button of a form invisible.

    I want the user of my program to always close the form through a button that I created. Therefore, I need to make the "X" button invisible.

    How do I do?


  • Lifetime Qt Champion

    @Alexandre-Camelo
    Hi
    Its a virtual function that your base has (QMainWindow)
    To override it ( term used that means to supply your own )
    you simply add it to your class.

    Easy way.
    Go to your .h file and right click on the class name
    and in the refactor menu, select insert function from base

    alt text

    then in the new window, search for close
    alt text

    and put a checkmark in it. Then press Ok.

    then in your class it adds
    protected:
    virtual void closeEvent(QCloseEvent *event) override
    {
    }

    you should then right click closeevent and
    choose the Move to .Cpp
    alt text
    so the body goes to the .cpp
    like

    void MainWindow::closeEvent(QCloseEvent *event)
    {
    }
    
    

    and now you are ready to use it.
    you might need to add
    #include <QCloseEvent> in top of cpp.

    You can call
    event->ignore(); to prevent it from closing if you wish.
    Like if asking user question to close.

    Doing it manually would be to add
    virtual void closeEvent(QCloseEvent *event) override;
    in .h and
    then
    void MainWindow::closeEvent(QCloseEvent *event)
    {
    }
    in cpp.

    Im showing the refactor menu as its easy to override others like mousePress MouseMove, paintEvent etc and get the syntax right first time.

    Note the override used.
    Its a compiler flag that tells it you think you are overwriting a base function and
    it will warn you if you dont. (which is good to know as the goal is to match syntax 100%)


  • Lifetime Qt Champion

    Hi
    You can use setWindowFlags
    https://doc.qt.io/qt-5/qwidget.html#windowFlags-prop
    like

    setWindowFlags ( Qt::CustomizeWindowHint | Qt::WindowTitleHint);

    it will remove min/max and close.
    As far as i know its not possible to just remove the close.

    but try the the example. ( its in Creator directly also)
    https://doc.qt.io/qt-5/qtwidgets-widgets-windowflags-example.html
    and see how your platforms allows them to be combined.

    Its important to understand that Qt does NOT draw the caption and borders or the buttons.
    So depending on your platform, it might be possible or not just to get rid of X

    Can I ask why you want only your button to close the form ?



  • @Alexandre-Camelo
    p_box->setWindowFlags(Qt::Window | Qt::FramelessWindowHint |Qt::WindowCloseButtonHint);
    I think this one help full for you


  • Lifetime Qt Champion

    @Alexandre-Camelo

    I want the user of my program to always close the form through a button that I created. Therefore, I need to make the "X" button invisible.

    Sounds like you want to perform some actions on close? Then rather overwrite closeEvent. Because a Window can also be closed with Alt+F4.

    Regards



  • @mrjj said in How to make the "CLOSE" button invisible:

    Can I ask why you want only your button to close the form ?

    I need some checks to be done before the form is closed.

    I tried to put these checks in the destructor (Form :: ~ Form), but the form is closed before my 'IF' statements (even if the 'delete ui' command is after my instructions).


  • Lifetime Qt Champion

    @Alexandre-Camelo said in How to make the "CLOSE" button invisible:

    I need some checks to be done before the form is closed.

    Then the only correct way is to overwrite closeEvent as I already wrote above :)

    Regards



  • This post is deleted!

  • Lifetime Qt Champion

    @Alexandre-Camelo

    Hi
    The correct way is to use closeEvent as @aha_1980 says

    Even if you remove the X button ctrl +f4 or right-click in taskbar also allows
    user to close app without using your button.

    MainWindow::closeEvent(QCloseEvent *event)
    {
    ... ask / do what you want
    event->accept(); // to have to close or reject to forbid it
    }

    This will always be called no matter how app is terminated.



  • @aha_1980 said in How to make the "CLOSE" button invisible:

    @Alexandre-Camelo

    I want the user of my program to always close the form through a button that I created. Therefore, I need to make the "X" button invisible.

    Sounds like you want to perform some actions on close? Then rather overwrite closeEvent. Because a Window can also be closed with Alt+F4.

    Regards

    I'm new to QT.

    Despite trying to read through the documentation, I still don't understand how to build new events besides the existing slots.

    Can you explain how to do this please?



  • @mrjj said in How to make the "CLOSE" button invisible:

    MainWindow::closeEvent(QCloseEvent *event)
    {
    ... ask / do what you want
    event->accept(); // to have to close or reject to forbid it
    }

    I tried to put this code in my cpp file, but it shows the following error:

    "cadacli.cpp: 156: 10: error: C ++ requires a type specifier for all declarations"

    Should I put in the cpp file or somewhere else?

    As I said, I'm new to QT and haven't learned to handle events other than existing slots.

    (An important detail: I need to CANCEL closing the form if the conditions are not met).


  • Lifetime Qt Champion

    @Alexandre-Camelo
    Hi
    Its a virtual function that your base has (QMainWindow)
    To override it ( term used that means to supply your own )
    you simply add it to your class.

    Easy way.
    Go to your .h file and right click on the class name
    and in the refactor menu, select insert function from base

    alt text

    then in the new window, search for close
    alt text

    and put a checkmark in it. Then press Ok.

    then in your class it adds
    protected:
    virtual void closeEvent(QCloseEvent *event) override
    {
    }

    you should then right click closeevent and
    choose the Move to .Cpp
    alt text
    so the body goes to the .cpp
    like

    void MainWindow::closeEvent(QCloseEvent *event)
    {
    }
    
    

    and now you are ready to use it.
    you might need to add
    #include <QCloseEvent> in top of cpp.

    You can call
    event->ignore(); to prevent it from closing if you wish.
    Like if asking user question to close.

    Doing it manually would be to add
    virtual void closeEvent(QCloseEvent *event) override;
    in .h and
    then
    void MainWindow::closeEvent(QCloseEvent *event)
    {
    }
    in cpp.

    Im showing the refactor menu as its easy to override others like mousePress MouseMove, paintEvent etc and get the syntax right first time.

    Note the override used.
    Its a compiler flag that tells it you think you are overwriting a base function and
    it will warn you if you dont. (which is good to know as the goal is to match syntax 100%)



  • @mrjj said in How to make the "CLOSE" button invisible:

    @Alexandre-Camelo
    Easy way.
    Go to your .h file and right click on the class name
    and in the refactor menu, select insert function from base

    wOoOoOoOoOwwww !!!

    Thank you so much, mrjj!

    Gave me a real QT class.

    Worked perfectly.

    In addition to solving my problem, you solved another question: how to create events in addition to the slots in the control menus.

    Perfect!

    Big hug!


  • Lifetime Qt Champion

    @Alexandre-Camelo
    Glad it worked for you.
    The refactor menu can many tricks. :)



  • @mrjj said in How to make the "CLOSE" button invisible:

    Glad it worked for you.
    The refactor menu can many tricks. :)

    I was going to open another topic, but as it relates to this one, I will ask right here:

    Is it possible to create events like this for controls (line edits, comboboxes, etc)?

    I tried but I could not.


  • Lifetime Qt Champion

    @Alexandre-Camelo
    Hi
    When you say "create events" do you mean to respond to events ?
    Like MousePress and such ?

    Like detect a click on a LineEdit and do something ?



  • @mrjj said in How to make the "CLOSE" button invisible:

    When you say "create events" do you mean to respond to events ?
    Like MousePress and such ?
    Like detect a click on a LineEdit and do something ?

    Yes.

    Example: When a line edit gains focus OR loses focus.

    I noticed that the slots are very few. Many useful events for good programming are missing.


  • Lifetime Qt Champion

    @Alexandre-Camelo
    Ok. well you would normally subclass a QLineEdit and add it to that.
    Give me 5 mins and ill take some shots. Its not complicated if we use the
    wizards again.



  • @mrjj OK.

    Waiting.

    Thanks!


  • Lifetime Qt Champion

    Hi
    Ok we are creating a subclass of QLineEdit so its own widget.
    Just like MainWindow is a subclass of QMainWindow.

    Select
    New File or project from the file menu.

    alt text
    Tell it to make C++ class

    Then we get this window.
    alt text
    Give it a name in class name. ( MyLineEdit here)
    Set the Base class to QWidget ( we change to lineEdit in code)
    Press next and finsihed.

    Now you get a brand new class.

    #include <QWidget>

    class MyLineEdit : public QWidget
    {
    Q_OBJECT
    public:
    explicit MyLineEdit(QWidget *parent = nullptr);

    signals:

    };

    now we want it to be a QLineEdit instead so we change code

    #include <QLineEdit> << other include

    class MyLineEdit : public QLineEdit <<< here we change
    {
    Q_OBJECT
    public:
    explicit MyLineEdit(QWidget *parent = nullptr);

    signals:

    };
    then last change is in .cpp
    we have

    MyLineEdit::MyLineEdit(QWidget *parent) : QWidget(parent)
    {

    }

    but it calls a QWidgetbase so we need to change it

    MyLineEdit::MyLineEdit(QWidget *parent) : QLineEdit(parent) <<< here we changed base class
    {

    }

    Now we have a subclassed QLineEdit. current its 100% like a normal one so lets add focus in/out.
    just like before with right click on the name and then refactor menu.

    alt text
    Note the red arrow. we can ask it to put the bodies in directly so we dont need to move them. (just saw that. doh :)

    Bow we get added

        virtual void focusInEvent(QFocusEvent *event) override;
        virtual void focusOutEvent(QFocusEvent *event) override;
    

    and also bodies in .cpp.

    Now how to use it.

    Option 1.
    You can just
    #include "MylineEdit.h" and then new it as normally
    MyLineEdit * myedit = new MyLineEdit(this);

    However, lets be a bit cool and use a ne feature called Promotion.
    its a replace standard widget with my widget when run and allows to use your custom control in Designer.

    So open mainwindow.ui
    and place a QLineEdit on it.
    Now Right click it and select Promote
    alt text
    alt text
    Type In the name of your custom widget in "Promoted class name" The actual class name we used.
    Then press Add
    Then Press Promote

    Now when you run the app. That standard lineEdit will be your
    MyLineEdit instead.

    To test it. Put something in the the bodies of focus in / and out and see :)

    Sorry took a bit longer than 5 mins ;)



  • @mrjj Thank you one more time!

    I won't try to do that today (that's a lot).

    Tomorrow, I'll do everything calmly and give you feedback.

    You helped me MUCH today.

    Your tips have moved me, A LOT, in my QT learning.

    Big hug!


  • Lifetime Qt Champion

    @Alexandre-Camelo
    Hi
    It looks a lot but i promise when you have done it a few times its not that crazy.
    Its good plan. Just ask if i missed some step or its bugging you.

    Do note we subclassed QLineEdit here.
    To catch events one can also use an eventfilter but subclassing is very useful in
    Qt as you can make own custom widgets that way. So i choose to show that way.

    https://doc.qt.io/qt-5/eventsandfilters.html



  • @mrjj said in How to make the "CLOSE" button invisible:

    @Alexandre-Camelo
    Hi
    It looks a lot but i promise when you have done it a few times its not that crazy.
    Its good plan. Just ask if i missed some step or its bugging you.

    Ok mrjj.

    It worked!

    One more great tip!

    I have some questions:

    1. Since the event is located in another file, I need to create a procedure inside the file where the line edit is located, so that I can, for example, throw focus on another line edit of that form, right?

    2. In this case, I associated the event with a previously existing line edit on the form. So if I need to create focus events for other line edits, do I need to create classes for each of them?

    3. What is the "QFocusEvent * event" pointer for? As I did not use it, the system issues a warning that it is not being used.

    4. Please give me an example of how to use the pointer "QFocusEvent * event"

    5. I created 2 test events, but I want to delete them so that QT doesn't issue warnings. How do I do that? Just delete the .cpp and .h files I created and rebuild?



  • @mrjj said in How to make the "CLOSE" button invisible:

    To catch events one can also use an eventfilter but subclassing is very useful in
    Qt as you can make own custom widgets that way. So i choose to show that way.

    https://doc.qt.io/qt-5/eventsandfilters.html

    About eventfilter, I read the documentation, tried to do it, but couldn't.

    I think it's best to open a new topic about this, okay?

    I will quote you there and await your guidance.

    Thanks again.


Log in to reply