Qt QGraphicsScene::drawItems subsitute? - qt

For QGraphicsScene::drawItems the reference says:
Reimplement this function to provide custom painting of all items for the scene; gaining complete control over how each item is drawn.
But this function is marked as obsolete.
Is there any new equivalent method?

QGraphicsView::paintEvent() now calls
d->scene->d_func()->drawItems()
which means the method is part of class QGraphicsScenePrivate which you cannot override afaik.
If you need to change the way your items are drawn, first try to think of another way (i.e. a solution which does not require stepping into the drawItems() method). If you can't find any solution like that, your only chance is reactivating the pre-4.6-behaviour by setting
QGraphicsView::setOptimizationFlag( QGraphicsView::IndirectPainting )

Related

Flex: select tree node right after the dataProvider is been assigned / updated / replace

i have a Flex tree control and im trying to select a tree node 3 levels down right after the dataProvider is assigned with a collection object like the following.
basically treeItem1, treeItem2, treeItem3 are the nodes in the tree and treeitem3 is a child of treeItem2 which is a child of treeItem1. Assume these treeItem(1,2,3) are referenced correctly from the collection items.
my problem is that if i wait for the whole component to load completely then select the nodes, it open/select/scrolltoIndex correctly. However, if i were to select the node right after the dataProvider is assigned, then it doesn't even open or select (basically the this.treeService.selectedItem is always null).
can anyone point out what i did wrong? is there anything needs to happen after the dataProvider is assigned?
thanks
this.treeService.dataProvider = oPricingHelper.getCurrentPricingSercicesTreeSource();
this.treeService.expandItem(treeItem1, true);
this.treeService.expandItem(treeItem2, true);
this.treeService.selectedItem = treeItem3;
this.treeService.scrollToIndex(this.treeService.selectedIndex);
I have used the updateComplete event to know when a component (such as a DataGroup or List) has completed rendering after performing a simple task (such as updating the dataProvider reference). Of course, you have to be careful and remove listening to updateComplete because it can run a lot, unless you have a need for it to run.
Something like:
//...some function...
this.treeService.addEventListener(FlexEvent.UPDATE_COMPLETE, onTreeUpdateComplete);
this.treeService.dataProvider = oPricingHelper.getCurrentPricingSercicesTreeSource();
//...rest of some function...
private function onTreeUpdateComplete(event:FlexEvent):void {
this.treeService.removeEventListener(FlexEvent.UPDATE_COMPLETE, onTreeUpdateComplete);
this.treeService.expandItem(treeItem1, true);
this.treeService.expandItem(treeItem2, true);
this.treeService.selectedItem = treeItem3;
this.treeService.scrollToIndex(this.treeService.selectedIndex);
}
I'm not positive your experiencing the same issue but I seem to have the same type of problem with using the advanced data grid, it appears in these cases where the dataprovider is acceptable as multiple types, the components do some extra work in the background to wrap things up into something Hierarchical (HierarchicalData or HierarchicalCollectionView) and in doing so the dataprovider setter call is not synchronous (so it will return before actually having assigned the internal property storing the dataprovider). I've used callLater in this case with moderate success, callLater is generally a bad practice but basically adds a function to a list of functions to call once background processing is done, so this is assuming that something in the dataprovider setter called UIComponent.suspendBackgroundProcessing() and that it will subsequently call UIComponent.resumeBackgroundProcessing() and then it will execute the list of functions added by using callLater. Alternatively you could use setTimeout(someFunction,1000).
These are both "hacks" the real solution is to dig into the framework code and see what it's really doing when you tell it to set the dataprovider. Wherever you see that it actually has set the dataprovider you could extend that class and dispatch an event that you could listen for to run the function to do the selections after this point.
If anyone has a better solution please by all means correct me (I would love to have a better answer than this)

whats up with QLayout->setParent

