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. QWidget::windowRole() use as storage for small textual data
Qt 6.11 is out! See what's new in the release blog

QWidget::windowRole() use as storage for small textual data

Scheduled Pinned Locked Moved Solved General and Desktop
33 Posts 4 Posters 9.5k Views 2 Watching
  • 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.
  • Please_Help_me_DP Please_Help_me_D

    @SGaist I'm trying to use bool QObject::setProperty(const char *name, const QVariant &value)
    In my MainWindow I call MyClass where I create some objects and set for each of them property:

    MyClass::BlaBla(){
        ads::CDockWidget* dockWidget = new ads::CDockWidget("2D Window");
        dockWidget->setWidget(qvtkOpenGLStereoWidget);
        dockWidget->setProperty("IsVisualization", true);
        dockManager->addDockWidget(ads::RightDockWidgetArea, dockWidget);
    }
    

    If I write QVariant variant = dockWidget->property("IsVisualization"); then the variant contains correct data everything if ok at this step.
    But if I do the same in the MainWindow inside onFocusChanged method I get invalid QVariant:

    void MainWindow::onFocusChanged(QWidget* old, QWidget* now){
        if (old != nullptr){
            QVariant variantOLD = old->property("IsVisualization");
        }
        if (now != nullptr){
            QVariant variantNOW = now->property("IsVisualization");
        }
    }
    

    What is wrong?

    P.S. dockManager and dockWidget are from advanced_docking_system library

    JonBJ Offline
    JonBJ Offline
    JonB
    wrote on last edited by JonB
    #5

    @Please_Help_me_D
    Since being inside onFocusChanged() makes no difference to whether you can read a property or not, I can only conclude that neither old nor now are the dockWidget on which you previously set the property, or you have reset it since then.

    Did you even try object_cast<>ing old/now to verify they are ads::CDockWidget*?

    Please_Help_me_DP 1 Reply Last reply
    2
    • JonBJ JonB

      @Please_Help_me_D
      Since being inside onFocusChanged() makes no difference to whether you can read a property or not, I can only conclude that neither old nor now are the dockWidget on which you previously set the property, or you have reset it since then.

      Did you even try object_cast<>ing old/now to verify they are ads::CDockWidget*?

      Please_Help_me_DP Offline
      Please_Help_me_DP Offline
      Please_Help_me_D
      wrote on last edited by
      #6

      @JonB after you wrote I tried it... Yes you are right, they are not a ads::CDockWidget*
      I guess I should be able to get ads::CDockWidget* from old/now via old->children?

      JonBJ 1 Reply Last reply
      0
      • Please_Help_me_DP Please_Help_me_D

        @JonB after you wrote I tried it... Yes you are right, they are not a ads::CDockWidget*
        I guess I should be able to get ads::CDockWidget* from old/now via old->children?

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by JonB
        #7

        @Please_Help_me_D
        I was doing this just today. The focus can be hitting some sub widget on a container. In my case widgets on a QMdiSubWindow. To find that I go up via parentWidget() till I find one or hit the topmost. If you are same thing you will want to go up parents from old/now, not chidren?

        Please_Help_me_DP 1 Reply Last reply
        1
        • JonBJ JonB

          @Please_Help_me_D
          I was doing this just today. The focus can be hitting some sub widget on a container. In my case widgets on a QMdiSubWindow. To find that I go up via parentWidget() till I find one or hit the topmost. If you are same thing you will want to go up parents from old/now, not chidren?

          Please_Help_me_DP Offline
          Please_Help_me_DP Offline
          Please_Help_me_D
          wrote on last edited by
          #8

          @JonB nice to hear that
          As I understood I should go up from old/now until I meet ads::CDockWidget* dockWidget != nullptr condition, correct? Like:

          {
          ads::CDockWidget* dockWidget = qobject_cast<ads::CDockWidget*>(old->parent());
          if (CDockWidget != nullptr){}
          }
          

          but in some while or for loop...

          JonBJ 1 Reply Last reply
          0
          • Please_Help_me_DP Please_Help_me_D

            @JonB nice to hear that
            As I understood I should go up from old/now until I meet ads::CDockWidget* dockWidget != nullptr condition, correct? Like:

            {
            ads::CDockWidget* dockWidget = qobject_cast<ads::CDockWidget*>(old->parent());
            if (CDockWidget != nullptr){}
            }
            

            but in some while or for loop...

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by JonB
            #9

            @Please_Help_me_D

            ads::CDockWidget* dockWidget = nullptr;
            while (old != nullptr && (dockWidget = qobject_cast<ads::CDockWidget*>(old)) == nullptr)
                old = old->parentWidget();
            qDebug() << dockWidget;
            
            Please_Help_me_DP 1 Reply Last reply
            1
            • JonBJ JonB

              @Please_Help_me_D

              ads::CDockWidget* dockWidget = nullptr;
              while (old != nullptr && (dockWidget = qobject_cast<ads::CDockWidget*>(old)) == nullptr)
                  old = old->parentWidget();
              qDebug() << dockWidget;
              
              Please_Help_me_DP Offline
              Please_Help_me_DP Offline
              Please_Help_me_D
              wrote on last edited by
              #10

              @JonB thank you
              But the loop ended and dockWidget is always nullptr. It takes about 7 cycles and there is no ads::CDockWidget*

              JonBJ 1 Reply Last reply
              0
              • Please_Help_me_DP Please_Help_me_D

                @JonB thank you
                But the loop ended and dockWidget is always nullptr. It takes about 7 cycles and there is no ads::CDockWidget*

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by
                #11

                @Please_Help_me_D
                So in that case the old or now (you have written this loop on both, haven't you?!) in the focus change is not a descendent of your ads::CDockWidget* dockWidget. I never said it was, you did! Or you check children if whatever it is works that way.

                Please_Help_me_DP 2 Replies Last reply
                0
                • JonBJ JonB

                  @Please_Help_me_D
                  So in that case the old or now (you have written this loop on both, haven't you?!) in the focus change is not a descendent of your ads::CDockWidget* dockWidget. I never said it was, you did! Or you check children if whatever it is works that way.

                  Please_Help_me_DP Offline
                  Please_Help_me_DP Offline
                  Please_Help_me_D
                  wrote on last edited by
                  #12

                  @JonB yes I have written both old and now
                  Yes I'm trying to find this widget via old->children();. I need few minuts

                  1 Reply Last reply
                  0
                  • JonBJ JonB

                    @Please_Help_me_D
                    So in that case the old or now (you have written this loop on both, haven't you?!) in the focus change is not a descendent of your ads::CDockWidget* dockWidget. I never said it was, you did! Or you check children if whatever it is works that way.

                    Please_Help_me_DP Offline
                    Please_Help_me_DP Offline
                    Please_Help_me_D
                    wrote on last edited by
                    #13

                    @JonB this code can't find ads::CDockWidget* neither:

                    void MainWindow::onFocusChanged(QWidget* old, QWidget* now){
                        if (old != nullptr){
                            ads::CDockWidget* dockWidget = nullptr;
                            QObjectList objList = old->children();
                            int i = 0;
                            while (i < objList.count() && dockWidget == nullptr){
                                dockWidget = qobject_cast<ads::CDockWidget*>(objList.at(i));
                                i++;
                            }
                        }
                    
                        if (now != nullptr){
                            ads::CDockWidget* dockWidget = nullptr;
                            QObjectList objList = now->children();
                            int i = 0;
                            while (i < objList.count() && dockWidget == nullptr){
                                dockWidget = qobject_cast<ads::CDockWidget*>(objList.at(i));
                                i++;
                            }
                        }
                    }
                    
                    JonBJ 1 Reply Last reply
                    0
                    • Please_Help_me_DP Please_Help_me_D

                      @JonB this code can't find ads::CDockWidget* neither:

                      void MainWindow::onFocusChanged(QWidget* old, QWidget* now){
                          if (old != nullptr){
                              ads::CDockWidget* dockWidget = nullptr;
                              QObjectList objList = old->children();
                              int i = 0;
                              while (i < objList.count() && dockWidget == nullptr){
                                  dockWidget = qobject_cast<ads::CDockWidget*>(objList.at(i));
                                  i++;
                              }
                          }
                      
                          if (now != nullptr){
                              ads::CDockWidget* dockWidget = nullptr;
                              QObjectList objList = now->children();
                              int i = 0;
                              while (i < objList.count() && dockWidget == nullptr){
                                  dockWidget = qobject_cast<ads::CDockWidget*>(objList.at(i));
                                  i++;
                              }
                          }
                      }
                      
                      JonBJ Offline
                      JonBJ Offline
                      JonB
                      wrote on last edited by JonB
                      #14

                      @Please_Help_me_D
                      So read up on what a ads::CDockWidget is/how it works. Your assumption in that QWidget::focusChanged(old, now) must lead to widgets either ancestors or descendants of this dock widget. I have no idea whether that is the case. Also your code may need to recurse down through children, no? At least start by finding out what old & now actually are in your focus change.

                      Please_Help_me_DP 1 Reply Last reply
                      0
                      • JonBJ JonB

                        @Please_Help_me_D
                        So read up on what a ads::CDockWidget is/how it works. Your assumption in that QWidget::focusChanged(old, now) must lead to widgets either ancestors or descendants of this dock widget. I have no idea whether that is the case. Also your code may need to recurse down through children, no? At least start by finding out what old & now actually are in your focus change.

                        Please_Help_me_DP Offline
                        Please_Help_me_DP Offline
                        Please_Help_me_D
                        wrote on last edited by
                        #15

                        @JonB yes possibly I should have done the recursion down to the other children but it alredy becomes too complicated solution for such kind of a task.
                        Now I'm trying to set property not on CDockWidget but to some parent. I'm trying...

                        1 Reply Last reply
                        0
                        • mrjjM Offline
                          mrjjM Offline
                          mrjj
                          Lifetime Qt Champion
                          wrote on last edited by
                          #16

                          Hi
                          It wont be the DockWidget that has the focus. it will be its widget/children.

                          I would call dumpObjectTree()
                          on old and now to see how they are made up.

                          Please_Help_me_DP 1 Reply Last reply
                          1
                          • mrjjM mrjj

                            Hi
                            It wont be the DockWidget that has the focus. it will be its widget/children.

                            I would call dumpObjectTree()
                            on old and now to see how they are made up.

                            Please_Help_me_DP Offline
                            Please_Help_me_DP Offline
                            Please_Help_me_D
                            wrote on last edited by Please_Help_me_D
                            #17

                            @mrjj thank you for advice. Here is the output:

                            setFocus(Qt::OtherFocusReason);
                            setFocus(Qt::OtherFocusReason);
                            ads::CDockWidget::2D Window 
                                QBoxLayout:: 
                                QAction:: 
                                QScrollArea::dockWidgetScrollArea 
                                    QWidget::qt_scrollarea_viewport 
                                        QVTKOpenGLStereoWidget:: 
                                            QVBoxLayout:: 
                                            QWindowContainer:: 
                                    QWidget::qt_scrollarea_hcontainer 
                                        QScrollBar:: 
                                        QBoxLayout:: 
                                    QWidget::qt_scrollarea_vcontainer 
                                        QScrollBar:: 
                                        QBoxLayout:: 
                            setFocus(Qt::OtherFocusReason);
                            setFocus(Qt::OtherFocusReason);
                            setFocus(Qt::OtherFocusReason);
                            

                            So the focus should be on one of those?
                            By the way this tree is from ads::CDockWidget
                            Just a moment and I find tree for old
                            /
                            and here is tree for now object:

                            ads::CDockWidgetTab:: 
                                QBoxLayout:: 
                                ads::CElidingLabel::dockWidgetTabLabel 
                                QPushButton::tabCloseButton 
                            
                            mrjjM 1 Reply Last reply
                            0
                            • Please_Help_me_DP Please_Help_me_D

                              @mrjj thank you for advice. Here is the output:

                              setFocus(Qt::OtherFocusReason);
                              setFocus(Qt::OtherFocusReason);
                              ads::CDockWidget::2D Window 
                                  QBoxLayout:: 
                                  QAction:: 
                                  QScrollArea::dockWidgetScrollArea 
                                      QWidget::qt_scrollarea_viewport 
                                          QVTKOpenGLStereoWidget:: 
                                              QVBoxLayout:: 
                                              QWindowContainer:: 
                                      QWidget::qt_scrollarea_hcontainer 
                                          QScrollBar:: 
                                          QBoxLayout:: 
                                      QWidget::qt_scrollarea_vcontainer 
                                          QScrollBar:: 
                                          QBoxLayout:: 
                              setFocus(Qt::OtherFocusReason);
                              setFocus(Qt::OtherFocusReason);
                              setFocus(Qt::OtherFocusReason);
                              

                              So the focus should be on one of those?
                              By the way this tree is from ads::CDockWidget
                              Just a moment and I find tree for old
                              /
                              and here is tree for now object:

                              ads::CDockWidgetTab:: 
                                  QBoxLayout:: 
                                  ads::CElidingLabel::dockWidgetTabLabel 
                                  QPushButton::tabCloseButton 
                              
                              mrjjM Offline
                              mrjjM Offline
                              mrjj
                              Lifetime Qt Champion
                              wrote on last edited by
                              #18

                              hi
                              yes it should but we don't have to guess.
                              you can do
                              QWidget * fw = qApp->focusWidget();
                              and check out in the debugger (or qDebug its object name) to see what it is.

                              It should be possible to get hold of the Dock again to get your data back.

                              Please_Help_me_DP 2 Replies Last reply
                              1
                              • mrjjM mrjj

                                hi
                                yes it should but we don't have to guess.
                                you can do
                                QWidget * fw = qApp->focusWidget();
                                and check out in the debugger (or qDebug its object name) to see what it is.

                                It should be possible to get hold of the Dock again to get your data back.

                                Please_Help_me_DP Offline
                                Please_Help_me_DP Offline
                                Please_Help_me_D
                                wrote on last edited by
                                #19

                                @mrjj QWidget * fw and now widget is the same. See the picture please
                                1.png

                                1 Reply Last reply
                                0
                                • mrjjM mrjj

                                  hi
                                  yes it should but we don't have to guess.
                                  you can do
                                  QWidget * fw = qApp->focusWidget();
                                  and check out in the debugger (or qDebug its object name) to see what it is.

                                  It should be possible to get hold of the Dock again to get your data back.

                                  Please_Help_me_DP Offline
                                  Please_Help_me_DP Offline
                                  Please_Help_me_D
                                  wrote on last edited by Please_Help_me_D
                                  #20

                                  @mrjj now I'm trying to set property to the tabWidgets to wich CDockWidget belongs to:

                                  QVTKOpenGLStereoWidget* MainWindowObjectCreator::createVTK2DWindow(){
                                      QVTKOpenGLStereoWidget* qvtkOpenGLStereoWidget = new QVTKOpenGLStereoWidget;
                                      ads::CDockWidget* dockWidget = new ads::CDockWidget("2D Window");
                                      dockWidget->setWidget(qvtkOpenGLStereoWidget);
                                      dockManager->addDockWidget(ads::RightDockWidgetArea, dockWidget);
                                      dockWidget->tabWidget()->setProperty("IsVisualization", 4443);
                                      //dockWidget->dumpObjectTree();
                                      return qvtkOpenGLStereoWidget;
                                  }
                                  

                                  but each time I manually change the focus the MainWindow::onFocusChanged(QWidget* old, QWidget* now) works twice:
                                  first time now is the old window
                                  second time is the new window
                                  and when the second time this method works then I get the correct value of property:

                                  void MainWindow::onFocusChanged(QWidget* old, QWidget* now){
                                      QWidget * fw = qApp->focusWidget();
                                      fw->dumpObjectTree();
                                      if (now != nullptr){
                                          now->dumpObjectTree();
                                          QVariant varia = now->property("IsVisualization"); 
                                      }
                                  }
                                  

                                  By the way this works only if the focused window is tabbed. Otherwise the focused widget is not a tabWidget(). So idea to set property to the tabWidget() doesn't seem to work

                                  mrjjM 1 Reply Last reply
                                  0
                                  • Please_Help_me_DP Please_Help_me_D

                                    @mrjj now I'm trying to set property to the tabWidgets to wich CDockWidget belongs to:

                                    QVTKOpenGLStereoWidget* MainWindowObjectCreator::createVTK2DWindow(){
                                        QVTKOpenGLStereoWidget* qvtkOpenGLStereoWidget = new QVTKOpenGLStereoWidget;
                                        ads::CDockWidget* dockWidget = new ads::CDockWidget("2D Window");
                                        dockWidget->setWidget(qvtkOpenGLStereoWidget);
                                        dockManager->addDockWidget(ads::RightDockWidgetArea, dockWidget);
                                        dockWidget->tabWidget()->setProperty("IsVisualization", 4443);
                                        //dockWidget->dumpObjectTree();
                                        return qvtkOpenGLStereoWidget;
                                    }
                                    

                                    but each time I manually change the focus the MainWindow::onFocusChanged(QWidget* old, QWidget* now) works twice:
                                    first time now is the old window
                                    second time is the new window
                                    and when the second time this method works then I get the correct value of property:

                                    void MainWindow::onFocusChanged(QWidget* old, QWidget* now){
                                        QWidget * fw = qApp->focusWidget();
                                        fw->dumpObjectTree();
                                        if (now != nullptr){
                                            now->dumpObjectTree();
                                            QVariant varia = now->property("IsVisualization"); 
                                        }
                                    }
                                    

                                    By the way this works only if the focused window is tabbed. Otherwise the focused widget is not a tabWidget(). So idea to set property to the tabWidget() doesn't seem to work

                                    mrjjM Offline
                                    mrjjM Offline
                                    mrjj
                                    Lifetime Qt Champion
                                    wrote on last edited by mrjj
                                    #21

                                    @Please_Help_me_D

                                    Ah silly me. of cause now will be the same as qApp->focusWidget(); :)
                                    and it does seem to be the actual DockWidget ! which i didn't expect.

                                    Is it possible you have 2 connects to the onFocusChanged signal ?
                                    Not really sure why it should run twice. I don't recall it normally does that.

                                    Its normally not an issue to loop up the chain to find the top parent.
                                    So not sure why it dont seem to work for you here.

                                    Please_Help_me_DP 1 Reply Last reply
                                    1
                                    • mrjjM mrjj

                                      @Please_Help_me_D

                                      Ah silly me. of cause now will be the same as qApp->focusWidget(); :)
                                      and it does seem to be the actual DockWidget ! which i didn't expect.

                                      Is it possible you have 2 connects to the onFocusChanged signal ?
                                      Not really sure why it should run twice. I don't recall it normally does that.

                                      Its normally not an issue to loop up the chain to find the top parent.
                                      So not sure why it dont seem to work for you here.

                                      Please_Help_me_DP Offline
                                      Please_Help_me_DP Offline
                                      Please_Help_me_D
                                      wrote on last edited by
                                      #22

                                      @mrjj yes, now I also find this looking for parent not working for me strange :)
                                      Just a moment I guess that I'm missing something important...

                                      mrjjM 1 Reply Last reply
                                      0
                                      • Please_Help_me_DP Please_Help_me_D

                                        @mrjj yes, now I also find this looking for parent not working for me strange :)
                                        Just a moment I guess that I'm missing something important...

                                        mrjjM Offline
                                        mrjjM Offline
                                        mrjj
                                        Lifetime Qt Champion
                                        wrote on last edited by
                                        #23

                                        @Please_Help_me_D
                                        Yeah as the dumptree clearly shows its the top as we expect
                                        ads::CDockWidget::2D Window
                                        QBoxLayout::
                                        QAction::
                                        QScrollArea::dockWidgetScrollArea
                                        QWidget::qt_scrollarea_viewport
                                        QVTKOpenGLStereoWidget::
                                        QVBoxLayout::
                                        ....

                                        But the test with qApp->focusWidget(); showd now is the Dock so
                                        looking at this code.

                                        f (now != nullptr){
                                        ads::CDockWidget* dockWidget = nullptr;
                                        QObjectList objList = now->children();

                                        Do you only check its children ?
                                        and in that case, the line
                                        dockWidget = qobject_castads::CDockWidget*(objList.at(i));

                                        will always return null as you skip the now ? and only cast children ?

                                        Please_Help_me_DP 1 Reply Last reply
                                        0
                                        • mrjjM mrjj

                                          @Please_Help_me_D
                                          Yeah as the dumptree clearly shows its the top as we expect
                                          ads::CDockWidget::2D Window
                                          QBoxLayout::
                                          QAction::
                                          QScrollArea::dockWidgetScrollArea
                                          QWidget::qt_scrollarea_viewport
                                          QVTKOpenGLStereoWidget::
                                          QVBoxLayout::
                                          ....

                                          But the test with qApp->focusWidget(); showd now is the Dock so
                                          looking at this code.

                                          f (now != nullptr){
                                          ads::CDockWidget* dockWidget = nullptr;
                                          QObjectList objList = now->children();

                                          Do you only check its children ?
                                          and in that case, the line
                                          dockWidget = qobject_castads::CDockWidget*(objList.at(i));

                                          will always return null as you skip the now ? and only cast children ?

                                          Please_Help_me_DP Offline
                                          Please_Help_me_DP Offline
                                          Please_Help_me_D
                                          wrote on last edited by Please_Help_me_D
                                          #24

                                          @mrjj let me clarify one moment.
                                          Earlier wrote an unclear post about what widget which dumptree has.
                                          Also there are two possible ways how ads::CDockWidget is attached to the application:

                                          1. CDockWidget is tabbed
                                          2. CDockWidget is float (we can pick and drag it as it is like a new window)

                                          So we have different dumptrees for QWidget* now object on onFocusChanged(QWidget* old, QWidget* now)

                                          Here is dumptree for tabbed CDOckWidget:

                                          ads::CDockWidgetTab:: 
                                              QBoxLayout:: 
                                              ads::CElidingLabel::dockWidgetTabLabel 
                                              QPushButton::tabCloseButton 
                                          

                                          And here is for float CDockWidget:

                                          QScrollArea::dockWidgetScrollArea 
                                              QWidget::qt_scrollarea_viewport 
                                                  QVTKOpenGLStereoWidget:: 
                                                      QVBoxLayout:: 
                                                      QWindowContainer:: 
                                              QWidget::qt_scrollarea_hcontainer 
                                                  QScrollBar:: 
                                                  QBoxLayout:: 
                                              QWidget::qt_scrollarea_vcontainer 
                                                  QScrollBar:: 
                                                  QBoxLayout:: 
                                          

                                          So we have a deal with two types of top level widgets :)
                                          But I would like to set the property only to a single type widget and if onFocusChanged() look for that object in now widget

                                          mrjjM 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