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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T17:48:30+00:00 2026-06-08T17:48:30+00:00

I have the following snippet: class A : public QWidget { Q_OBJECT public: A(QWidget

  • 0

I have the following snippet:

    class A : public QWidget
    {
       Q_OBJECT
   public:
     A(QWidget *parent = 0);

     void
     setGeometry(int x, int y, int w, int h);

      protected:
       virtual void
       resizeEvent(QResizeEvent *event);

    }

    class B : public A
    {
       Q_OBJECT
   public:
     B(A*parent = 0);   

     void
    setGeometry(int x, int y, int w, int h);

   protected:
     virtual void
     resizeEvent(QResizeEvent *event);
    }

void
A::setGeometry(int x, int y, int w, int h) 
{
    QWidget::setGeometry(x, y, w, h);
}

void
A::resizeEvent( QResizeEvent * event) 
{
    QWidget::resizeEvent(event);
    // common A and B stuff
}

void
B::setGeometry(int x, int y, int w, int h) 
{
    A::setGeometry(x, y, w, h);
}

void
B::resizeEvent( QResizeEvent * event) 
{
    A::resizeEvent(event);

}

Calling setGeometry on an instance of A will fire resizeEvent() . Invoke setGeometry on an instance of B will not fire resizeEvent(). Is there anything wrong with this?

EDIT:
I could do the same calculation I need inside setGeometry successfully. Now, mine is only curiosity.

  • 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-08T17:48:32+00:00Added an answer on June 8, 2026 at 5:48 pm

    There are a few problems with the snippet above, so I’ve altered it in a few places… the code below is the minimum necessary to produce the behaviour you want.

    Header:

    class TestA : public QWidget
    {
      Q_OBJECT
    
      public:
        explicit TestA(QWidget *Parent = 0) : QWidget(Parent) {}
        ~TestA() {}
    
      protected:
        virtual void resizeEvent(QResizeEvent *);
    };
    
    class TestB : public TestA
    {
      Q_OBJECT
    
      public:
        explicit TestB(QWidget *Parent = 0) : TestA(Parent) {}
        ~TestB() {}
    
      protected:
        virtual void resizeEvent(QResizeEvent *);
    };
    

    Implementation:

    void TestA::resizeEvent(QResizeEvent *)
    {
      qDebug() << "TestA Resize";
    }
    
    void TestB::resizeEvent(QResizeEvent *)
    {
      qDebug() << "TestB Resize";
    }
    
    MainWindow::MainWindow(QWidget *parent) :
      QMainWindow(parent),
      ui(new Ui::MainWindow)
    {
      ui->setupUi(this);
    
      TestA* A = new TestA(this);
      TestB* B = new TestB(this);
    
      A->setGeometry(0,0,100,100);
      B->setGeometry(200,200,100,100);
    }
    

    Changes:

    • Addition of Q_OBJECT macros to class definition:

      Tells the compiler to add Qt meta-object code for the class (not strictly necessary to ensure that resizeEvent()s are called, but should be included for objects which inherit QObject as a matter of course).

    • Addition of constructors which allow the passing in of a parent object and invoke the base class constructor with this parent object AND passing a parent object into the constructors of the two objects when they are created:

      From the docs:

      When changing the geometry, the widget, if visible, receives a move event (moveEvent()) and/or a resize event (resizeEvent()) immediately. If the widget is not currently visible, it is guaranteed to receive appropriate events before it is shown.

      If your widgets don’t have parents setting the geometry is half meaningless, as the x and y parts refer to its position relative to a parent. On top of that, since the widgets have no parents they can’t be visible as part of the application so Qt doesn’t bother to call the appropriate resizeEvent().

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

Sidebar

Related Questions

I have following code snippet: class ABC{ public: int a; void print(){cout<<hello<<endl;} }; int
I have the following code snippet: class Cert { public static void main(String[] args)
I have the following code snippet: public class A { public static void main(String[]
I have the following code snippet: public abstract class Foo { protected Foo() }
I have the following classes class Parent { virtual void doStuff() = 0; };
I have the following code snippet Class Parent { public override String ToString() {
I have the following (snippet) of a kernel. __global__ void plain(int* geneVec, float* probs,
Consider the following snippet: import java.util.*; public class EqualsOverload { public static void main(String[]
I have the following code snippet. public class ImageStoreActivity extends ListActivity { private DBHelper
Consider the following code snippet: class Test { public int Length{ get; set; }

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.