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

  • SEARCH
  • Home
  • 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 8085351
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T18:05:16+00:00 2026-06-05T18:05:16+00:00

If I declare a function virtual in the base non-QObject class and then overlaod

  • 0

If I declare a function virtual in the base non-QObject class and then overlaod it as a slot in the derived class that has Q_OBJECT macro and that has QObject as one of the base classes is it supposed to work ok?

Is it guaranteed that virtual calls will work? What should happen if you connenct to the slot of the derived class?

class Base
{
public:
    virtual void f();
};

class Derived: public QObject, public Base
{
    Q_OBJECT
public slots:
    virtual void f();
};
  • 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-06-05T18:05:19+00:00Added an answer on June 5, 2026 at 6:05 pm

    Yes:

    Since slots are normal member functions, they follow the normal C++
    rules when called directly. <…> You can also define slots to be virtual, which we have found quite
    useful in practice.

    http://qt-project.org/doc/qt-4.8/signalsandslots.html#slots

    In your example Derived::f is a normal virtual function. If it’s called directly, it works as expected, just as the documentation says. When invoked by signal, it’s called by qt_static_metacall, which is generated in moc_Derived.cpp as following:

    void Derived::qt_static_metacall(QObject *_o, QMetaObject::Call _c, 
                                     int _id,     void **_a)
    {
        if (_c == QMetaObject::InvokeMetaMethod) {
            Q_ASSERT(staticMetaObject.cast(_o));
            Derived *_t = static_cast<Derived *>(_o);
            switch (_id) {
            case 0: _t->f(); break;
            default: ;
            }
        }
        Q_UNUSED(_a);
    }
    

    So, it ends with normal function call _t->f().

    Note that there is no way to invoke Base::f by a signal. This function can be executed only if present object is actually Base instance and not Derived instance. And since Base is not QObject-based, you can’t pass its instance to connect function.

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

Sidebar

Related Questions

I have a function X() declared as PURE VIRTUAL in base class: class Base
I was trying to declare a callback function in class and then somewhere i
I have a function declared in my base class and specified as virtual, I
Consider: class base { base(); virtual void func(); } class derived : public base
Bar and Box are derived classes of Foo and Foo has a virtual function
Do you have to declare methods replacing a pure virtual function in a base
I'd like to have a base class that has a constant field (like an
Here I have declared another virtual function in Derived class. #include <iostream> using namespace
I want to forward declare a static member function of a class in another
Given the base class A and the derived class B: class A { public:

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.