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 7398323
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T03:51:22+00:00 2026-05-29T03:51:22+00:00

Trying to create a widget in the baseclass and the only thing that will

  • 0

Trying to create a widget in the baseclass and the only thing that will change in the widget depending on what derived class we call is the title.

class BaseView : public QWidget {
    Q_OBJECT
public:
     explicit BaseView(QWidget *parent = 0);
protected:
     QString title; 
     virtual QString getTitle() const {
         return title;
     }

BaseView.cpp :

BaseView::BaseView(QWidget *parent) : QWidget(parent) {

     title = "test"
}

void BaseView::createTopWidget() {
    QGroupBox *topWidget = new QGroupBox(getTitle());
    ....
}

The in the derived class header:

class DerivedView : public BaseView {
    Q_OBJECT
public:
     explicit DerivedView(QWidget *parent = 0);
protected:
     QString getTitle() const {
         return title;
     }

And in the derived constructor I set title to “correct”.

When I run the program by creating a DerivedView, the title is still “test”. How can I do this so I will call and get at derived class value from the base class?

  • 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-29T03:51:22+00:00Added an answer on May 29, 2026 at 3:51 am

    Unless you have a very firm grasp on C++, you must not call virtual functions in a constructor. The problem is that during subobject creation, the type of the object is the base subobject, not the derived object, so the virtual function call is dispatched to the “wrong” function.

    Baby example:

    struct Base
    {
        virtual int foo() { return 8; }
    
        Base() { set_something(foo()); }  // who're you gonna call?
    };
    struct Derived
    {
        virtual int foo() { return -12; }
    };
    
    Derived d;
    

    Now in the base constructor for d.Base(), the function that gets called is d.Base::foo(), not d.Derived::foo().

    Moreover, by the time the base class constructor runs, the member variable title has only been set to "test", and it has not yet been overwritten by the assignment in the derived constructor, which runs only after the base constructor completes.

    You can solve your problem by passing the title in the constructor explicitly:

    class BaseView : public QWidget
    {
    public:
        BaseView(QWidget * parent = 0, QString const & t = "test")
        : QWidget(parent), title(t)
        {
            something.set_title(title);
        }
    
        // ...
    };
    
    class DerivedView
    {
    public:
        DerivedView(QWidget * parent = 0)
        : BaseView(parent, "correct")
        { }
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to create a widget for Sharepoint that will show the average
I'm trying to create a widget that will create a military time string from
I'm trying to create a widget within the module and then load that widget
I'm trying to create an icon/widget (1 cell x 1 cell) that can be
I'm trying to create a form that has an expandable widget. The problem is
Trying to create a widget with custom fonts, but read that widget doesn't support
I'm trying to create a widget (which is basically an iframe) that would have
I'm trying to create my first AppWidget using the AlarmManager class so that I
I'm trying to create a widget that contains a single ImageView which, when clicked,
I'm trying to create a launcher widget with a TextView that is editable. Everything

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.