I want to transform a QGraphicsItem by its Qt::YAxis, what works very well.
QTransform trans;
trans.rotate (value, Qt::YAxis);
item->setTransformOriginPoint (0,0);
//item->setTransformOriginPoint (500, 250);
item->setTransform (trans);
The problem i have is that the setTransformOriginPoint seems to be ignored 'cause it dont work. Everytime my item transforms at its left-side. But i want to transform it by its right-side and i think i am affected by this bug:
https://bugreports.qt-project.org/browse/QTBUG-13378?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel
Can someone confirm a similar problem like this?
Or what i can do to transform my item at its right-side?
Edit: Sorry i forgot to say that i not use an animation like described at this bug-report!
A QTransform has everything about the transformation, include the origins where you want the transform to happen. setTransformOriginPoint() only affect simple functions like rotate() that does not specify the origins.
Try adding translate to your QTransform before adding rotate().
QTransform trans;
trans.translate(500, 250).rotate(value, Qt::YAxis);
item->setTransform(trans);
Edit:
Yes, QTransform in QGraphicsItem is not very straightforward to use. It's also really limited on what you can do. Starting with Qt 4.6, QGraphicsTransform can be used for higher level of control on the transformation. That may be what you are looking for.
Related
So I have been struggling with this question for quite some time. I have tried many things, none of them seem to work.
So, I want to make a game in Qt, and one of the things I need is that player(QRectItem for now) rotate always to the mouse position. I just need to get readings of that position all the time, so not when i click or when I drag, all the time.
How can I do that?
I set
this->setMouseTracking(true);
on a class that inherits for QGraphicsView class, also I have set focus on it.
Dont know if the problem is with overriding functions(dont know which one to override) or with focus.
void Game::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
qDebug() << QCursor::pos();
}
Did this but it does not work at all.
Btw, I am noob it Qt, this is my first project.
Thanks in advance! :)
P.S.
I have really done research, but if I have somehow missed topic with same or similar question that can solve this problem, just paste it and accept my apologies. :)
EDITED
You can install an event filter on your QApplication object, examine the received events for mouse movement events, convert the resulting position into your scene, and then use it to orient your rectangle.
Look at QObject::installEventFilter. Event filters are pretty easy to use. When a mouse event is received by an object, its coordinates are in that object's coordinate space, so you'll need to convert from that to your graphics scene coordinates. There will probably be several conversions to get that because you'll need to map the received position to your QGraphicsView using mapTo and then map the result of that to your scene using QGraphicsView::mapToScene.
This should get you pretty close. Let me know if you need more help.
I have made a controller for a car and I am currently trying to make a GUI for the controller in Qt.
The only thing I am lacking now is a visualization of the car's position and angle relative to its starting point.
I want to make something like this:
where the circle represents the car and the line in it represents its heading/angle.
An added bonus feature would be to have a fading trace of its path as it moves, but if I got the basics down, I should be able to sort that out myself.
I have tried looking at some plot examples, but couldn't extrapolate what I needed to solve my problem.
How would you recommend going about the implementation of something like this?
You say:
I have all the measurements
So let's assume that you have Euler angles such that you can filter out changes in roll and only consider changes in φ.
To do this you'll be looking to extend a QWidget adding a member φ, we'll name it: m_phi. We'll also need to add your QPixMap as a member, we'll call it m_px. And you'll be overriding the QWidget::paintEvent.
paintEvent(qPaintEvent* /*event*/) {
if(!m_px.isNull()) {
QPainter* p;
p.setRenderHint(QPainter::Antialiasing);
p.translate(width() / 2.0, height() / 2.0);
p.save();
p.rotate(m_phi);
QRect r = m_px.rect();
r.moveCenter(QPoint());
p.drawPixmap(r, m_px);
p.restore();
}
}
I found that video on Youtube. It shows how to flip something called QML Flipable. I'm completely unfamiliar with QML so I have the following questions:
1) Is it possible to flip an ordinary QLabel like this? If yes how do I do that?
2) If it's not possible then how do I add that Flipable entity to my QMainWindow?
Thanks, everyone
You could use the QGraphicsPixmap with your image , set the rotation center with setTransformOriginPoint and animate the transformation matrix with QProertyAnimation
There's no convenient way to do this. Desktop OS's don't provide this functionality.
One option is to use QGraphicsView. You can add widgets to a QGraphicsScene using QGraphicsScene::addWidget(), for example, and then transform the view the way you want.
Quick google search points me to a blog article where author of the video (Kunal Parmar) describing how this particular effect was done.
I am using a QGraphicsPolygonItem and I have noticed that it always connects the end-point with the start-point.
I know that the polygon terms means exactly that, and what I am looking for is "polyline" or "polygonal chain". I didnt found nothing like that in QGraphicsItem subclasses.
How do I draw a polygonal chain in QGraphics Framework? Is there a property of QGraphicsPolygonItem or a class that does that?
I had a similar problem, and I solved it by using the QGraphicsPathItem class. In the following code, polygon is a non-closed QPolygonF object (i.e. a QPolygonF which start-point is different from its end-point):
QPainterPath path = new QPainterPath();
path.addPolygon(polygon);
QGraphicsPathItem contour = new QGraphicsPathItem(path);
contour.setPen(new QPen(QColor.black));
When displaying this QGraphicsPathItem object, the start-point is (in theory) disconnected from its end-point.
I'm sorry this example code is in Java; but the mechanisms should be the same as in C++.
You can use QPainterPath and use lineTo method to input yors polyline points, then just use QGraphicsPathItem to make it graphics item.
Alternatively you also might think about combining several QGraphicsLineItem into one QGraphicsItemGroup, but that's more difficult as you need to pay attention to aligning lines together.
Is this what you are looking for?
EDIT:
QPainterPath is apparently closing paths, then you are left with group of lines only.
EDIT2:
Sorry for confusing you, but HostileFork seem to be right - you just use QPainterPath and call pathItem->setBrush(QBrush(Qt::transparent)); to keep your path unfilled.
I subclassed QGraphicsItem and reimplemented paint.
In paint I wrote something like this for labeling the item:
painter->drawText("Test",10,40);
After some time I think It may be useful to handle labeling with seperate item. So I wrote something like this.
QGraphicsTextItem *label = new QGraphicsTextItem("TEST",this);
setPos(10,40);
But two "TEST" drawing do not appear in the same place on screen. I guess difference may be related with item coordinates - scene coordinates. I tried all mapFrom... and mapTo... combinations inside QGraphicsItem interface but no progress. I want to drawings to appear in the same place on screen.
What I miss?
I assume that you are using the same font size and type in both cases. If the difference in position is very small the reason can be the QGraphicTextItem is using some padding for the text it contains. I would try to use QGraphicsSimpleTextItem that is not going to add fancy stuff internally and see if you still have the same problem. The coordinates system is the same one if you use painter or setPost so that is not the problem. If this doesn't help I will suggest to specify the same rect for both to avoid Qt adding it owns separation spaces.