The app have QGraphicscene(mpscene) with QGraphicsView(mpGraphicsView) i am trying to add qml map object to QGraphicscene(mpscene) But map view come separately like popup window. How can i add this map to QGraphicscene(mpscene)?
Following are the few methods i tried as suggested in other posts.
method1
PlaneTrack *blr2bgm = new PlaneTrack; //Cpp object set to root context
QQmlEngine *engine = new QQmlEngine(mpGraphicsView);
QQuickWidget *view = new QQuickWidget(engine, mpGraphicsView);
view->setResizeMode(QQuickWidget::SizeViewToRootObject);
view->rootContext()->setContextProperty("blr2bgm",blr2bgm);
view->setSource(QUrl(QStringLiteral("qrc:/PlaneTrack.qml")));
mpScene->addWidget(view);
method2
PlaneTrack *blr2bgm = new PlaneTrack; //Cpp object set to root context
QQuickView *view = new QQuickView();
view->rootContext()->setContextProperty("blr2bgm",blr2bgm);
view->setSource(QUrl(QStringLiteral("qrc:/PlaneTrack.qml")));
view->setResizeMode(QQuickView::SizeRootObjectToView);
QWidget *container = QWidget::createWindowContainer(view);
container->setMinimumSize(view->size());
container->setFocusPolicy(Qt::TabFocus);
mpScene->addWidget(container);
In PlaneTrack.qml file window container was there that i changed to Item.
Following code adds qml to graphic scene.
PlaneTrack *blr2bgm = new PlaneTrack; // cpp class object data interact
QQuickWidget *view = new QQuickWidget();
view->rootContext()->setContextProperty("blr2bgm",blr2bgm);
view->setSource(QUrl(QStringLiteral("qrc:/PlaneTrack.qml")));
view->setMinimumSize(300, 300);
view->setResizeMode(QQuickWidget::SizeRootObjectToView);
mpScene->addWidget(view); // adding to Graphic scene
Related
I'm trying to record a video played by QMediaPlayer. Later I will need to put a simple widget (overlay) on top of it. What would be the best way to implement it? Here is my variant, but it does not produce any file:
Class members:
QWidget* parentWidget;
QMediaPlayer* mediaPlayer;
QVideoWidget* videoWidget;
QMediaRecorder *mediaRecorder;
Record function:
mediaPlayer = new QMediaPlayer(parentWidget);
mediaRecorder = new QMediaRecorder(mediaPlayer);
videoWidget = new QVideoWidget;
mediaPlayer->setVideoOutput(videoWidget);
mediaPlayer->setSource(QUrl::fromLocalFile("C:/DJI_0051.MP4"));
mediaRecorder->setVideoResolution(1920, 1080);
mediaRecorder->setQuality(QMediaRecorder::HighQuality);
mediaRecorder->setOutputLocation(QUrl::fromLocalFile("C:/DJI_0051_EDITED.MP4"));
mediaRecorder->setVideoFrameRate(60);
QMediaFormat mediaFormat;
mediaFormat.setAudioCodec(QMediaFormat::AudioCodec::AAC);
mediaFormat.setVideoCodec(QMediaFormat::VideoCodec::H264);
mediaFormat.setFileFormat(QMediaFormat::MPEG4);
mediaRecorder->setMediaFormat(mediaFormat);
mediaPlayer->play();
mediaRecorder->record();
I'm trying to load .obj files into Qt using the Qt3D library. So far I have this code mostly copied from one of the examples:
Qt3DCore::QEntity *createScene()
{
// Root entity
Qt3DCore::QEntity *rootEntity = new Qt3DCore::QEntity;
// Material
Qt3DRender::QMaterial *material = new Qt3DExtras::QPhongMaterial(rootEntity);
// Chest Entity
Qt3DCore::QEntity *chestEntity = new Qt3DCore::QEntity(rootEntity);
Qt3DRender::QMesh *chestMesh = new Qt3DRender::QMesh(rootEntity);
chestMesh->setSource(QUrl("qrc:/PT18E.obj"));
chestEntity->addComponent(chestMesh);
chestEntity->addComponent(material);
return rootEntity;
}
int main(int argc, char* argv[])
{
QGuiApplication app(argc, argv);
Qt3DExtras::Qt3DWindow view;
Qt3DCore::QEntity *scene = createScene();
// Camera
Qt3DRender::QCamera *camera = view.camera();
camera->lens()->setPerspectiveProjection(45.0f, 16.0f/9.0f, 0.1f, 1000.0f);
camera->setPosition(QVector3D(0, 0, 1));
camera->setViewCenter(QVector3D(0, 0, 0));
// For camera controls
Qt3DExtras::QOrbitCameraController *camController = new Qt3DExtras::QOrbitCameraController(scene);
camController->setLinearSpeed( 10.0f );
camController->setLookSpeed( 180.0f );
camController->setCamera(camera);
view.setRootEntity(scene);
view.show();
return app.exec();
}
So far, the application can render the object and display it. However, I had to attach the .obj file to a qrc file in order for it to be able to find and render it. When I try to set the source for the chestMesh using the absolute path from any directory, e.g.,
chestMesh->setSource(QURL(C:/Users/username/Documents/Qt/simple-cpp/PT18E.obj))
it does not work. Nothing gets rendered and I see the error in the console saying
QFSFileEngine::open: No file name specified
I've tried adding the exact same path to a QFile object and use the exists function to see if the path is correct, and sure enough it is. But for some reason the mesh cannot use a source that isn't in a qrc file.
How can I load and render any .obj file from the file system (without having to put it in a qrc) in Qt?
For composing the correct url pointing to the file we can try static call QUrl::fromLocalFile. Or given the above code:
chestMesh->setSource(QUrl::fromLocalFile(winPath2ObjFile));
I working on one application and I have problem with tab name.
When I click on push button (NEW) I want to dynamically create new tab.
With this function i create new file:
bool MainWindow::toolbarNewFile()
{
QWidget *page = new QWidget;
QTextEdit *codeEditor = new QTextEdit;
QGridLayout *layout = new QGridLayout;
layout->addWidget(codeEditor);
page->setLayout(layout);
tab_widget->addTab(page,"File");
return true;
}
But all tabs have name "FILE"
How to set in tab name number. When i make new tab auto set number of the tab like this.
File-1, File-2, File-3
I try to set counter i=0; and in addTab(page,"File-"+ i++); Doesn't work.
You need to covert integer to the QString to be able to concat it to the QString. Even better, you can use QString::arg function and get readable and potentially faster code very easily:
tab_widget->addTab(page, QString("File-%1").arg(i++));
Where i is field in your class initialized to 1.
A widget called Tachometer will update accordingly when a signal is sent from its corresponding model, but it will not respond at all when I add the widget to a layout. What might account for this? Without showing the internals of the model or the view (which would be exhaustive), I will attempt to point out where it is not working:
void MainWindow::setupWidgets()
{
QHBoxLayout *horiz1 = new QHBoxLayout();
QHBoxLayout *horiz2 = new QHBoxLayout();
odometer = new Odometer(ui->dashFrame);
fuelGauge = new FuelGauge(ui->dashFrame);
tripometer = new Tripometer(ui->dashFrame);
tachometer = new Tachometer(ui->dashFrame);
temperatureGauge = new TemperatureGauge(ui->dashFrame);
oilPressureGauge = new OilPressureGauge();
QVBoxLayout *vertOPG = new QVBoxLayout();
if ( oilPressureGauge->getTitle() != "" )
{
QLabel *OPGLabel = new QLabel(oilPressureGauge->getTitle());
vertOPG->addWidget(OPGLabel);
}
vertOPG->addWidget(oilPressureGauge);
speedometer = new Speedometer(ui->dashFrame);
horiz1->addWidget(tachometer);
horiz1->addWidget(speedometer);
horiz2->addWidget(fuelGauge);
horiz2->addWidget(temperatureGauge);
horiz2->addLayout(vertOPG);
horiz2->addWidget(tripometer);
horiz2->addWidget(odometer); // will NOT update when added to layout
QVBoxLayout *vert1 = new QVBoxLayout(ui->dashFrame);
vert1->addLayout(horiz1);
vert1->addLayout(horiz2);
this->ui->dashFrame->setLayout(vert1);
}
I am perplexed that the other Widgets will update as expected, but not the object named tachometer. As I said before, if I don't add it to the layout, e.g.
// ...
tachometer = new Tachometer(ui->dashFrame);
// ...
it appears to work just fine. Furthermore, by debugging on the console I see that the RPM value for the tachometer is being sent to the appropriate slot that updates the view for the tachometer (I just noticed the tachometer finally responded to the update, but it isn't updating after every timeout as it should). It seems like some latency issues are clearly involved here.
If anyone might have an idea as to why this might be, I would greatly appreciate some clarification.
HI..
i want to add elements dynamically to listview in QT for symbian OS, i have set of delegate methods associated with listview.
if i add elements statically, the control comes to delegate methods, and view is perfect.
but if i add dynamically, control is not at all coming to delegate methods.
i don't no how to do it. ill place here some sample code, that how i am adding elements.
this is how i am setting the view,
MylistView = new QListView();
QDesktopWidget* desktopWidget = QApplication::desktop();
QRect clientRect = desktopWidget->geometry();
MylistView->setMinimumSize(QSize(clientRect.width()-7,clientRect.height()-1));
MylistView->setViewMode(QListView::ListMode);
MylistView->setMovement(QListView::Free);
MylistView->setItemDelegate(new ItemDeligate(MylistView));
MylistView->setSelectionMode(QAbstractItemView::SingleSelection);
bool val =GreenPixmap.load(":/new/prefix1/temp/test.png");
ListModel = new QStandardItemModel();
ListModel->appendColumn(ItemList);
MylistView->setModel(ListModel);
Listlayout.addWidget(MylistView);
Listlayout.addWidget(MylistView);
this->setLayout(&Listlayout);
AddItemMenu = new QAction("Add",this);
menuBar()->addAction(AddItemMenu);
val = connect(AddItemMenu,SIGNAL(triggered()),this,SLOT(addItem()));
This is how i am adding dynamically when the click event occurs, (i.e dynamically adding items)
QStandardItem *Items = new QStandardItem(QIcon(GreenPixmap),"Avatar");
Items->setData("WAKE UP",ItemDeligate::SubTextRole);
ItemList.append(Items);
ListModel->appendColumn(ItemList);
please suggest me, what mistake i am doing in adding elemetns
I just made this quick example in my app, it's working, maybe it will gibe you an hint :
QStandardItem* Items = new QStandardItem("Avatar");
QStandardItemModel* ListModel = new QStandardItemModel();
ListModel->appendRow(Items);
listView->setModel(ListModel);
In summary, you should simply append a row on your model ! It should fix your problem !
If I missed something, let me know !