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. How to make the "CLOSE" button invisible
Forum Updated to NodeBB v4.3 + New Features

How to make the "CLOSE" button invisible

Scheduled Pinned Locked Moved Solved General and Desktop
23 Posts 4 Posters 3.1k Views 2 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.
  • Alexandre CameloA Alexandre Camelo

    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?

    A Offline
    A Offline
    avinash.r.p
    wrote on last edited by
    #3

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

    Alexandre CameloA 1 Reply Last reply
    3
    • Alexandre CameloA Alexandre Camelo

      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?

      aha_1980A Offline
      aha_1980A Offline
      aha_1980
      Lifetime Qt Champion
      wrote on last edited by
      #4

      @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

      Qt has to stay free or it will die.

      Alexandre CameloA 1 Reply Last reply
      2
      • mrjjM mrjj

        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 CameloA Offline
        Alexandre CameloA Offline
        Alexandre Camelo
        wrote on last edited by
        #5

        @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).

        aha_1980A 1 Reply Last reply
        0
        • Alexandre CameloA Alexandre Camelo

          @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).

          aha_1980A Offline
          aha_1980A Offline
          aha_1980
          Lifetime Qt Champion
          wrote on last edited by
          #6

          @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

          Qt has to stay free or it will die.

          1 Reply Last reply
          1
          • A avinash.r.p

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

            Alexandre CameloA Offline
            Alexandre CameloA Offline
            Alexandre Camelo
            wrote on last edited by
            #7
            This post is deleted!
            mrjjM 1 Reply Last reply
            0
            • Alexandre CameloA Alexandre Camelo

              This post is deleted!

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by mrjj
              #8

              @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.

              Alexandre CameloA 1 Reply Last reply
              2
              • aha_1980A aha_1980

                @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

                Alexandre CameloA Offline
                Alexandre CameloA Offline
                Alexandre Camelo
                wrote on last edited by
                #9

                @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?

                mrjjM 1 Reply Last reply
                0
                • mrjjM mrjj

                  @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.

                  Alexandre CameloA Offline
                  Alexandre CameloA Offline
                  Alexandre Camelo
                  wrote on last edited by
                  #10

                  @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).

                  1 Reply Last reply
                  0
                  • Alexandre CameloA Alexandre Camelo

                    @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?

                    mrjjM Offline
                    mrjjM Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on last edited by mrjj
                    #11

                    @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%)

                    Alexandre CameloA 1 Reply Last reply
                    4
                    • mrjjM mrjj

                      @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%)

                      Alexandre CameloA Offline
                      Alexandre CameloA Offline
                      Alexandre Camelo
                      wrote on last edited by
                      #12

                      @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!

                      mrjjM 1 Reply Last reply
                      2
                      • Alexandre CameloA Alexandre Camelo

                        @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!

                        mrjjM Offline
                        mrjjM Offline
                        mrjj
                        Lifetime Qt Champion
                        wrote on last edited by
                        #13

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

                        Alexandre CameloA 1 Reply Last reply
                        2
                        • mrjjM mrjj

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

                          Alexandre CameloA Offline
                          Alexandre CameloA Offline
                          Alexandre Camelo
                          wrote on last edited by
                          #14

                          @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.

                          mrjjM 1 Reply Last reply
                          0
                          • Alexandre CameloA Alexandre Camelo

                            @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.

                            mrjjM Offline
                            mrjjM Offline
                            mrjj
                            Lifetime Qt Champion
                            wrote on last edited by
                            #15

                            @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 ?

                            Alexandre CameloA 1 Reply Last reply
                            0
                            • mrjjM mrjj

                              @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 ?

                              Alexandre CameloA Offline
                              Alexandre CameloA Offline
                              Alexandre Camelo
                              wrote on last edited by Alexandre Camelo
                              #16

                              @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.

                              mrjjM 1 Reply Last reply
                              0
                              • Alexandre CameloA Alexandre Camelo

                                @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.

                                mrjjM Offline
                                mrjjM Offline
                                mrjj
                                Lifetime Qt Champion
                                wrote on last edited by
                                #17

                                @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.

                                Alexandre CameloA 1 Reply Last reply
                                0
                                • mrjjM mrjj

                                  @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.

                                  Alexandre CameloA Offline
                                  Alexandre CameloA Offline
                                  Alexandre Camelo
                                  wrote on last edited by
                                  #18

                                  @mrjj OK.

                                  Waiting.

                                  Thanks!

                                  1 Reply Last reply
                                  0
                                  • mrjjM Offline
                                    mrjjM Offline
                                    mrjj
                                    Lifetime Qt Champion
                                    wrote on last edited by mrjj
                                    #19

                                    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 ;)

                                    Alexandre CameloA 1 Reply Last reply
                                    2
                                    • mrjjM mrjj

                                      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 ;)

                                      Alexandre CameloA Offline
                                      Alexandre CameloA Offline
                                      Alexandre Camelo
                                      wrote on last edited by
                                      #20

                                      @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!

                                      mrjjM 1 Reply Last reply
                                      1
                                      • Alexandre CameloA Alexandre Camelo

                                        @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!

                                        mrjjM Offline
                                        mrjjM Offline
                                        mrjj
                                        Lifetime Qt Champion
                                        wrote on last edited by
                                        #21

                                        @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

                                        Alexandre CameloA 2 Replies Last reply
                                        2
                                        • mrjjM mrjj

                                          @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

                                          Alexandre CameloA Offline
                                          Alexandre CameloA Offline
                                          Alexandre Camelo
                                          wrote on last edited by Alexandre Camelo
                                          #22

                                          @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?

                                          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