QWidget::move is not relative to its parent Widget but seems on my screen...?
-
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 amove(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.... -
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 amove(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.... -
@mbruel
Are you sure about the parentage?
Are you using layouts? I thinkmove()
does not work as you would expect if you do.
Try it in a standalone program, and against a widget other than aQListWidget
?@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 myQWidget* 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
-
@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 myQWidget* 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
@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.
-
@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 myQWidget* 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-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... -
@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.
-
@Christian-Ehrlicher
I've changed the modality to beQt::WindowModality::WindowModal
but it doesn't change anything.
I've even removed thesetWindowModality(modality);
from the constructor but still the position is outside my QListWidget when I do amove(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? -
@Christian-Ehrlicher
I've changed the modality to beQt::WindowModality::WindowModal
but it doesn't change anything.
I've even removed thesetWindowModality(modality);
from the constructor but still the position is outside my QListWidget when I do amove(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? -
Thanks you all :)
got it working by removing everything and just letting thesetWindowModality(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); }