Update: Debug info
I am working on a Qt 5.3 project and need to plot data in 2D and 3D coordinate systems. I've been looking into vtk 6.1 because it seems very powerful overall and I will also need to visualize image data at a later point. I have using Qvtkwidget smoothly with this example
Qvtkwidget example
#include "mainwindow.h"
#include <QApplication>
#include <vtkAutoInit.h>
VTK_MODULE_INIT(vtkRenderingOpenGL);
#include <vtkSmartPointer.h>
#include <vtkSphereSource.h>
#include <vtkPolyDataMapper.h>
#include <vtkActor.h>
#include <vtkImageViewer.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkInteractorStyleImage.h>
#include <vtkRenderer.h>
#include <vtkJPEGReader.h>
#include <QVTKWidget.h>
int main(int argc, char** argv)
{
QApplication app(argc, argv);
QVTKWidget widget;
widget.resize(256,256);
// Setup sphere
vtkSmartPointer<vtkSphereSource> sphereSource =
vtkSmartPointer<vtkSphereSource>::New();
sphereSource->Update();
vtkSmartPointer<vtkPolyDataMapper> sphereMapper =
vtkSmartPointer<vtkPolyDataMapper>::New();
sphereMapper->SetInputConnection(sphereSource->GetOutputPort());
vtkSmartPointer<vtkActor> sphereActor =
vtkSmartPointer<vtkActor>::New();
sphereActor->SetMapper(sphereMapper);
// Setup window
vtkSmartPointer<vtkRenderWindow> renderWindow =
vtkSmartPointer<vtkRenderWindow>::New();
// Setup renderer
vtkSmartPointer<vtkRenderer> renderer =
vtkSmartPointer<vtkRenderer>::New();
renderWindow->AddRenderer(renderer);
renderer->AddActor(sphereActor);
renderer->ResetCamera();
widget.SetRenderWindow(renderWindow);
widget.show();
app.exec();
return EXIT_SUCCESS;
}
But when I tried to implement the graph example (/Examples/Charts/Cxx/QChartTable.cxx ), the program show Segmentation Fault errors.
#include <vtkAutoInit.h>
VTK_MODULE_INIT(vtkRenderingOpenGL)
#include "vtkFloatArray.h"
#include "vtkMath.h"
#include "vtkRenderer.h"
#include "vtkRenderWindow.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkSmartPointer.h"
#include "vtkContextView.h"
#include "vtkContextScene.h"
#include "vtkChartXY.h"
#include "vtkPlot.h"
#include "vtkTable.h"
#include "vtkTimerLog.h"
#include <QApplication>
#include <QWidget>
#include <QMainWindow>
#include <QHBoxLayout>
#include "QVTKWidget.h"
#include "vtkQtTableView.h"
#define VTK_CREATE(type, name) \
vtkSmartPointer<type> name = vtkSmartPointer<type>::New()
int main(int argc, char** argv)
{
// Qt initialization
QApplication app(argc, argv);
QMainWindow mainWindow;
mainWindow.setGeometry(0, 0, 1150, 600);
// QVTK set up and initialization
QVTKWidget *qvtkWidget = new QVTKWidget(&mainWindow);
// Set up my 2D world...
VTK_CREATE(vtkContextView, view); // This contains a chart object
view->SetInteractor(qvtkWidget->GetInteractor());
qvtkWidget->SetRenderWindow(view->GetRenderWindow());
// Create a table with some points in it...
VTK_CREATE(vtkTable, table);
VTK_CREATE(vtkFloatArray, arrX);
arrX->SetName("X Axis");
table->AddColumn(arrX);
VTK_CREATE(vtkFloatArray, arrC);
arrC->SetName("Cosine");
table->AddColumn(arrC);
VTK_CREATE(vtkFloatArray, arrS);
arrS->SetName("Sine");
table->AddColumn(arrS);
// Make a timer object - need to get some frame rates/render times
VTK_CREATE(vtkTimerLog, timer);
// Test charting with a few more points...
int numPoints = 29;
float inc = 7.0 / (numPoints-1);
table->SetNumberOfRows(numPoints);
for (int i = 0; i < numPoints; ++i)
{
table->SetValue(i, 0, i * inc);
table->SetValue(i, 1, cos(i * inc) + 0.0);
table->SetValue(i, 2, sin(i * inc) + 0.0);
}
// table->Update();
// Add multiple line plots, setting the colors etc
vtkSmartPointer<vtkChartXY> chart = vtkSmartPointer<vtkChartXY>::New();
view->GetScene()->AddItem(chart);
vtkPlot *line = chart->AddPlot(vtkChart::LINE);
line->SetInputData(table, 0, 1);
line->SetColor(255, 0, 0, 255);
line = chart->AddPlot(vtkChart::LINE);
line->SetInputData(table, 0, 2);
line->SetColor(0, 255, 0, 255);
line->SetWidth(2.0);
// Instantiate a vtkQtChart and use that too
/* vtkQtChart *qtChart = new vtkQtChart;
chart = qtChart->chart();
line = chart->AddPlot(vtkChart::LINE);
line->SetTable(table, 0, 1);
line->SetColor(255, 0, 0, 255);
line = chart->AddPlot(vtkChart::LINE);
line->SetTable(table, 0, 2);
line->SetColor(0, 255, 0, 255);
line->SetWidth(2.0);
*/
// Now lets try to add a table view
QWidget *widget = new QWidget(&mainWindow);
QHBoxLayout *layout = new QHBoxLayout(widget);
VTK_CREATE(vtkQtTableView, tableView);
tableView->SetSplitMultiComponentColumns(true);
tableView->AddRepresentationFromInput(table);
tableView->Update();
layout->addWidget(qvtkWidget, 2);
//layout->addWidget(qtChart, 2);
layout->addWidget(tableView->GetWidget());
mainWindow.setCentralWidget(widget);
// Now show the application and start the event loop
mainWindow.show();
return app.exec();
}
I have no idea how this error occurs.
I have recompiled VTK in debug mode and get more debug info. This is the backtrace for this program. In this screenshot show that problem could come form "this" pointer "this" should be vtkTextRenderer * but "this" point to null pointer. I think this could be a bug in VTK 6.1 but have no idea how to fix it.Please give me some suggestion. Thanks
I am newbie and still can not post picture so I upload it to this link
Debug Screenshot
0 vtkTextRenderer::GetBoundingBox vtkTextRenderer.h 136 0x7ffff0fbda58
1 vtkTextRendererStringToImage::GetBounds vtkTextRendererStringToImage.cxx 62 0x7ffff0fbd68b
2 vtkOpenGLContextDevice2D::ComputeStringBounds vtkOpenGLContextDevice2D.cxx 885 0x7ffff650d338
3 vtkOpenGLContextDevice2D::ComputeStringBounds vtkOpenGLContextDevice2D.cxx 809 0x7ffff650cab4
4 vtkContext2D::ComputeStringBounds vtkContext2D.cxx 619 0x7ffff64f24dd
5 vtkAxis::GetBoundingRect vtkAxis.cxx 871 0x7ffff7a9d66d
6 vtkChartXY::UpdateLayout vtkChartXY.cxx 762 0x7ffff7ac6064
7 vtkChartXY::Paint vtkChartXY.cxx 325 0x7ffff7ac4450
8 vtkContextScenePrivate::PaintItems vtkContextScenePrivate.h 80 0x7ffff64ea83a
9 vtkContextScene::Paint vtkContextScene.cxx 120 0x7ffff64fc2f2
10 vtkContextActor::RenderOverlay vtkContextActor.cxx 239 0x7ffff64f5787
11 vtkRenderer::UpdateGeometry vtkRenderer.cxx 585 0x7ffff3518bc3
12 vtkOpenGLRenderer::DeviceRender vtkOpenGLRenderer.cxx 270 0x7ffff61102c0
13 vtkRenderer::Render vtkRenderer.cxx 292 0x7ffff3517dfb
14 vtkRendererCollection::Render vtkRendererCollection.cxx 51 0x7ffff35164d3
15 vtkRenderWindow::DoStereoRender vtkRenderWindow.cxx 759 0x7ffff352bc36
16 vtkRenderWindow::DoFDRender vtkRenderWindow.cxx 728 0x7ffff352bb46
17 vtkRenderWindow::DoAARender vtkRenderWindow.cxx 607 0x7ffff352b4f2
18 vtkRenderWindow::Render vtkRenderWindow.cxx 423 0x7ffff352aac2
19 vtkXOpenGLRenderWindow::Render vtkXOpenGLRenderWindow.cxx 1831 0x7ffff61abc51
20 vtkRenderWindowInteractor::Render vtkRenderWindowInteractor.cxx 168 0x7ffff3537751
... <More>
It is NOT a bug. The object vtkTextRenderer point to null bescause the Text Font module has NOT been implemented. It should been implemented using vtkRenderingFreeType.
Please try including this snippet in your code:
#include "vtkAutoInit.h"
VTK_MODULE_INIT(vtkRenderingFreeType);
more detail information can be found here: http://www.vtk.org/Wiki/VTK/VTK_6_Migration/Factories_now_require_defines
I have recompiled VTK in debug mode and get more debug info. This is the backtrace for this program. In this screenshot show that problem could come form "this" pointer "this" should be vtkTextRenderer * but "this" point to null pointer. I think this could be a bug in VTK 6.1 but have no idea how to fix it.Please give me some suggestion. Thanks
Update Debug Info in first post.
Related
In the VTK book at page 63 there is an example which I pasted below.
#include <vtkActor.h>
#include <vtkCamera.h>
#include <vtkConeSource.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkPolyDataMapper.h>
#include <vtkRenderWindow.h>
#include <vtkRenderer.h>
int main(int, char* [])
{
vtkNew<vtkNamedColors> colors;
vtkNew<vtkConeSource> cone;
cone->SetHeight(3.0);
cone->SetRadius(1.0);
cone->SetResolution(10);
vtkNew<vtkPolyDataMapper> coneMapper;
coneMapper->SetInputConnection(cone->GetOutputPort());
vtkNew <vtkActor> coneActor;
coneActor->SetMapper(coneMapper);
vtkNew <vtkRenderer > ren1;
ren1->AddActor(coneActor);
ren1->SetBackground(
colors->GetColor3d("MidnightBlue").GetData());
vtkNew <vtkRenderWindow > renWin;
renWin->AddRenderer(ren1);
renWin->SetSize(300, 300);
for (auto i = 0; i < 360; ++i)
{
// render the image
renWin->Render();
// rotate the active camera by one degree
ren1->GetActiveCamera()->Azimuth(1);
}
return EXIT_SUCCESS;
}
I have downloaded and compiled VTK 9.1 on my Windows 10 machine. I tried to run the example as it is and it crashes on the following line:
ren1->SetBackground(
colors->GetColor3d("MidnightBlue").GetData());
The culprit is actually the call to colors->GetColor3d("MidnightBlue").GetData(). Since I do not care what color the background is at this very early point, I replaced the crashing line with
ren1->SetBackground(0.20, 0.20, 0.20);
Now it runs to the end, but no window shows up. My expectation was that a window would pop up with a rotating cone inside. No such luck. Can anybody point me in the right direction? What am I doing wrong?
We are aiming to replace our previous 3D Engine with Qt3D. As a last obstacle we need to correctly implement a pixel correct transparency. We are now trying to implement depth peeling as a possible approach to make pixel correct transparency workable. For this algorithm one has to do perform a deferred (multipass) rendering, which can be achieved with QRenderPassFilter and QFilterKey inside an effect.
Now, I already had big problems to make the combination of QRenderPassFilter and QFilterKey together with the material QDiffuseSpecularMaterial going to work correctly. Even if there is just one pass.
This is my source code:
#include <QApplication>
#include <QWidget>
#include <QVBoxLayout>
#include <QFrame>
#include <Qt3DCore/QTransform>
#include <Qt3DRender/QSortPolicy>
#include <Qt3DRender/QRenderSettings>
#include <Qt3DRender/QRenderSurfaceSelector>
#include <Qt3DRender/QViewport>
#include <Qt3DRender/QCamera>
#include <Qt3DRender/QCameraSelector>
#include <Qt3DRender/QClearBuffers>
#include <Qt3DRender/QDirectionalLight>
#include <Qt3DRender/QTexture>
#include <Qt3DExtras/QPlaneMesh>
#include <Qt3DExtras/QDiffuseSpecularMaterial>
#include <Qt3DExtras/Qt3DWindow>
#include <Qt3DRender/QFilterKey>
#include <Qt3DRender/QParameter>
#include <Qt3DRender/QRenderPass>
#include <Qt3DRender/QRenderPassFilter>
#include <Qt3DRender/QTechnique>
#include <QDebug>
int main(int argc, char* argv[])
{
QApplication a(argc, argv);
auto view = new Qt3DExtras::Qt3DWindow();
auto mClearBuffers = new Qt3DRender::QClearBuffers;
auto mMainCameraSelector = new Qt3DRender::QCameraSelector;
mMainCameraSelector->setCamera(view->camera());
auto mRenderSurfaceSelector = new Qt3DRender::QRenderSurfaceSelector;
auto mMainViewport = new Qt3DRender::QViewport;
auto renderPassFilter = new Qt3DRender::QRenderPassFilter;
{
auto filterKey = new Qt3DRender::QFilterKey(renderPassFilter);
filterKey->setName(QStringLiteral("renderingStyle"));
filterKey->setValue(QStringLiteral("forward"));
// Adding the filterKey to the renderPassFilter hides the plane
// Name and Value of filterKey matches the FilterKey inside the QDiffuseSpecularMaterial
renderPassFilter->addMatch(filterKey); // Removing this lines shows the plane mesh
mClearBuffers->setClearColor(Qt::lightGray);
mClearBuffers->setBuffers(Qt3DRender::QClearBuffers::BufferType::ColorDepthBuffer);
mMainCameraSelector->setParent(mClearBuffers);
mClearBuffers->setParent(renderPassFilter);
}
renderPassFilter->setParent(mRenderSurfaceSelector);
mRenderSurfaceSelector->setParent(mMainViewport);
view->setActiveFrameGraph(mMainViewport);
view->activeFrameGraph()->dumpObjectTree();
auto rootEntity = new Qt3DCore::QEntity();
view->setRootEntity(rootEntity);
view->camera()->lens()->setPerspectiveProjection(45.0f, 1., 0.1f, 10000.0f);
view->camera()->setPosition(QVector3D(0, 2, 0));
view->camera()->setUpVector(QVector3D(0, 1, 0));
view->camera()->setViewCenter(QVector3D(0, 0, 0));
auto planeEntity = new Qt3DCore::QEntity(rootEntity);
auto meshMaterial = new Qt3DExtras::QDiffuseSpecularMaterial;
meshMaterial->setDiffuse(QColor("#ff00ff"));
planeEntity->addComponent(meshMaterial);
auto mesh = new Qt3DExtras::QPlaneMesh;
mesh->setWidth(0.3);
mesh->setHeight(0.3);
planeEntity->addComponent(mesh);
auto container = QWidget::createWindowContainer(view);
QFrame frame;
frame.setLayout(new QVBoxLayout);
frame.layout()->addWidget(container);
frame.resize(QSize(400, 300));
frame.show();
return a.exec();
}
The console outputs my framegraph as:
Qt3DRender::QViewport::
Qt3DRender::QRenderSurfaceSelector::
Qt3DRender::QRenderPassFilter::
Qt3DRender::QFilterKey::
Qt3DRender::QClearBuffers::
Qt3DRender::QCameraSelector::
Now, if I remove the line
renderPassFilter->addMatch(filterKey);
everything works as expected and I see my simple plane mesh.
However, adding the line, which should not filter anything the plane mesh is no longer displayed.
I'm really running out of ideas, what I'm possibly doing wrong here. How, can I make my small program with my renderPassFilter going to work and what are my errors?
I also didn't really understood, what are purposes of the settings name and value in the QFilterKey, which of both is necessary to filter out certain effects?
After carefully studying my application and particularly the QDiffuseSpecularMaterial I figured out, that the QFilterKey inside the QDiffuseSpecularMaterial is not added to the QRenderPass Object, but moreover added to the QTechnique, which I found rather obscure.
Now, adding a QTechniqueFilter instead of a QRenderPassFilter made the program working as expected. Changing the string forward to something different e.g. xxx hides the plane as expected.
Adding the line
meshMaterial->dumpObjectTree();
indeed gave me the clue
Qt3DExtras::QDiffuseSpecularMaterial::
Qt3DRender::QShaderProgramBuilder::
Qt3DRender::QShaderProgram::
Qt3DRender::QShaderProgramBuilder::
Qt3DRender::QShaderProgram::
Qt3DRender::QFilterKey::
Qt3DRender::QEffect::
Qt3DRender::QTechnique::
Qt3DRender::QRenderPass::
Qt3DRender::QNoDepthMask::
Qt3DRender::QBlendEquationArguments::
Qt3DRender::QBlendEquation::
Qt3DRender::QTechnique::
Qt3DRender::QRenderPass::
Qt3DRender::QTechnique::
Qt3DRender::QRenderPass::
Qt3DRender::QParameter::
Qt3DRender::QParameter::
Qt3DRender::QParameter::
Qt3DRender::QParameter::
Qt3DRender::QParameter::
So dumpObjectTree() seems to a good of the shelf debugging tool, when dealing with Qt3D and Qt in general.
#include <QApplication>
#include <QWidget>
#include <QVBoxLayout>
#include <QFrame>
#include <Qt3DCore/QTransform>
#include <Qt3DRender/QSortPolicy>
#include <Qt3DRender/QRenderSettings>
#include <Qt3DRender/QRenderSurfaceSelector>
#include <Qt3DRender/QViewport>
#include <Qt3DRender/QCamera>
#include <Qt3DRender/QCameraSelector>
#include <Qt3DRender/QClearBuffers>
#include <Qt3DRender/QTechniqueFilter>
#include <Qt3DRender/QDirectionalLight>
#include <Qt3DRender/QTexture>
#include <Qt3DExtras/QPlaneMesh>
#include <Qt3DExtras/QDiffuseSpecularMaterial>
#include <Qt3DExtras/Qt3DWindow>
#include <Qt3DRender/QFilterKey>
#include <Qt3DRender/QParameter>
#include <Qt3DRender/QRenderPass>
#include <Qt3DRender/QRenderPassFilter>
#include <Qt3DRender/QTechnique>
#include <QDebug>
int main(int argc, char* argv[])
{
QApplication a(argc, argv);
auto view = new Qt3DExtras::Qt3DWindow();
auto mClearBuffers = new Qt3DRender::QClearBuffers;
auto mMainCameraSelector = new Qt3DRender::QCameraSelector;
mMainCameraSelector->setCamera(view->camera());
auto mRenderSurfaceSelector = new Qt3DRender::QRenderSurfaceSelector;
auto mMainViewport = new Qt3DRender::QViewport;
auto renderPassFilter = new Qt3DRender::QTechniqueFilter;
{
auto filterKey = new Qt3DRender::QFilterKey(renderPassFilter);
filterKey->setName(QStringLiteral("renderingStyle"));
filterKey->setValue(QStringLiteral("forward"));
// Adding the filterKey to the renderPassFilter hides the plane
// Name and Value of filterKey matches the FilterKey inside the QDiffuseSpecularMaterial
renderPassFilter->addMatch(filterKey); // Removing this lines shows the plane mesh
mClearBuffers->setClearColor(Qt::lightGray);
mClearBuffers->setBuffers(Qt3DRender::QClearBuffers::BufferType::ColorDepthBuffer);
mMainCameraSelector->setParent(mClearBuffers);
mClearBuffers->setParent(renderPassFilter);
}
renderPassFilter->setParent(mRenderSurfaceSelector);
mRenderSurfaceSelector->setParent(mMainViewport);
view->setActiveFrameGraph(mMainViewport);
view->activeFrameGraph()->dumpObjectTree();
auto rootEntity = new Qt3DCore::QEntity();
view->setRootEntity(rootEntity);
view->camera()->lens()->setPerspectiveProjection(45.0f, 1., 0.1f, 10000.0f);
view->camera()->setPosition(QVector3D(0, 2, 0));
view->camera()->setUpVector(QVector3D(0, 1, 0));
view->camera()->setViewCenter(QVector3D(0, 0, 0));
auto planeEntity = new Qt3DCore::QEntity(rootEntity);
auto meshMaterial = new Qt3DExtras::QDiffuseSpecularMaterial;
meshMaterial->setDiffuse(QColor("#ff00ff"));
planeEntity->addComponent(meshMaterial);
auto mesh = new Qt3DExtras::QPlaneMesh;
mesh->setWidth(0.3);
mesh->setHeight(0.3);
planeEntity->addComponent(mesh);
auto container = QWidget::createWindowContainer(view);
QFrame frame;
frame.setLayout(new QVBoxLayout);
frame.layout()->addWidget(container);
frame.resize(QSize(400, 300));
frame.show();
return a.exec();
}
I am working on some codes about qt3d. I create a Qt3DWindow(named view).
Try to add a root entity(named rootEntity) in it.
Put a cube entity like this:
m_cubeEntity = new Qt3DCore::QEntity;
...
Qt3DExtras::QCuboidMesh *cubeMesh = new Qt3DExtras::QCuboidMesh;
Qt3DRender::QMaterial *cubeMaterial = new Qt3DRender::QMaterial;
Qt3DCore::QTransform *cubeTransform = new Qt3DCore::QTransform;
Qt3DRender::QObjectPicker *objectPicker = new Qt3DRender::QObjectPicker;
...
m_cubeEntity.addComponent(cubeMesh);
m_cubeEntity.addComponent(cubeMaterial);
m_cubeEntity.addComponent(cubeTransform);
m_cubeEntity.adComponent(objectPicker);
m_cubeEntity.setParent(m_rootEntity);
Everything works fine.
And then I find using PickingSettings.BoundingVolumePicking to pick my cube entity is inaccuracy. I want the PickingSettings.TrianglePicking.
====================================================================/
So then i do it like this:
m_renderSettings = new Qt3DRender::QRenderSettings();
m_renderSettings->pickingSettings()->setPickMethod(Qt3DRender::QPickingSettings::TrianglePicking);
m_renderSettings->pickingSettings()->setPickResultMode(Qt3DRender::QPickingSettings::AllPicks);
m_renderer = new Qt3DExtras::QForwardRenderer();
m_renderer->setClearColor(Qt::lightGray);
m_renderSettings->setActiveFrameGraph(m_renderer);
m_rootEntity->addComponent(m_renderSettings);
But now nothing is rendered. If i remove "m_renderSettings" from rootEntity, everything returns correct.
How to set rendersetting correctly for the root entity?
Most likely you just set a bad framegraph. You can easily display the standard framegraph using dumpObjectTree(), which tends to be a very useful function in debugging the framegraph of Qt3D.
For your simple use case it suffices to just use the renderSettings already contained in the activeFrameGraph() of the Qt3DWindow.
Just try the following simple app, that contains your desired QObjectPicker.
#include <QApplication>
#include <Qt3DCore/QTransform>
#include <Qt3DRender/QPickEvent>
#include <Qt3DRender/QCamera>
#include <Qt3DRender/QFrameGraphNode>
#include <Qt3DRender/QObjectPicker>
#include <Qt3DExtras/QDiffuseSpecularMaterial>
#include <Qt3DExtras/QCuboidMesh>
#include <Qt3DExtras/Qt3DWindow>
#include <Qt3DRender/QRenderSettings>
#include <QDebug>
int main(int argc, char* argv[])
{
QApplication a(argc, argv);
auto view = new Qt3DExtras::Qt3DWindow();
auto rootEntity = new Qt3DCore::QEntity();
view->setRootEntity(rootEntity);
// Shows your framegraph! Simple forward renderer!
view->activeFrameGraph()->dumpObjectTree();
auto rendersettings=view->renderSettings();
rendersettings->pickingSettings()->setPickMethod(Qt3DRender::QPickingSettings::TrianglePicking);
rendersettings->pickingSettings()->setPickResultMode(Qt3DRender::QPickingSettings::AllPicks);
auto cameraEntity = view->camera();
cameraEntity->lens()->setPerspectiveProjection(45.0f, 1., 0.1f, 10000.0f);
cameraEntity->setPosition(QVector3D(0, 2, 0));
cameraEntity->setUpVector(QVector3D(0, 1, 0));
cameraEntity->setViewCenter(QVector3D(0, 0, 0));
auto cubeEntity = new Qt3DCore::QEntity(rootEntity);
auto cubeMesh = new Qt3DExtras::QCuboidMesh;
cubeMesh->setXExtent(1.);
cubeMesh->setYExtent(1.);
auto cubeMaterial = new Qt3DExtras::QDiffuseSpecularMaterial;
auto objectPicker = new Qt3DRender::QObjectPicker;
QObject::connect(objectPicker, &Qt3DRender::QObjectPicker::clicked, [](Qt3DRender::QPickEvent* pick) {
qDebug() << pick;
});
cubeEntity->addComponent(cubeMesh);
cubeEntity->addComponent(cubeMaterial);
cubeEntity->addComponent(objectPicker);
view->show();
return a.exec();
}
When graphing a line series within a small range using QtChart library (for example, between 0 and 1e-15), the line series seemingly does not get rendered. Using larger numbers with the same exact code works perfectly.
I've tried boiling down my code to be as simple as possible, to find any mistakes I was making, but I cannot find any. I suspect this may be a bug in Qt, but I'm posting here in case I missed something before I file a bug report.
I've also failed to find any information online about people experiencing similar bugs, but that may just be an indication that my google-fu is weak.
I originally encountered this bug using PyQt5, but have recreated it in C++ to ensure it was not a PyQt5 specific bug.
#include <QtWidgets/QApplication>
#include <QtWidgets/QMainWindow>
#include <QtCharts/QChartView>
#include <QtCharts/QLineSeries>
#include <QtCharts/QLegend>
#include <QtCharts/QValueAxis>
QT_CHARTS_USE_NAMESPACE
#define FACTOR 1e-13
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QLineSeries *lineseries = new QLineSeries();
lineseries->setName("series");
lineseries->setUseOpenGL(true);
for (double i = 0.0; i < 10.0; i += 1.0)
lineseries->append(QPointF(i*FACTOR, i * FACTOR));
QChart *chart = new QChart();
chart->addSeries(lineseries);
chart->setTitle("Line example");
QValueAxis *axisX = new QValueAxis();
chart->setAxisX(axisX, lineseries);
QValueAxis *axisY = new QValueAxis();
chart->setAxisY(axisY, lineseries);
axisY->setRange(0.0, 9.0*FACTOR);
axisX->setLabelFormat("%.3E");
axisY->setLabelFormat("%.3E");
chart->legend()->setVisible(true);
chart->legend()->setAlignment(Qt::AlignBottom);
QChartView *chartView = new QChartView(chart);
chartView->setRenderHint(QPainter::Antialiasing);
QMainWindow window;
window.setCentralWidget(chartView);
window.resize(840, 900);
window.show();
return a.exec();
}
Running the code attached, I would expect to observe a linear line series rendered in the plot, but I see nothing rendered.
Modifying the FACTOR constant to something >= 1e-12 will make the line series display properly.
I need to modify a QPolarChart to create a Satellite SkyPlot.
For this, I need to reverse the radial axis (elevation axis) so that 90 is placed on the origin and 0 on the outer ring.
In the list of members for QValueAxis I found the method setReverse, which seems to do what I need.
However, doing setReverse(true) on the elevation axis does not flip the axis as I was expecting.
How can I reverse the axis then?
This is the minimal code required to run this example:
#include <QtWidgets/QApplication>
#include <QtWidgets/QMainWindow>
#include <QtCharts/QChartView>
#include <QtCharts/QPolarChart>
#include <QtCharts/QValueAxis>
#include <QScatterSeries>
QT_CHARTS_USE_NAMESPACE
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QtCharts::QPolarChart *chart = new QtCharts::QPolarChart();
chart->legend()->hide();
QScatterSeries *series = new QScatterSeries();
for (int i = 0; i < 360; i += 10) {
series->append(i, i);
}
chart->addSeries(series);
QValueAxis *azimuthAxis = new QValueAxis();
azimuthAxis->setRange(0, 360);
azimuthAxis->setTickCount(9);
azimuthAxis->setLabelFormat("%d");
azimuthAxis->setLabelsVisible(true);
chart->addAxis(azimuthAxis, QPolarChart::PolarOrientationAngular);
QValueAxis *elevationAxis = new QValueAxis();
elevationAxis->setRange(0, 90);
elevationAxis->setTickCount(7);
elevationAxis->setLabelFormat("%d");
elevationAxis->setLabelsVisible(true);
elevationAxis->setReverse(true); // <-- REVERSE THE AXIS
chart->addAxis(elevationAxis, QPolarChart::PolarOrientationRadial);
QtCharts::QChartView *chartView = new QtCharts::QChartView();
chartView->setChart(chart);
chartView->setRenderHint(QPainter::Antialiasing);
QMainWindow window;
window.setCentralWidget(chartView);
window.resize(800, 600);
window.show();
return app.exec();
}
UPDATE:
After reading Damien's comment:
You may check with isReverse if the command setReverse was taken into account
I made the test by putting qDebug() << elevationAxis->isReverse(); immediately before and immediately after elevationAxis->setReverse(true);, and it prints out false and true, respectively.
qDebug() << elevationAxis->isReverse(); // <-- Prints out "false"
elevationAxis->setReverse(true); // <-- REVERSE THE AXIS
qDebug() << elevationAxis->isReverse(); // <-- Prints out "true"
So the reverse property is indeed changed. However, this change is not reflected visually in the axis.
The documentation for the reverse property reads:
This property holds whether a reverse axis is used.
By default, the value is false.
The reverse axis is supported with a line, spline, and scatter series,
as well as an area series with a cartesian chart. All axes of the same
orientation attached to the same series must be reversed if one is
reversed or the behavior is undefined.
A polar chart is certainly NOT a cartesian chart, so that might be the root of the problem.
I couldn't make the setReversed(true) work whatever the way I tried to do it.
So I found another solution: Do it manually.
I printed the series in reversed way:
QScatterSeries *series = new QScatterSeries();
for (int i = 0; i < 360; i += 10) {
series->append(i, MAX_ELEVATION - i);
}
MAX_ELEVATION equals to 90.
Then I reversed the labels names. For that I had to replace QValueAxis by its derived QCategoryAxis.
The full code
#include <QtWidgets/QApplication>
#include <QtWidgets/QMainWindow>
#include <QtCharts/QChartView>
#include <QtCharts/QPolarChart>
#include <QtCharts/QValueAxis>
#include <QScatterSeries>
#include <QtCharts/QCategoryAxis>
QT_CHARTS_USE_NAMESPACE
#define MAX_ELEVATION 90
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QtCharts::QPolarChart *chart = new QtCharts::QPolarChart();
chart->legend()->hide();
QScatterSeries *series = new QScatterSeries();
for (int i = 0; i < 360; i += 10) {
series->append(i, MAX_ELEVATION - i);
}
chart->addSeries(series);
QValueAxis *azimuthAxis = new QValueAxis();
azimuthAxis->setRange(0, 360);
azimuthAxis->setTickCount(9);
azimuthAxis->setLabelFormat("%d");
azimuthAxis->setLabelsVisible(true);
chart->addAxis(azimuthAxis, QPolarChart::PolarOrientationAngular);
/*
QValueAxis *elevationAxis = new QValueAxis();
elevationAxis->setRange(0, 90);
elevationAxis->setTickCount(7);
elevationAxis->setLabelFormat("%d");
elevationAxis->setLabelsVisible(true);
elevationAxis->setReverse(true); // <-- REVERSE THE AXIS
chart->addAxis(elevationAxis, QPolarChart::PolarOrientationRadial);
*/
QCategoryAxis *elevationAxis = new QCategoryAxis;
elevationAxis->setRange(0, MAX_ELEVATION);
for(unsigned int i = 0; i <= MAX_ELEVATION; i += 15)
elevationAxis->append(QString::number(MAX_ELEVATION-i), i);
elevationAxis->setLabelsPosition(QCategoryAxis::AxisLabelsPositionOnValue);
elevationAxis->setLabelsVisible(true);
chart->addAxis(elevationAxis, QPolarChart::PolarOrientationRadial);
QtCharts::QChartView *chartView = new QtCharts::QChartView();
chartView->setChart(chart);
chartView->setRenderHint(QPainter::Antialiasing);
QMainWindow window;
window.setCentralWidget(chartView);
window.resize(800, 600);
window.show();
return app.exec();
}
It is equivalent to what setReversed(true) should do. It will display your series exactly the way you want without modifying anything of the way the data are computed.
Here you can see what it looks like:
It worked successfully for me.
Hope it can help.