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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T20:51:59+00:00 2026-05-16T20:51:59+00:00

I created a little widget on my own, including a QProgressBar and a QLabel

  • 0

I created a little widget on my own, including a QProgressBar and a QLabel in a QVBoxLayout. It has also a function which returns the text of the label (self-created).
Now in my MainWindow I have two other QHBoxLayouts and I want to drag and drop my widget from one to another. It also works when I click on the little free space between the QLabel and the QProgressBar. But when I click on one of them directly, the application crashed and burned painfully.
I also know where it fails. My mousePressEvent looks like this:

void DragDrop::mousePressEvent(QMouseEvent *event) {

// !!!!---- make sure ONLY MyWidgets are here, else: CRASH ----!!!!
    MyWidget *child = static_cast<MyWidget*>(childAt(event->pos()));
    if (!child)
    return;

qDebug() << child->returnLabelText();
...

}

So when I click on the ProgressBar, it will cast the ProgressBar, not my own widget. And because the QProgressBar doesn’t have a function like returnLabelText() (but my widget does) it fails.
What is a better method to get my widget?

  • 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-16T20:51:59+00:00Added an answer on May 16, 2026 at 8:51 pm

    QWidget::childAt(int,int) returns the child widget, not the parent widget. In your case, it returns the QProgressBar. You then try to cast into a MyWidget, which it is not. What you are looking for is for the parent of the QProgressBar (or QLabel).

    static_cast does not verify the type of the object you are trying to cast, and will always yield a non-null pointer even if the cast is invalid. What you are looking for here is dynamic_cast, which will return NULL if the object is not of the type you are looking for. Since you are looking for the parent (or an ancestor) of the widget being clicked, you could use a loop to iterate through the clicked widget’s ancestry to find the instance of MyWidget you are looking for.

    void DragDrop::mousePressEvent(QMouseEvent *event) {
      QWidget *widget = childAt(event->pos());
      do {
        MyWidget *myWidget = dynamic_cast<MyWidget*>(widget);
        widget = widget->parentWidget();
      } while (myWidget == NULL && widget != NULL)
      if (myWidget == NULL)
        return;
    
      qDebug() << myWidget->returnLabelText();
      // ...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The organization I work for has created a small flash widget that we're encouraging
I created a little proxy node script, which looksup the request.url and either passes
So I'm a newbie to TDD, and I successfully created a nice little sample
I created a program using dev-cpp and wxwidgets which solves a puzzle. The user
I'm trying to create a widget (which is basically an iframe) that would have
I'm trying to display a little square of solid color. I've created a QGraphicsView
I have a Python script which uses Tkinter for the GUI. My little script
I have some javascript which will create some sort of widget on a page.
I created a little app a while ago. I created admin.py and used admin.site.register(MenuEntry)
I created a little texture drawing class from observing the LabelMaker example in the

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.