QWidget::visibleRegion() equivalent for QML items - qt

I would like to know the portion of a QML item that is visible on the screen. For example, if a Button is partially covered by a SplitView.
I can achieve this in QtWidgets using the function visibleRegion() but can't seem to find an equivalent function for QML items.

Related

Qt: Correct implementation of floating widgets

I've inherited my class from QWidget. Basically no extra code, just changes with the editor.
The way I use it:
focusWidget = new FocusWidget(this); //this points to the mainWindow
focusWidget->show();
focusWidget->hide();
Now the widget appears like this (it now looks ugly because of the bad 4k scaling), at the top left corner of the mainWindow.
I intend to use my application mostly full screen.
Is this usage correct?
How can I make it a floating widget?
If I want multiple widgets like that, how can I control their position?

How can I make a QMenuBar item appear over its QMenu

First of all, I'm fairly new with Qt and Qt Creator so go easy if this is a stupid question.
I was practicing using Qt Creator, playing around with css styles. In particular, I'm trying to get the menubar and its menus to look something like this (on Windows): http://i.stack.imgur.com/9lMnQ.png.
However, the closest I've been able to get so far is this: http://i.stack.imgur.com/5Nlen.png.
I've searched online to see if anyone has tried something like this but I wasn't able to find anything.
The only possible solution I can think of is if the menubar item (with no bottom border) could be rendered in above the menu, so that they overlap, covering its top border over the width that they overlap.
If that won't work or is impossible or whatever please do suggest any other solutions/workarounds/hacks.
Thanks in advance!
I think that the only good solution is to avoid any tricks and create a new widget:
Create a new class inherited from QWidget with Qt::Popup attribute.
Place a QMenu into a layout of the widget.
Get a position of QMenuBar item which is clicked using QMenuBar::getActionGeometry.
Calculate position of the widget and of the tab in the widget to be placed over the menubar item.
Customize form of the widget using QWidget::setMask to make it look like a rectangle with a tab.
Show your widget instead of QMenu.

How to add scroll bar for an item that is larger than the screen resolution in qml?

I use QtQuick 1.1 and I have an item like this below:
Item {
id: myItem
width: 12345
height: 12345
//...
}
When I run my qml project, it doesn't show any scroll bar for this item (horizental and vertical).
How can i add scroll bar to it? And if I use Qt and QML together (using a QWidget and QDeclarativeView on it), then what's the solution?
Making UI using qml is kind of UI paradigm shift when compared to making desktop widgets. What you are expecting is a normal desktop widget behavior, which is absent in the most mobile platforms UIs. In them, usually, scroll bars are associated with the lists and not with complete pages.
You can however implement that in qml as well. You can have the top element as flickable instead of a rectangle, and the show the scroll bars yourself based on flicks on the page. Try to go through the qml RSSfeed example to understand how you can use combination of flickable and other elements to achieve this.
P.S. : Also, see the qml desktop components introduced in Qt5. They will give you the widget behavior. See if it fits what you want.
But again, you should ask yourself, what exactly are you trying to achieve here ?

How can you know when a specific part of an item drawn with a QStyledItemDelegate is hovered?

I have a custom QAbstractItemModel used to display information in a QTreeWidget. However, individual indices are drawn using a QStyledItemDelegate. One item that is drawn using the delegate is a pixmap. When the user hovers the mouse over the pixmap (either help event style or hover enter style is fine) I need to do something, what it is isn't important.
So my question is, how can I know when the mouse has hovered over a specific item inside a QTreeWidget, when that item is drawn using a delegate?
In other situations, I could just subclass QLabel, set the pixmap on it, and then do whatever I need in the event() function, but in this case there is no object behind the pixmap, it is just painted onto the screen, so it doesn't actually receive events. Is it possible to use the delegate to paint an actual widget where I want it to so that widget can receive events, or do I have to work around this some other way?
You could subclass QTreeWidget and reimplement the mouseMoveEvent. In the event you can use the itemAt function in order to check if a valid item is at the mouse position and then do what you want.
void MyTreeWidget::mouseMoveEvent(QMouseEvent * event)
{
QTreeWidget::mouseMoveEvent(event);
QTreeWidgetItem* treeItem = itemAt(event->pos());
if (treeItem != NULL)
doSomething(treeItem);
}
You could avoid subclassing QTreeWidget and implement it in the parent widget/main window. Notice however that the itemAt function expects coordinates in the widget's viewport so you should transform the coordinates to tree widget's coordinates. IMHO it is more elegant to subclass it and just implement the mouseMoveEvent function.
EDIT
If you need to detect the position of an icon within the widget item, it is a bit more advanced but you can check my answer to an older question for more details:
Position of icon in QTreeWidgetItem
There is no Qt built-in solution to this problem. The problem can be solved, however, by saving the geometry of the individual items that are painted using the delegate as they are painted. The paint() function, however, has a const modifier so the data structure you use to save the geometry must be mutable. In this case, I don't think this constitutes a breach of the principles of OOP, but is rather a prime example of why the mutable keyword exists and when it should be used.
You then need to subclass QTreeWidget so you can re-implement the mouseMoveEvent() function as webclectic said. Inside that function you can compare the position of the mouse to the geometry of the item that you painted earlier. If the mouse is inside the item, then it is being hovered.

Qt Flickable Widgets

I've been playing around with the Flickable class from Qt Labs. I understand pretty well how the example works, but I'm having trouble understanding how I can use it to display something other than something dynamically painted inside the paintEvent.
If I have a list of buttons that I want to be Flickable, is there a way to reimplement the Flickable paintEvent to call all the button's paintEvents?
It seems like the Flickable class pretty much figures out which buttons would be visible so all I'd have to do for each button is set its y position and tell it to paint itself at that position.
Any pointers would be appreciated...
In your subclass, you need to reimplement setScrollOffset. There you can do various things. The included ColorList just triggers a repainting update, but theoretically you could also e.g. move widgets around. In fact, there is no need to reimplement paintEvent in any subclass of Flickable.
Note: I wrote that Flickable class.

Resources