Programmatically send commands (i.e. scroll) to the topmost active widget



  • Hi

    I have multiple QWebView and QPlainTextEdit that are shown and hidden programmatically by user interaction. All the objects are created when the app starts and have a fixed stacking, meaning front (raised) and back position are fixed.

    I now need to send PageScroll's to the currently shown and topmost widget.

    I know how to get the focused widget. But that does not work since the focus does not automatically go back to next widget currently shown once a widget that had a higher level is hidden.

    If it were only QWebView and the topmost shown would always have the focus - which it does not have mandatory, this would - it actually does - work:

    QWebView *webGUI = qobject_cast<QWebView *>(focusWidget());
    QPoint ScrollPos = webGUI->page()->mainFrame()->scrollPosition();
    qDebug() << webGUI->page()->mainFrame()->scrollPosition();
    ScrollPos.ry() -= SCROLL_PIXEL_PER_PAGE;
    webGUI->page()->mainFrame()->setScrollPosition(ScrollPos);
    qDebug() << webGUI->page()->mainFrame()->scrollPosition();
    

    What I'm missing and don't know how to do:

    • How to get, or direct it to the topmost shown widget
    • What if the topmost is a QTextEdit?
    • Can I somehow get the information what type of widget is the topmost (or at least focused)?

    I hope I did make myself clear enough ;-)
    Thanks for any hints.
    McL



  • @McLion

    I know how to get the focused widget. But that does not work since the focus does not automatically go back to next widget currently shown once a widget that had a higher level is hidden.

    Perhaps you could implement such behaviour by having some variable which contains a pointer or an index to the previous widget. Or if the order in which the widgets are created is fixed, iterate through the collection of widgets en check if the next in line is not hidden.

    As for widgettype your can do:

    QPlainTextEdit *textEdit = qobject_cast<QPlainTextEdit*>(widget);
    if (textEdit) 
        //do something
    

    And the same of course for a QTextEdit or a QWebView.



  • @Jan-Willem said:

    Perhaps you could implement such behaviour by having some variable which contains a pointer or an index to the previous widget. Or if the order in which the widgets are created is fixed, iterate through the collection of widgets en check if the next in line is not hidden.

    Thanks.
    I was thinking about such a thing. Yes, I do have an index which tells me which of the elements are currently active and I can tell which one is the topmost. I'm maintaining a QMap with pointers to all objects for various other reasons and functions.
    I was thinking about a 'more general way', however, if that's the way to go, it'll probably work.

    For the widgettype:

    Does

    QPlainTextEdit *textEdit = qobject_cast<QPlainTextEdit*>(widget); 
    

    return 0 if it's not a textEdit?
    Same with?

    QWebView *webGUI = qobject_cast<QWebView *>(widget);
    

    So, I'd have to iterate to find the type. There is no such thing as an return value that indicates the type of a widget?



  • Yes, it returns NULL if the cast failed.

    As for the type, this might also work:

    QObject *obj = widget;
    objType = obj->metaObject()->className();  
    if (objType == "QPlainTextEdit")  {
            QPlainTextEdit *textEdit = qobject_cast<QPlainTextEdit*>(obj);
            //do something
    }
    

    Never tried it though. If it works, you won't have to make multiple casts.
    But you will still have to check which className it has.


Log in to reply
 

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