|
Rotate 3D surface in QT |
Posted by Luca on Sep-18-2013 02:01 |
|
Hello,
could you provide a small example or a quick guide on how to implement a rotatable (through mouse click&drag) 3D surface in QT?
I can easily display a 3D surface in a QChartViewer but I don't know how to make it rotate.
Thank you very much for your help. |
Re: Rotate 3D surface in QT |
Posted by Peter Kwan on Sep-19-2013 01:12 |
|
Hi Luca,
To rotate a 3D surface, simply draw it with a different angle. For example, you may create a "drawChart" subroutine like:
void drawChart(double elevationAngle, douible rotationAngle)
{
.... create surface chart as usual .....
c->setViewAngle(elevationAngle, rotationAngle);
..... continue creating the chart and display it as usual ......
}
The drawChart routine can be used to draw your initial chart with initial elevation and rotationAngle angles. When you want to change the angle, you can call drawChart again with the new angle. In this way, you can rotate the chart to any angle you like.
The second part is to create a user interface to allow the user to modify the angles. You may use scrollbars, sliders, spin buttons, text boxes to allow the user to directly input the angles, or to compute the rotation angles based on the direction of mouse movements, etc.. The user interface part of your application would need to be written by you.
Hope this can help.
Regards
Peter Kwan |
Re: Rotate 3D surface in QT |
Posted by Luca on Sep-20-2013 03:19 |
|
This is what I'm trying to do
void QChartViewer::onPlotAreaMouseDrag(QMouseEvent *event)
{
switch (m_mouseUsage)
{
// SNIPPED CODE
case Chart::MouseUsageScroll :
{
qDebug() << event->pos() << m_plotAreaMouseDownXPos - event->pos().x() << m_plotAreaMouseDownYPos - event->pos().y();
if (m_isDragScrolling || isDrag(m_scrollDirection, event))
{
qDebug() << "CHECK THIS IS EXECUTED";
m_isDragScrolling = true;
SurfaceChart *temp = (SurfaceChart*)this->getChart();
temp->setViewAngle(145,45,0); // Just test values
this->setChart(temp);
updateViewPort(true, true);
}
}
}
}
But I can't get it to work. The code is executed but nothing happens on the chart.
I tried to add:
temp.addTitle(0, "Title");
just for testing purposes and it works fine.
I guess I can't just change the viewing angle once the chart has been created.
I have to create a new chart each time, am I right?
Thank you very much. |
Re: Rotate 3D surface in QT |
Posted by Peter Kwan on Sep-21-2013 03:12 |
|
Hi Luca,
Once the chart is displayed, you cannot modify the chart. So in your case, the code should be:
drawChart(145, 45);
The drawChart routine is the code I described in my previous message.
If you would like to control the scrolling inside onPlotAreaMouseDrag, I suggest you simply put the following code in onPlotAreaMouseDrag:
//elevationAngle and rotationAngle and two member variables you need to add to the
//QChartViewer object. I hard coded it to 145 and 45 for testing. In your real code, you
//need to modify them according to the mouse movement.
elevationAngle = 145;
rotationAngle = 45;
updateViewPort(true, true);
Then in the QChartViewer.viewPortChanged signal handler, simply do:
drawChart(elevationAngle , rotationAngle);
Hope this can help.
Regards
Peter Kwan |
|