How identify what Widget is activated?



  • I have for beginning one widget like this :
    0_1531391786498_QMainWindow.png
    The window in yellow is a widget. When I click on a button, one signal is send and I go in a function for split this widget and obtain this :
    0_1531392014979_Capture.PNG
    We can see that another widget has been added at the window (and the arrow is the split).
    This function to obtain that is :

        QWidget *first = MainWindow::first;
        QWidget *second = createContentPanel();
        QSplitter *split = new QSplitter;
    
        split->addWidget(first);
        split->addWidget(second);
        delete layout;
        layout = new QVBoxLayout;
        layout->addWidget(split);
        widget->setLayout(layout);
    

    What I want, it's replace "first = MainWindow::first;" by another way to search the QWidget first.
    This code has to work for more widget and "first" have to be equal at the QWidget selected (clicked or with the mouse on the widget.)

    Someone know how we can identify what widget is activated?


  • Lifetime Qt Champion

    Hi,

    Do you mean you want to keep a pointer on the widget that has the focus ?



  • @SGaist
    I would like to have a function that return a QWidget where is my mouse.
    Is it possible ?



  • @bozo6919

    I would like to have a function that return a QWidget where is my mouse.

    If nobody offers you a better way, you could walk all your widgets (http://doc.qt.io/qt-5/qapplication.html#allWidgets) and use http://doc.qt.io/qt-5/qwidget.html#underMouse to determine which widget the mouse is over.

    Previously clicked is another matter.

    If you just want the widget with current focus that's much easier.

    If that's really what you are looking for. Depending on what you are going to use this information to achieve, there may be better ways to approach it.


  • Lifetime Qt Champion

    You could have an event filter that would look for the focus related events.



  • @JonB Thanks for your answer.
    So, I stock now one QWidgets in a class named ContentPanel and I stock all theses ContentPanel in a QVector.
    At all ContentPanel, I apply this function :

    bool ContentPanel::event(QEvent *event)
    {
        if (event->type() == QEvent::Enter)
            focus = true;
        else if (event->type() == QEvent::Leave)
            focus = false;
        return (QWidget::event(event));
    }
    

    I have just to look for the variable focus on my class ContentPanel who refer to a QWidget.

    Thanks :)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.