Qt 6 record video from QMediaPlayer - qt

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();

Related

How to add QML map to QGraphicscene?

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

How to Record or Clip Video from a video file Using Qt?

I am using visual studio 2010 integrated with Qt5.0.1.
I want to record or clip a video from a video file.
I used QMediarecorder but cannot able to record the video. Below is the snippet I am using.
Any solution will be helpful.
QMediaPlayer mediaPlayer;
mediaPlayer.setmedia("c:\\sample.avi");
QVideoWidget videoWidget;
mediaPlayer.setVideoOutput(&videoWidget);
mediaPlayer.play();
QMediaRecorder *mediaRecorder = new QMediaRecorder(&mediaPlayer);
QVideoEncoderSettings *videosettings = new QVideoEncoderSettings;
videosettings->setResolution(1280,720);
videosettings->setQuality(QMultimedia::VeryHighQuality);
videosettings->setFrameRate(25.0);
videosettings->setCodec("H.264Video");
mediaRecorder->setVideoSettings(settings);
QString file = "C:\\record.avi";
QFile file1(file);
file1.open(QIODevice::WriteOnly);
mediaRecorder->setContainerFormat("avi");
mediaRecorder->setOutputLocation(QUrl::fromLocalFile(file));
mediaRecorder->record();
after some seconds in another function i will call
mediaRecorder->stop();
When you use mediaRecorder->setVideoSettings, set it to videosettings. You don't have a variable settings.

Qt QTabWidget - auto set tab name number

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.

QCompleter and QListWidget as custom popup issue

I have a QCompleter using a QStringListModel for my QPlainTextEdit (check this example):
QStringListModel* model = new QStringListModel(names);
QCompleter* completer = new QCompleter(model);
completer->setCompletionMode(QCompleter::PopupCompletion);
completer->setModelSorting(QCompleter::UnsortedModel);
It works fine. Now I need some Icon, Tooltips for each suggestion I'm trying to use a QListWidget as custom popup:
QListWidget* w = new QListWidget();
foreach(name, names) {
QListWidgetItem* i = new QListWidgetItem(name);
i->setIcon(/*my Icon*/);
i->setToolTip("");
w->addItem(i);
}
completer->setPopup(w);
The popup ok, just like I need, but the completion no more work. I cannot type the text to make it filter the suggestion, just Up/Down key.
I have try:
completer->setModel(w->model());
but no help!
What is my misstake or just QStringListModel give me the ability to filter the suggestions? What do you suggest?
Thanks you!
I mostly deal with PyQt, but same deal. My syntax may be off, but you should use a QStandardItemModel vs. a QStringListModel. From there, you can leave it as the standard popup (QListView)
Something like:
QStandardItemModel* model = new QStandardItemModel();
// initialize the model
int rows = names.count(); // assuming this is a QStringList
model->setRowCount(rows);
model->setColumnCount(1);
// load the items
int row = 0;
foreach(name, names) {
QStandardItem* item = new QStandardItem(name);
item->setIcon(QIcon(":some/icon.png");
item->setToolTip("some tool tip");
model->setItem(row, 0, item);
row++;
}
completer->setModel(model);
completer->popup()->setModel(model); // may or may not be needed

Qt: view added to layout not updating, but will update when not in layout

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.

Resources