Important: Please read the Qt Code of Conduct -

Putting other widgets inside a QPushButton

  • I'm trying to make a widget that represents a layer in an image, then when the user clicks it, the widget will be "selected." But I also want there to me some other widgets inside the widget to edit things about the image. I'm wondering if it's possible to put other widgets inside a QPushButton?

    I already tried having a QWidget sublass and overloading QWidget::mousePressEvent() and already had some issues propigating the events to the inside widgets.

  • Moderators

    [quote author="cuddlykittens11" date="1422550102"]I already tried having a QWidget sublass and overloading QWidget::mousePressEvent() and already had some issues propigating the events to the inside widgets.[/quote]

    propagating works in the other direction around. Meaning when your parent/container widget receives the mouse-event it wasn't accepted from your child widget (if the event happend at the child widgets pos).

    So don't use a QPushButton and place widgets inside it. What widgets did you place inside the widget which didn't work?

  • QPushButtons, QSpinBoxes, and QLineEdits. I also tried using a QGraphicsProxyWidget sublcass, and then placing the widgets inside of a QGraphicsScene. It didn't work out too well.

  • Moderators

    you have to be more specific than that.
    What didn't work? What did you try exactly?

  • Okay, maybe a picture will help illustrate. I'm trying to make a widget to select and modify layers in an image. Here's a bad little mockup:


    I was thinking that I could represent each layer as a widget (display info like resolution for that layer), but also have some buttons inside the widget to modify the cel (e.g. show/hide layer with a QCheckBox or delete layer with a QPushButton). But I also want the user to select the current layer by clicking on the area that isn't another widget.

    I tried acomplishing this via a subclass of QGraphicsProxyWidget, but I have many issues when it game to event propigation.

  • What you have there is a list of items. Wouldn't it make sense to use something that is meant for displaying just that?

    Do you really want to have this inside a graphics view (you mentioned QGraphicsProxyWidget), or could it just be a widget? Or perhaps a Quick scene? What version of Qt are you on?

    Anyway: just because you need to handle a mouse click, it doesn't mean that you have to subclass QPushButton or put other things on top of it. Don't do that.

  • I'm running Qt5. Would it be possible to use a "QListwidget": or one of its parent classes to acchive this?

  • Moderators

    yes can be. But you should only use it if you just want to display a few (not to many) widgets inside it.

  • how many would be "not too many"? Would 50-100 be too much.

    As a possible solution to this problem, I was thinking about subclassing QGraphicsObject and making clickable areas (e.g. the Delte button up there) that will perform actions.

  • Putting widgets inside item views is not ideal. It doesn't scale well, and it is a bit of a hassle. This works much better with Quick, where a delegate may contain anything you want, including Quick controls. Perhaps you could use a Quick scene to display your list on a QQuickWidget (available from Qt 5.3).

Log in to reply