When I try to change the location of a layout with setParent the result is non-functional or odd.
the following works:
ui.txDiag_1->setParent(ui.tab_2);
movingHlayout = new QVBoxLayout(ui.tab_2);
movingHlayout->setSpacing(3);
movingHlayout->setMargin(3);
movingHlayout->setObjectName(QString::fromUtf8("movingHlayout"));
movingHlayout->addWidget(ui.txDiag_1);
but this doesnt(movingHlayout has been constructed before hand):
ui.txDiag_1->setParent(ui.tab_2);
movingHlayout->setParent(ui.tab_2);
movingHlayout->setSpacing(3);
movingHlayout->setMargin(3);
movingHlayout->setObjectName(QString::fromUtf8("movingHlayout"));
movingHlayout->addWidget(ui.txDiag_1);
You possibly have to call setLayout() on the widget you are setting the layout up in.
http://doc.qt.io/qt-5/qwidget.html#setLayout
As you can see from the docs, the ownership of the layout will automatically be set to the target widget.
To complete the other answer, here is the reason why setParent does not work as you expect:
setParent is not in QLayout implementation, but in QObject only. So using it will only change the pointer ownership and deletion, not the widget layout mechanism.

How do I determine if the item is selected or not in the paint() function of QItemDelegate?

I am making a table using QItemDelegate. I use the paint(..) method to draw delegated items to look the same when they go out of edit mode but I also need to draw the items differently when they are selected or not and the paint method is also called during those events. My question is how do I know when to draw which?
thank you.
The QItemDelegate.paint method accepts a QStyleOptionViewItem as a parameter, which is inherited from QStyleOption. QStyleOption has a member variable named state of type QStyle::State. One of the possible bit flags for state is QStyle::State_Selected, which is what you want to test for.

QGraphicsItem unselect redraw problem

Very simple Qt GUI application:
On the scene I have multiple circles implemented as QGraphicsItem
boundingRect returns square around
this circle.
Method 'shape' is not overridden.
The problem appears when in paint() method I've added:
if (isSelected()) {
painter->drawRect(re);
}
Selection is drawn well, but unselection doesn't cause redrawing. At log level I can see that item really lost selection flag.
Calling update() from itemChange is useless also.
Thank you in advance for any suggestion.
After 10 days I returned back to this problem and discovered that my QGraphicsItem constructed with
setCacheMode(DeviceCoordinateCache);
OMG! Stupid mistake, when this line was removed (by default QGraphicsItem::NoCache used) selection is redrawn well.
You can also try to change the default QGraphicsView::MinimalViewportUpdate to FullViewportUpdate with setViewportUpdateMode(QGraphicsView::FullViewportUpdate);
Or you can call scene()->update(); from the item to schedule a repaint.
One of them was required at least when I kept changing the QGraphicsItem::ItemHasNoContents flag on an item.

Flex - Is there a way to specify what direction a ComboBox will open?

Maybe I should further qualify this - Is there a way to specify which direction a ComboBox will open without copying and pasting the entire ComboBox class and ripping out the code where it determines which direction it will open in...
I'm my specific case - I need it to open upwards - always.
UPDATE: You can't fix this by subclassing it because the function that handles the direction of the opening is:
private function displayDropdown(show:Boolean, trigger:Event = null):void
And that bad boy uses a fair amount of private variables which my subclass wouldn't have access to...
If you build up the Menu object yourself, you can place the menu anywhere you want by simply setting the x,y coordinates of the menu object. You'll need to calculate those coordinates, but you might be able to do this easily without subclassing ComboBox.
I am doing something similar with PopUpButton; you might find it easier to work with PopUpButton. This is based on real code from my current project:
private function initMenu(): void {
var m:Menu = new Menu();
m.dataProvider = theMenuData;
m.addEventListener(MenuEvent.ITEM_CLICK, menuClick);
m.showRoot = false;
// m.x = ... <-- probably don't need to tweak this.
// m.y = ... <-- this is really the interesting one :-)
theMenu.popUp = m;
}
<mx:PopUpButton id="theMenu" creationComplete="initMenu()" ... />
BTW, to get the PopUpButton to act more like I wanted it (always popup, no matter where the click), setting openAlways=true in the MXML works like a charm.
I doubt it - you'd need to subclass the control (which isn't that big a deal.)
Maybe you could mess with the real estate so it's placed in such a fashion (e.g. crowded into the lower right corner) that up is naturally coerced?
I would recommend checking out this post. Yes, you do have to grab the ComboBox code and modify it, but at least now you have an idea where the modifications need to go.
You could set the MaxDropDownHeight, if you set it big enough Windows will automatically set the direction upwards.
This irritated me no end. I have uploaded a solution, its a simple Class that extends the PopUpButton and removes the logic of stage bounds detection as it failed 50% of the time anyway. My code just allows you to simply specify whether you want to open the menu up or down:
http://gist.github.com/505255

Resources