Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Programmatically send commands (i.e. scroll) to the topmost active widget
Forum Update on Monday, May 27th 2025

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

Scheduled Pinned Locked Moved Unsolved General and Desktop
focuswidgetscroll
4 Posts 2 Posters 1.7k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • McLionM Offline
    McLionM Offline
    McLion
    wrote on last edited by
    #1

    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

    J 1 Reply Last reply
    0
    • McLionM McLion

      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

      J Offline
      J Offline
      Jan-Willem
      wrote on last edited by
      #2

      @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.

      1 Reply Last reply
      1
      • McLionM Offline
        McLionM Offline
        McLion
        wrote on last edited by
        #3

        @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?

        1 Reply Last reply
        0
        • J Offline
          J Offline
          Jan-Willem
          wrote on last edited by Jan-Willem
          #4

          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.

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved