How to get actual position of qtoolbar? - qt

I am just trying to get the actual x, y values or Qt::ToolBarArea area of my QToolBar.
Is there any function to get it so?

Check out QWidget::mapTo and associated functions. I reckon you want to do something like:
yourWidget->mapToParent(0, 0) - maps the top left corner of yourWidget (i.e. 0, 0) to the coordinate system of its parent widget.

Related

Xcode 7 Swift 2 - Move button within a specific view area

I would like to move a button's position (X & Y) within a specific area of a view object.
What I've done so far is, I've added a view object on to my main screen and I've also added a button inside the view, so far OK.
Now, I managed to get the bounds of the view object and I know the size of my button say (50x50). So what I want is, when I click on the button, it should move to another random location but within the view object, it should not move outside the view object boundaries. Right now what happens in my button appears to be moving a portion of it outside the view object based on the random X & Y.
Any help will be appreciated.
I managed to get it as well. Here is how I did it:
I created a function which I pass the shape to and return the location that I want the piece to move to which is within the bounds of the view.
func randomizeLocation(shape: CGRect) -> CGPoint {
let randomX = arc4random_uniform(UInt32(gameBoard.frame.width - shape.width)) + UInt32(shape.width / 2)
let randomY = arc4random_uniform(UInt32(gameBoard.frame.height - shape.height)) + UInt32(shape.height / 2)
return CGPointMake(CGFloat(randomX), CGFloat(randomY))
}
Just in speaking about the x coordinate (left/right)
The logic here is that arc4random_uniform will return a random # between 0 and the given number (- 1).
But you don't want a number starting at zero because that would put the object off the edge of the screen to the left by 1/2. So, if you add 1/2 of the width to the overall function, you will never go off to the left side of the screen.
To keep from going off the right side of the screen you want a maximum number of the screen width minus 1/2 of the width of the object. To get this, you must specify the number you pass to the arc4random function as minus the whole width of the shape, because when you add back half, you are shifting by 1/2 of the width of your object on both sides (minimum and maximum return value).
So to use this function, take your object, lets say a UILabel and set its center to this functions result as such:
let myLabel = UIButton(type: .Custom)
myLabel.frame = CGRectMake(100, 100, 100, 100)
myLabel.center = randomizeLocation(myLabel.bounds)
And that it. Btw, I did not account for the -1 in the return b/c i figured the one pixel was too insignificant to bother with..

Find center of a fixed-width bounding box

