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. Very new to QT
Qt 6.11 is out! See what's new in the release blog

Very new to QT

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 4 Posters 860 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.
  • U Offline
    U Offline
    U7Development
    wrote on last edited by
    #1

    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

    sierdzioS 1 Reply Last reply
    0
    • U U7Development

      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

      sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

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

      (Z(:^

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

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

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

        1 Reply Last reply
        4
        • U Offline
          U Offline
          U7Development
          wrote on last edited by
          #4

          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 😁

          mrjjM 1 Reply Last reply
          0
          • U U7Development

            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 😁

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

            @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

            jsulmJ 1 Reply Last reply
            3
            • mrjjM mrjj

              @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

              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on last edited by
              #6

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

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              mrjjM 1 Reply Last reply
              2
              • jsulmJ jsulm

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

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

                @jsulm
                omg yes, i saw it as the constructor :)
                deleted. Thanks.

                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