How to find out the screen number from qml code? - qt

I need to find out whether the popup is visible on the screen. In Qt, I would call
qApp->desktop()->screenNumber(this)
within the widget class function.
This code returns -1 when there is no screen number because the given widget is not visible on any screen. I need to find out when this case scenario happens to show the widget on default position on primary screen (when the given screen is not available).
I have no idea how to find out the current screen number in QML code or other way that the pop up is not visible.

I solved the issue by creating a subclass from QQuickWindow, setting the appropriate flags
setFlags(Qt::WindowStaysOnTopHint | Qt::Popup );
and creating a function which can be called to find out whether the popup is visible based on the pixel coordinates of the popup
bool BlinkReminderWindow::existPointOnScreen(int x, int y)
{
return QGuiApplication::screenAt(QPoint(x,y))!=nullptr;
}

Related

How to determine whether a QML component is currently visible on screen

I have a set of controls which have bindings to frequently changing data values. The data comes from a limited hardware-bus. Therefore, it would be better to disable the binding while the control is not visible on screen. The item's visible-property doesn't help in this case. So, how to determine if an Item-based QML widget is currently visible on screen (and not hidden by an overlay or currently outside the visible area)?
Source: https://forum.qt.io/topic/54116/how-to-check-if-a-item-is-currently-visible-on-screen
I have almost the same problem. Hoping someone here has a solution.
Here's what I would try to get working:
First, I presume there is a ScrollView or Flickable in play here? If so, then hook to signals like Flickable::movementEnded().
Second, when that signal fires, use Item::mapToItem() to check if each of your Item's visible rectangle (based on x, y, width, height) intersects your window's contentItem rectangle. Set the result as a boolean on each of your items and make sure the data retrieval is disabled when it is false (using && or a tertiary JS expression).
Or if more convenient, remove the binding when false and reapply it with Qt.binding() when true.

Deal with window state in MdiArea

I use in my app a MDI central widget. Currently I always open a child as maximized
child->showMaximized();
This cause problems if I use cascaded or tiled workspace.
Now I tried to find out if the subwindows are maximized or not. If not I want to open them in
child->showNormal();
But the snippet:
foreach(QWidget* widget, mdiArea->subWindowList()) {
if(widget->windowState()==Qt::WindowMaximized){
qDebug("maximized");
}else{
qDebug("not maximized");
}
}
It looks that this subwindow will only have WindowActive and WindowNoState.
Any idea how to check if the subwindow (child) is maximized or not?
With maximized I talk about the full mdiArea (Workspace)
windowState() returns a set of flags, so for example windowState() can indicate that a window is active (Qt::WindowActive) and maximized (Qt::WindowMaximized), so if you want to verify if one of them is active you must use the operator "&":
if(widget->windowState() & Qt::WindowMaximized)

What is the simplest way to get notified whenever the user scrolls a QScrollArea?

Just as a QPushButton provides a default clicked() signal, I expected QScrollArea to have a sliderChanged() or similar signal. Interestingly, the QScrollBar does have such a signal.
All I would like to do is to know what part of the huge widget inside the scroll area is visible, whenever the user scrolls it.
There are many solutions, none of which seem elegant to me:
subclass QScrollArea
subclass the widget inside the scroll area, and re-implement its paint event.
create a custom veiwport, using QScrollBar
periodically poll the position of the widget inside the scroll area. This seems to be the worst solution.
Is there a way without subclassing?
There is QAbstractSlider::valueChanged() signal that is emitted when the slider value has changed, with the new slider value as argument. This will notify you as soon as you scroll your view.
WRT the second problem, neither of mentioned points necessary. You need to:
1) Get the position of inner widget (if any) related to the scroll area:
QPoint p = scrollArea->widget()->pos();
It use to be a negative coordinates if you scrolled your view down/right or null without scrolling.
2) Get the size of the visible area
QSize s = scrollArea->viewport()->size();
With these two values you can construct a QRect that will represent the visible area of your inner widget.

Getting the absolute location of a QT widget nested in multiple layouts

I'd like to get the absolute location of a QT widget that is a (horizontal) layout which is itself located in a central widget that has a layout.
I've tried the QWidget::mapToGlobal and QWidget::mapTo but I keep getting (0,0).
Edit
Here's my current implementation
QPoint p( 0 , 0 ); // Recall that widget 'lineEdit' is in a layout, which is itself in a layout
QPoint point = ui.lineEdit->mapToGlobal( p );
"Absolute location" is meaningless.
QWidget::mapToGlobal, as the docs state:-
"Translates the widget coordinate pos to global screen coordinates",
This is not want you want, as you mention in the comments that you're looking for "the position of the widget relative to the top left corner of the window"
You can use the function QWidget::mapTo, but note the following in the documentation:-
The parent must not be 0 and must be a parent of the calling widget.
From the code you've added to the example, it looks like you've used Qt Designer to create and setup the widgets. As you're calling mapToGlobal, there are only two reasons for the mapToGlobal to return (0, 0):-
1) The widget's top left corner is actually at the top left of the screen
2) I suspect you're calling the map functions before the window and its widgets have been displayed on the screen.
Without seeing all your code, I would say that no. 2 is the most likely issue here, so you're calling the mapping functions in something like the constructor of the MainWindow.

Dockable window -- not maximise

I have made a gui which contains dockable windows.
If i click on maximize square on the docable window it comes out but does not occupies the full screen of my gui.
Example if i click syntax window it does not occupy full screen
Which property do i have to change to make docable window to occupy full screen ?
Please see the attached image.
You will need to create your own TitleBarWidget and set it with:
void QDockWidget::setTitleBarWidget ( QWidget * widget )
So you will be able to have as many buttons as you want and maximize it. Following code will help you with it:
QDockWidget *dockWidget = qobject_cast<QDockWidget*>(parentWidget());
dockWidget->showMaximized();
Edit: To keep the 2 existing buttons functionality:
The docking will be done with setFloating(bool ). So:
QDockWidget *dockWidget = qobject_cast<QDockWidget*>(parentWidget());
dockWidget->setFloating( !dockWidget->isFloating () );
For the close, parent close() method will work.
And, last edit, i promise ;).
You will need to have the title to show it on your titleWidget:
And it is in windowTitle : QString property of parent:

Resources