Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6990793
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T19:24:14+00:00 2026-05-27T19:24:14+00:00

My coding practice using Qt can best be described as follows: If the Widget

  • 0

My coding practice using Qt can best be described as follows:

  1. If the Widget is going to be actively used (e.g. A QLineEdit which provides text), I declare it in the header file and then initialise it in MainWindow.cpp.
    e.g. TextEditor.h:

class TextEditor
{
//other code
private:
QLineEdit edtFind;
};

2.. If a widget is not going to be used (e.g. QLabel, QWidget), or it’s part of a signal slot system (e.g. QPushButton), I declare and inialise it inside constructor using new.

-e.g.

TextEditor::TextEditor()
{
   //other code
   QWidget* searchPanel = new QWidget();
   edtFind = new QLineEdit("Enter Search Term");
   QPushButton* findButton = new QPushButton("Find");
   connect(findButton,SIGNAL(pressed()),this,SLOT(find()));

   ui->statusbar->addPermanentWidget(searchPanel);
}

My question is, am I using an efficient approach in point 2? Would it be better to not allocate memory from the heap?

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T19:24:15+00:00Added an answer on May 27, 2026 at 7:24 pm

    Your approach is not efficient. You should use heap allocated objects when you actually need them:

    • objects that have a longer lifetime
    • using a forward declaration in order to avoid including a header file
    • holding a reference to an object created elsewhere

    Your approach is more complicated without any visible benefit. Heap is known to be slow, and allocating a large number of small objects is known to fragment it (this might not make a difference in your app but it’s still a bad practice).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Give me some of your thoughts on which is a better coding practice/makes more
In coding a traditional MVC application, what is the best practice for coding server-side
I'm using Damian Conway's inside-out objects as described is his wonderful book Perl Best
I know it's 'technically' not the best practice to be hard coding strings; but
I've done some searching on this but can't find any sort of best practice
So I've been reading up on coding best practices, and the importance of using
I am wondering if there is a best coding practice in regard to dealing
I know it is better coding practice to avoid inline javascript like: <img id=the_image
I participate in a TDD Coding Dojo, where we try to practice pure TDD
I'm looking for some best practice advice on enumerations and retrieving an associated string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.