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::move is not relative to its parent Widget but seems on my screen...?

QWidget::move is not relative to its parent Widget but seems on my screen...?

Scheduled Pinned Locked Moved Solved General and Desktop
10 Posts 4 Posters 1.6k 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.
  • mbruelM Offline
    mbruelM Offline
    mbruel
    wrote on last edited by
    #1

    Hi,
    I've created a SpinnerWidget that is a child of QListWidget.
    I'm trying to set its position on the center of my QListWidget but I'm not managing to do it :'(
    When I do a move(10,10) the SpinnerWidget appears on the top left corner of my screen outside the mainWindow and also its parent that is my QListWidget. Any idea why? I'd except to see it on the top left corner of my QListWidget....

    JonBJ 1 Reply Last reply
    0
    • mbruelM mbruel

      Hi,
      I've created a SpinnerWidget that is a child of QListWidget.
      I'm trying to set its position on the center of my QListWidget but I'm not managing to do it :'(
      When I do a move(10,10) the SpinnerWidget appears on the top left corner of my screen outside the mainWindow and also its parent that is my QListWidget. Any idea why? I'd except to see it on the top left corner of my QListWidget....

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

      @mbruel
      Are you sure about the parentage?
      Are you using layouts? I think move() does not work as you would expect if you do.
      Try it in a standalone program, and against a widget other than a QListWidget?

      mbruelM 1 Reply Last reply
      0
      • JonBJ JonB

        @mbruel
        Are you sure about the parentage?
        Are you using layouts? I think move() does not work as you would expect if you do.
        Try it in a standalone program, and against a widget other than a QListWidget?

        mbruelM Offline
        mbruelM Offline
        mbruel
        wrote on last edited by mbruel
        #3

        @JonB yes I'm sure about the parenting.
        It's initialized like this, the second argument of WaitingSpinnerWidget being the parent.

        ProjectList::ProjectList(QWidget *parent) : QListWidget(parent)
          , m_Spinner(new WaitingSpinnerWidget(Qt::WindowModality::ApplicationModal, this, false))
        {
            QScroller::grabGesture(viewport(), QScroller::LeftMouseButtonGesture);
        
            setVerticalScrollMode(QAbstractItemView::ScrollPerPixel);
            verticalScrollBar()->setSingleStep(20);
            setSelectionRectVisible(false);
            setSelectionMode(QAbstractItemView::NoSelection);
            setSpacing(5);
            setSortingEnabled(true);
        
            connect(this, &QListWidget::itemDoubleClicked, this, &ProjectList::projectDoubleClicked);
        
            setObjectName("ProjectList");
            setStyleSheet("QListWidget#ProjectList { background: " GT_LIGHTGREY "; border: none; border-top: 1px solid lightgrey; margin: 0px 10px 0px 10px; }");
        }
        

        But my ProjectList is in a QFrame which use a Layout would that be the issue?
        I'd expect my

        QWidget* HomePage::buildProjectsSide()
        {
            QFrame* frame = new QFrame;
            QVBoxLayout* layout = new QVBoxLayout;
            layout->setMargin(0);
            layout->setSpacing(0);
        
            QHBoxLayout* projectsHeaderLayout = new QHBoxLayout;
            projectsHeaderLayout->setMargin(10);
            m_ProjectsBtn = new QPushButton(tr("Project(s)", "", 0), this);
        ...
            projectsHeaderLayout->addWidget(m_ProjectsBtn, 1);
        ...
            layout->addLayout(projectsHeaderLayout);
        ...
            m_ListProjects = new ProjectList(this);
        ...
            layout->addWidget(m_ListProjects, 1);
            frame->setObjectName("HomePageFrame");
            frame->setStyleSheet("QFrame#HomePageFrame { background: " GT_LIGHTGREY "; border: 1px solid lightgrey; border-radius: 5px; }");
        
            frame->setLayout(layout);
            return frame;
        }
        

        @JonB well my widget has to be a ListWidget, and I'd like to make appear a modal loading spinner widget in its center when it is refreshing the data

        JonBJ J.HilkJ 2 Replies Last reply
        0
        • mbruelM mbruel

          @JonB yes I'm sure about the parenting.
          It's initialized like this, the second argument of WaitingSpinnerWidget being the parent.

          ProjectList::ProjectList(QWidget *parent) : QListWidget(parent)
            , m_Spinner(new WaitingSpinnerWidget(Qt::WindowModality::ApplicationModal, this, false))
          {
              QScroller::grabGesture(viewport(), QScroller::LeftMouseButtonGesture);
          
              setVerticalScrollMode(QAbstractItemView::ScrollPerPixel);
              verticalScrollBar()->setSingleStep(20);
              setSelectionRectVisible(false);
              setSelectionMode(QAbstractItemView::NoSelection);
              setSpacing(5);
              setSortingEnabled(true);
          
              connect(this, &QListWidget::itemDoubleClicked, this, &ProjectList::projectDoubleClicked);
          
              setObjectName("ProjectList");
              setStyleSheet("QListWidget#ProjectList { background: " GT_LIGHTGREY "; border: none; border-top: 1px solid lightgrey; margin: 0px 10px 0px 10px; }");
          }
          

          But my ProjectList is in a QFrame which use a Layout would that be the issue?
          I'd expect my

          QWidget* HomePage::buildProjectsSide()
          {
              QFrame* frame = new QFrame;
              QVBoxLayout* layout = new QVBoxLayout;
              layout->setMargin(0);
              layout->setSpacing(0);
          
              QHBoxLayout* projectsHeaderLayout = new QHBoxLayout;
              projectsHeaderLayout->setMargin(10);
              m_ProjectsBtn = new QPushButton(tr("Project(s)", "", 0), this);
          ...
              projectsHeaderLayout->addWidget(m_ProjectsBtn, 1);
          ...
              layout->addLayout(projectsHeaderLayout);
          ...
              m_ListProjects = new ProjectList(this);
          ...
              layout->addWidget(m_ListProjects, 1);
              frame->setObjectName("HomePageFrame");
              frame->setStyleSheet("QFrame#HomePageFrame { background: " GT_LIGHTGREY "; border: 1px solid lightgrey; border-radius: 5px; }");
          
              frame->setLayout(layout);
              return frame;
          }
          

          @JonB well my widget has to be a ListWidget, and I'd like to make appear a modal loading spinner widget in its center when it is refreshing the data

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

          @mbruel said in QWidget::move is not relative to its parent Widget but seems on my screen...?:

          But my ProjectList is in a QFrame which use a Layout would that be the issue?

          I think it may be, I suspect any layout may interfere (though not sure).

          well my widget has to be a ListWidget, and I'd like to make appear a modal loading spinner widget in its center when it is refreshing the data

          The point is to test the behaviour to discover the issue, not a long-term solution. E.g. now try removing any layout just to see if that is the issue, then rethink what to actually do if it is.

          1 Reply Last reply
          1
          • mbruelM mbruel

            @JonB yes I'm sure about the parenting.
            It's initialized like this, the second argument of WaitingSpinnerWidget being the parent.

            ProjectList::ProjectList(QWidget *parent) : QListWidget(parent)
              , m_Spinner(new WaitingSpinnerWidget(Qt::WindowModality::ApplicationModal, this, false))
            {
                QScroller::grabGesture(viewport(), QScroller::LeftMouseButtonGesture);
            
                setVerticalScrollMode(QAbstractItemView::ScrollPerPixel);
                verticalScrollBar()->setSingleStep(20);
                setSelectionRectVisible(false);
                setSelectionMode(QAbstractItemView::NoSelection);
                setSpacing(5);
                setSortingEnabled(true);
            
                connect(this, &QListWidget::itemDoubleClicked, this, &ProjectList::projectDoubleClicked);
            
                setObjectName("ProjectList");
                setStyleSheet("QListWidget#ProjectList { background: " GT_LIGHTGREY "; border: none; border-top: 1px solid lightgrey; margin: 0px 10px 0px 10px; }");
            }
            

            But my ProjectList is in a QFrame which use a Layout would that be the issue?
            I'd expect my

            QWidget* HomePage::buildProjectsSide()
            {
                QFrame* frame = new QFrame;
                QVBoxLayout* layout = new QVBoxLayout;
                layout->setMargin(0);
                layout->setSpacing(0);
            
                QHBoxLayout* projectsHeaderLayout = new QHBoxLayout;
                projectsHeaderLayout->setMargin(10);
                m_ProjectsBtn = new QPushButton(tr("Project(s)", "", 0), this);
            ...
                projectsHeaderLayout->addWidget(m_ProjectsBtn, 1);
            ...
                layout->addLayout(projectsHeaderLayout);
            ...
                m_ListProjects = new ProjectList(this);
            ...
                layout->addWidget(m_ListProjects, 1);
                frame->setObjectName("HomePageFrame");
                frame->setStyleSheet("QFrame#HomePageFrame { background: " GT_LIGHTGREY "; border: 1px solid lightgrey; border-radius: 5px; }");
            
                frame->setLayout(layout);
                return frame;
            }
            

            @JonB well my widget has to be a ListWidget, and I'd like to make appear a modal loading spinner widget in its center when it is refreshing the data

            J.HilkJ Online
            J.HilkJ Online
            J.Hilk
            Moderators
            wrote on last edited by
            #5

            @mbruel can you show the constructor of WaitingSpinnerWidget ?


            Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


            Q: What's that?
            A: It's blue light.
            Q: What does it do?
            A: It turns blue.

            1 Reply Last reply
            0
            • mbruelM Offline
              mbruelM Offline
              mbruel
              wrote on last edited by
              #6

              @J-Hilk here it is, pretty simple:

              WaitingSpinnerWidget::WaitingSpinnerWidget(Qt::WindowModality modality,
                                                         QWidget *parent,
                                                         bool centerOnParent,
                                                         bool disableParentWhenSpinning)
                  : QWidget(parent, Qt::Dialog | Qt::FramelessWindowHint),
                    _centerOnParent(centerOnParent),
                    _disableParentWhenSpinning(disableParentWhenSpinning){
                  initialize();
              
                  // We need to set the window modality AFTER we've hidden the
                  // widget for the first time since changing this property while
                  // the widget is visible has no effect.
                  setWindowModality(modality);
                  setAttribute(Qt::WA_TranslucentBackground);
              }
              

              in case the initialized:

              void WaitingSpinnerWidget::initialize() {
                  _color = QColor(73,73,73);
                  _roundness = 100.0;
                  _minimumTrailOpacity = 3.14159265358979323846;
                  _trailFadePercentage = 80.0;
                  _revolutionsPerSecond = 1.57079632679489661923;
                  _numberOfLines = 20;
                  _lineLength = 10;
                  _lineWidth = 2;
                  _innerRadius = 10;
                  _currentCounter = 0;
                  _isSpinning = false;
              
                  _timer = new QTimer(this);
                  connect(_timer, SIGNAL(timeout()), this, SLOT(rotate()));
                  updateSize();
                  updateTimer();
                  hide();
              }
              

              @JonB yeah could be a problem somewhere with Layout, not sure... I tried to remove the one its direct parent (QFrame) but it didn't change anything...
              There might be 3 or 4 parents before arriving to the MainWindow... not sure I could remove them all...

              1 Reply Last reply
              0
              • Christian EhrlicherC Offline
                Christian EhrlicherC Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on last edited by
                #7

                @mbruel said in QWidget::move is not relative to its parent Widget but seems on my screen...?:

                ApplicationModal

                Adding a widget to a layout with such an attribute is for sure not a good idea.

                Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                Visit the Qt Academy at https://academy.qt.io/catalog

                1 Reply Last reply
                0
                • mbruelM Offline
                  mbruelM Offline
                  mbruel
                  wrote on last edited by mbruel
                  #8

                  @Christian-Ehrlicher
                  I've changed the modality to be Qt::WindowModality::WindowModal but it doesn't change anything.
                  I've even removed the setWindowModality(modality); from the constructor but still the position is outside my QListWidget when I do a move(10, 10) for example :'(

                  PS: the Widget is not added to any Layout. Its is just a children of the parent but never added in the layout. How should I include it? I'd like it to be a modal overlay on the parent.
                  Oh I guess that's the problem, it's not included in the QListWidget. What should I do?

                  J.HilkJ 1 Reply Last reply
                  0
                  • mbruelM mbruel

                    @Christian-Ehrlicher
                    I've changed the modality to be Qt::WindowModality::WindowModal but it doesn't change anything.
                    I've even removed the setWindowModality(modality); from the constructor but still the position is outside my QListWidget when I do a move(10, 10) for example :'(

                    PS: the Widget is not added to any Layout. Its is just a children of the parent but never added in the layout. How should I include it? I'd like it to be a modal overlay on the parent.
                    Oh I guess that's the problem, it's not included in the QListWidget. What should I do?

                    J.HilkJ Online
                    J.HilkJ Online
                    J.Hilk
                    Moderators
                    wrote on last edited by
                    #9

                    @mbruel

                    Qt::Dialog

                    try it without that flag set


                    Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                    Q: What's that?
                    A: It's blue light.
                    Q: What does it do?
                    A: It turns blue.

                    1 Reply Last reply
                    0
                    • mbruelM Offline
                      mbruelM Offline
                      mbruel
                      wrote on last edited by
                      #10

                      Thanks you all :)
                      got it working by removing everything and just letting the setWindowModality(Qt::WindowModality::WindowModal)

                      Full constructor becoming:

                      WaitingSpinnerWidget::WaitingSpinnerWidget(bool insideWidget, QWidget *parent)
                          : QWidget(parent), _insideWidget(true), _disableParentWhenSpinning(true)
                      {
                          Q_UNUSED(insideWidget) // no need, just here to have a distinct constructor
                          initialize();
                          setAttribute(Qt::WA_TranslucentBackground);
                          setWindowModality(Qt::WindowModality::WindowModal);
                      }
                      
                      1 Reply Last reply
                      1

                      • Login

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