Results 1 to 20 of 24

Thread: Question on QLineEdit Style

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows
    Thanks
    21
    Thanked 418 Times in 411 Posts

    Default Re: Question on QLineEdit Style

    1. If a class is derived from QObject, then usually you create it on the heap using new. If you give the new instance a parent, then the parent will delete it and there is no memory leak. You can give an object a parent in many ways:
    - in the constructor: myObject = new QObject( parentObject );
    - explicitly: myObject->setParent( parentObject );
    - using some other method call that internally sets the parent: parentObject->addWidget( myWidget );, etc.

    Classes derived from QObject always have a constructor that take a "parent" argument.

    2. If a class is not derived from QObject (QColor, QFont, QPen, QString, etc.), you almost always create it on the stack: QPen myPen; and pass it into a method by reference: painter->setPen( myPen ); If you create the instance on the heap using new, then you must also delete it.
    Deciding if an object should be allocated on the heap or stack has nothing and should have nothing to do with the fact its a QObject or not, or any other type of class.
    However:
    scope of usage, performance issues, resource issue should.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,376
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: Question on QLineEdit Style

    Quote Originally Posted by high_flyer View Post
    scope of usage, performance issues, resource issue should.
    Totally agreed.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,349
    Qt products
    Qt5
    Platforms
    Windows
    Thanks
    318
    Thanked 872 Times in 859 Posts

    Default Re: Question on QLineEdit Style

    Nitpicks, you two.

    Sometimes presenting a relatively simple set of rules to help guide a beginner is better. If you are trying to write your first Qt programs and just can't get it to work because you misunderstand basic Qt principles, you aren't worrying about "scope of usage, performance issues, and resource issues."

    When I first started to learn Qt, one of the hardest things to understand was who was in charge of an object's lifetime and whether an object could be stack vs. heap allocated. Understanding the simple rule that "if it is a QObject then its parent will delete it" was a key to end my confusion. A large percentage of the "why doesn't this work?" posts in this forum are exactly because of misunderstanding the Qt parent-child concept and stack vs. heap allocation with respect to object lifetimes.

    Once the programmer figures that out and starts having working programs, that is the time to start thinking about the nuances of performance and scope.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,376
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: Question on QLineEdit Style

    Quote Originally Posted by d_stranz View Post
    Sometimes presenting a relatively simple set of rules to help guide a beginner is better.
    The problem is usually people stop at this point. Then they spread those rules without proper understanding that it is merely a simplification. They write a "tutorial", post it somewhere in the Web and then other people read it, incorrectly apply those rules they learned by reading the tutorial and come here (or elsewhere) claiming that Qt is stupid and difficult becaust it doesn't work the way they read about in the tutorial. Of course totally missing the point that Qt has nothing to do with this.

    If you are trying to write your first Qt programs and just can't get it to work because you misunderstand basic Qt principles, you aren't worrying about "scope of usage, performance issues, and resource issues."
    If one knows the rules of C++, those same rules apply to Qt. If they don't know the rules, they should learn them first before trying to write their first Qt program.

    Once the programmer figures that out and starts having working programs, that is the time to start thinking about the nuances of performance and scope.
    What I'm worried about is that this "once" often never happens.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    May 2012
    Posts
    5
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Question on QLineEdit Style

    @d_stranz
    I have almost the same question when I begin my first Qt program. Your rules are helpful, and can help new Qt programmers to quickly finish their first Qt program without too many critical bugs. This's a very very good way to lead someone to the world of Qt programming.
    @wysota
    In fact, many books are using the same way to help programmers to gradually understand Qt. Just think about what we have learnt when we are 7,8 years old, we are told sqrt(-1) is wrong, but what about 10 years later? I think people would like to learn a certain thing step by step. If a beginner is told the danger of memory leak or some other abstract issues at their first program, they may not serious about those because they don't know what they are and how they come up. Only when they themselves witness the consequences of e.g memory leak will they keep an eye on the problem. So I think study is a process of correction. If one is STUDYING, then "once" will happen, i'm sure.

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,376
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: Question on QLineEdit Style

    Quote Originally Posted by comuslxl View Post
    In fact, many books are using the same way to help programmers to gradually understand Qt.
    Two facts:
    1. People don't read books nowadays, they read tutorials
    2. If they do read a book, they read the first chapter and then they put the book aside because they are excellent programmers now.

    Just think about what we have learnt when we are 7,8 years old, we are told sqrt(-1) is wrong, but what about 10 years later?
    When you are 8 years old, you do not try to calculate sqrt(-1). People do try to use everything C++/Qt offer starting from the first day of their programming life. Just read 100-200 random threads on this forum and you'll come to similar conclusions.

    If one is STUDYING, then "once" will happen, i'm sure.
    Yes... "if"... And yet people come here and don't know the difference between C++ and Qt or between a compiler and an IDE (e.g. "I upgraded QtCreator to 2.5.0 and now my program doesn't work"). People do not bother learning why things work the way they work if they find a "solution" to their "problem" after searching the internet for two minutes. They immediately fall into another pitfall and start browsing forums for solutions on that.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Empty style sheet changes QLineEdit
    By mpi in forum Qt Programming
    Replies: 3
    Last Post: 15th June 2009, 20:13
  2. Replies: 5
    Last Post: 8th June 2009, 22:16
  3. QLineEdit question
    By MarkoSan in forum Qt Programming
    Replies: 4
    Last Post: 29th June 2008, 09:58
  4. question about the mac style
    By billconan in forum Newbie
    Replies: 3
    Last Post: 31st August 2006, 11:40
  5. pen style question
    By impeteperry in forum Qt Programming
    Replies: 4
    Last Post: 21st July 2006, 20:49

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.