Given a collection of points, I'd like to find the center of a bounding box (fixed-length and width) that maximizes the number of points within said box. I'm at a loss for an efficient way to do this.
Algorithm with complexity O(N^2*logN) (I hope that better one exists):
Edit: article exploiting interval trees claims O(NlogN) complexity
Sort data array A by X coordinate.
Scan A with sweep line left to right.
For every point in A get LeftX = A[k].X - left coordinate of vertical band, find the rightmost coordinate of vertical band RightX = LeftX + Width.
Copy points inside the band to another array B.
Sort B by Y-coordinate.
Scan B width sweep line top to down.
For every point B[i] get TopY = B[i].Y - top coordinate of rectangle, calculate BottomY = TopY + Height.
Use binary search in B:
B[j] is the last bottom point in B with B[j].Y <= BottomY.
Find number of points in the current rectangle:
Number of points is N(k, i) = j - i + 1
Check whether N(k, i) is maximum among others
This seems like a difficult problem, here is my idea:
Hold a graph, each node holds a rectangle and a subset of points. the rectangle defines the area where placing the bounding box in would overlap all the points in the subset.
To build the graph:
Start with a root node holding the empty set and the rect [top:-inf, bottom:inf, left:-inf, right:inf]
For each point in the tree call this recursive function with the root node (pseudo code):
function addPoint(node, point)
// check that you didn't already try to add this point to this node
// node.tested can be a hash set
if(node.tested contains point)
return
node.tested.add(point)
newRect = node.rect.intersectWith(boundingBoxAround(point))
// if the bounding box around the point does not intersect the rectangle, return
if(newRect is invalid) // rect is invalid if right<left or bottom<top
return
node.addChild(new node(newRect, node.pointSet U {point})
for each child of node
addPoint(child, point)
Now you just pick the node with the largest subset, you can keep track of that when building the graph so you don't need to run through the graph again.
I hope my idea is clear, let me know if I can explain it better.

proper way of calculating mouse 'z' position (Unity 3d)

Whenever I develop games that use mouse input, I will get confused of calculating the mouse position. Especially the z position.
The ways I saw many using.
mouse position z = mouse position y.
z = distance between camera and object.
z = difference b/w object z and camera z. (I am using. Doesn't work when camera and object is rotated).
z = some arbitrary value. (many use 0 and some other values).
others.
Which method is correct? Is there any other method which is correct?
Please let me know.
The same answer as in your second mouse based question applies here too: Mouse based aiming Unity3d
TL;DR: use a raycast from camera to intersect the plane that the action is on.
Vector3 pz = Camera.main.ScreenToWorldPoint(Input.mousePosition);
pz.z should be what you are asking for if I'm understanding right. Tell me if it works
The mouse position is in theory external to the game world itself. Therefore, the mouse position relates simply to X and Y co-ordinates of the screen space in which you are interacting with your game (IE width:height of your game).
What you're asking seems to be more of "How do I model the mouse position in my game world?"
As noted by Mario, Camera.main.ScreenToWorldPoint(Input.mousePosition) will convert your mouse position to world co-ordinates. However, this assumes that your main camera is where you want to convert to. In reality, you want to call the ScreenToWorldPoint method on the Camera that is rendering whatever space it is you are wanting to interact with. For example, you may have your main game world at (0, 0, 0) but you may be rendering your GUI on top using a separate camera that renders objects at (-5000, 0, 0).
To answer your question, to model the mouse z position it should simply be the same z value as your Camera. You can then perform calculations on that value to suit your particular needs.
IE:
1) mouse.position.z = mouse.position.y - These are entirely different. Now you're just using an arbitrary value
2) Distance between camera and object - That's a calculation made from your original object.position.z and original mouse.position.z. Not your actual z value.
3) See 2.
4) See 1.

Dimensioning in Qt

I have graphicsView in which different items are drawn in the scene.I want to have an option for dimensioning i.e to dimension.
There are different items like Line, Circle, Ellipse, Arc, Point, Text,
I want whenver the the two points are clicked on any item,or the anywhere on the scene it should tell me its dimension.
The items are drawn with the mouseclicks. Can I get help to proceed?
The items are inherited from QGraphicsItem, LineItem.
Like:http://imgur.com/kBOjfmw
You can use something like this to get the length in inches:
qreal distance(QPointF & p1, QPointF & p2) {
return QLineF(p1, p2).length() / QApplication::screens().at(0)->physicalDotsPerInch();
}
Multiply by 2.54 if you want centimeters. Then create a QGraphicsItem which draws the line between the two points and text with the distance.
You can use void QWidget::mousePress/Release/Event(QMouseEvent * event) and get the click position from the event->pos(), and you should map to the scene coords when you are actually drawing the dimension.
If you don't want the actual line length but the horizontal or vertical length as in that image you posted as example, you can calculate that from the absolute difference between the two points x or y components.

How to get the negative position value in Group?

The dashed rectangle is the parent group and inside, there is a label. Its x is negative.
Now, what I want to do is relocation the outside group to the contents' top-left point and meanwhile the contents' move back to the outside group's (0,0) point. The result looks like everything keeps the same position as before.but in fact, both inside content and outside group is moved.
It is easy to realize in flash, however, in flex i got trouble.
The function "getRect" returns wrong values.it's never return the correct position the inside content is.(like the thumb shows,the position should be like [-70,50])
(Feel free to correct me because I'm not sure what you want to accomplish here)
If your Label (let's say it is called myLabel) is correctly located directly inside of your Group, simply calling myLabel.x will return the X-coordinate of the label compared to its parent (which is your Group here, so you should get -70).
Then if you want to move the label so it fits into your Group viewport, you have two solutions:
Either you manually set myLabel.x = 0 and myLabel.y = 0. In this case the label will actually be moved at the Group origin.
Either you retrieve the matrix of your label component to call its .translate(dx, dy) function. Using the matrix functions will modify the way your Label is displayed, but its position will remain unchanged (More information about that on this page).
Short answer: If you don't care about keeping the original position of your label, just set myLabel.x = 0 and myLabel.y = 0 and it should be moved correctly.

Resources