Solved How to make the "CLOSE" button invisible
-
@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 basethen in the new window, search for close
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
so the body goes to the .cpp
likevoid 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 basewOoOoOoOoOwwww !!!
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!
-
@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.
-
@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.
-
@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. -
-
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.
Tell it to make C++ classThen we get this window.
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 haveMyLineEdit::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.
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
Type In the name of your custom widget in "Promoted class name" The actual class name we used.
Then press Add
Then Press PromoteNow 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!
-
@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. -
@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:
-
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?
-
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?
-
What is the "QFocusEvent * event" pointer for? As I did not use it, the system issues a warning that it is not being used.
-
Please give me an example of how to use the pointer "QFocusEvent * event"
-
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.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.