Unsolved Very new to QT
-
Hi, i am very new to QT...
Do you use pointers to create and initialize properties?..
if not, do you create and initialize them on header or cpp file ?Thanks
-
@U7Development said in Very new to QT:
Do you use pointers to create and initialize properties?..
Depends. You can store normal data in Q_PROPERTY, but you can store pointers, too. So this is really not something that can be answered definitely - all depends on your needs.
if not, do you create and initialize them on header or cpp file ?
Q_PROPERTY can only be used in a header file, or at least I have never seen it placed anywhere else.
I have no idea what you mean by "initialize" in context of a property.
-
Hi and welcome to the forums
If you mean its initial value, don't forget there is a normal variable underneath the
Q_PROPERTY and you can do like you normally do.class MyClass : public QObject { Q_OBJECT Q_PROPERTY(Priority priority READ priority WRITE setPriority) public: enum Priority { High, Low, VeryHigh, VeryLow }; Q_ENUM(Priority) void setPriority(Priority priority) { m_priority = priority; } Priority priority() const { return m_priority; } private: Priority m_priority = High; // set init value to High };
-
Thanks for replies and welcome!
My apologies, i said properties because my fault, i use unreal engine, there i have properties the same as components..
What i want to do is to create a control, for instance a QPushButton from code, inside a class
[code]
class MyWindow : QMainWindow{
//some people used pointers
QPushButton * btn1 = nullptr;//but others standard variable QPushButton btn2;
};
[/ code]Do both ways will serve for QT?
And thanks both for the explanation of Q_PROPERTY, that's new and welcome to learn 😁
-
@U7Development
Hi
Ah. well its actually widgets then.
If you place the button as a member (in .h )
then syntax will work.
QPushButton btn2;That said, if you have it as nonpointer and put it in a layout, you might get a double deletion
as both the layout and the class will try to delete it.So while both syntaxes are fine, the
QPushButton * btn1 = new QPushButton;
are the most commonly used to avoid issues.Do note Qt has a object system that will handle the deletion of Widgets so most of the time no
delete btn1 is needed if you insert into a parent. ( like a form)
explained here
https://doc.qt.io/qt-5/objecttrees.html -
@mrjj said in Very new to QT:
class MyWindow : QMainWindow{
QPushButton btn2; // this is local variable and will be deleted as soon as functions ends
};It's a class not a function :-)
-
@jsulm
omg yes, i saw it as the constructor :)
deleted. Thanks.