QVideoWidget is new window after leaving fullscreen - c++

I want my video player to show the QVideoWidget fullscreen when double clicking. I have created a new class, inherited the QVideoWidget class and I then overwritten the mousDoubleClickEvent.
//Mouse event in new VideoWidget Class
void VideoWidget::mouseDoubleClickEvent(QMouseEvent *event)
{
if( isFullScreen() )
showNormal();
else
setFullScreen( true ); //Show in fullscreen
}
The VideoWidget is used in my main window where (later) all other widgets are placed.
void MainWindow::setupUi()
{
QWidget* centralWidget = new QWidget( this );
QHBoxLayout* centralLayout = new QHBoxLayout( centralWidget );
videoWidget = new VideoWidget( this );
setCentralWidget( centralWidget );
centralLayout->addWidget( videoWidget );
}
The problem is now that whenver I enter the full screen mode by double click and exit again by double click, the video widget is no longer in the MainWindow. It is a new window. How do I place it back to its old position again?
Edit:
The videoWidget seems to be in a new window AND in my centralLayout. But when I close the new window it disappears in my centralLayout too.

I think video widget is being detached from main window when toggling fullscreen. Maybe you should try to re-add it to layout manually after returning to normal mode.

Related

Why is QSlider not updating it's position immediately in its UI?

I am creating a video application.
Upon starting this app, you would see a VideoWidget looping a playlist along with other widgets in the screen. By clicking the VideoWidget, the VideoWidget will go to fullscreen mode and a volume slider will be laid over it. It would go to show normal if clicked again in fullscreen mode.
To do this I created 2 classes. First, I created a main class that would contain all widgets including the Video widget. Second, I created a custom VideoWidget class. I instatiated my Qslider in this VideoWidget class and I instantiated a VideoWidget Object in my main class whose object is instantiated in main.cpp.
I got what I expect it to do. Except that the slider would not update its position immediately. It would only update position if you click to show normal then click to go back fullscreen. The volume change but the position of slider in UI does not change while in fullscreen.
I would like to ask what am I doing wrong? What should I do so that the slider position would update in UI?
Code Snippet:
in VideoWidget.h
class VideoWidget : public QVideoWidget
{
Q_OBJECT
QVideoWidget* videoWidget;
QMediaPlaylist* playlist;
QMediaPlayer *player;
public:
VideoWidget();
QSlider* slider;
};
In VideoWidget.cpp
VideoWidget::VideoWidget()
: videoWidget(new QVideoWidget(this)),
slider(new QSlider(Qt::Horizontal, this))
{
/*QMediaplaylist *playlist, QMediaPlayer *player instantiated here*/
slider->hide();
slider->setGeometry(300,735,600,20);
slider->setRange(0, 100);
slider->setValue(player->volume());
connect(slider, &QSlider::valueChanged, player, &QMediaPlayer::setVolume);
}
void VideoWidget::changeEvent(QEvent *event)
{
if(event->type() == QEvent::WindowStateChange)
slider->setVisible(windowState() == Qt::WindowFullScreen);
QWidget::changeEvent(event);
}
enter code here
void VideoWidget::resizeEvent(QResizeEvent* event) {
videoWidget->resize(size());
event->accept();
}
void VideoWidget::mousePressEvent(QMouseEvent *event)
{
this->setFullScreen(!isFullScreen());
event->accept();
}
In the MainWidget.cpp
mainwidget::mainwidget(QWidget *parent)
: QWidget(parent)
{
videoWidget = new VideoWidget(); // the video container
videoWidget->setFixedSize(500, 300);
QBoxLayout *displayLayout = new QHBoxLayout;
displayLayout->addWidget(videoWidget, 2);
QBoxLayout *layout = new QVBoxLayout;
layout->addLayout(displayLayout);
setLayout(layout);
videoWidget->setGeometry(100,100,300,400);
videoWidget->show();
}
edit:
This is the app at startup playing a video of my hand.
When I click the Video,
The video sets to fullscreen and the slider appears. The slider can control the volume of mediaplayer but the problem is, it won't move when dragged.
You're very confusing in ways you describe what the problem is, but if I get you right that QSlider doesn't track the mouse but you can change volume with it, presumably with a click?
You had connected valueChangedsignal , which is emitted only after sliderReleased() if tracking property is false. You have to handle Pressed\Moved\Released group of signals if you want to adjust volume continuously, or you can use built-in function of QSlider (which usually is enough):
slider->setTracking(true);

Qt window bugs with OpenGL

I have created a Qt interface to visualise a 3D model (point cloud) and an image. The issue is that when I switch to full screen, (using keystroke F1 for example), I do get my fullscreen mode as expected. But When I switch to normal mode, the 3D model disappears in the widget as shown in the bottom figure.
QGLWidget is inserted like this in QMainWindow:
QMainWindow : setCentralWidget(viewerWidget);
Our 3D Engine Class,OpenGLViewer inherits QGLWidget
the function for toggle full window goes like this:
void OpenGLViewer::toggleFullWindow()
{
if (isFullScreen()) {
setWindowFlags(Qt::Widget);
showNormal();
}
else {
setWindowFlags(Qt::Window);
showFullScreen();
}
}
My ui widget goes like this
QLabel* App::createViewerBox()
{
viewerWidget = dynamic_cast<QWidget*>(viewer);
QHBoxLayout *layoutHor = new QHBoxLayout();
QLabel *frame = new QLabel();
layoutHor->addWidget(viewerWidget,Qt::AlignJustify);
frame->setLayout(layoutHor);
return frame;
}

QScrollArea - scrollbar for a widget with many childs

So, I got some problems with QScrollArea.
I want to put a widget with many children in a QScrollArea, but I just dont get any scrollbars.
Heres the code:
QDialog *dialog = new QDialog();
QVBoxLayout *dialoglayout = new QVBoxLayout( dialog );
QScrollArea *area = new QScrollArea();
dialoglayout->setMargin( 0 );
dialoglayout->addWidget( area );
area->setAlignment( Qt::AlignCenter );
area->setAlignment( Qt::AlignTop );
area->setWidgetResizable( true );
// mainwidget has a lot of children
QWidget *mainwidget = randomclass.getWidget();
QVBoxLayout *mainwidgetlayout = new QVBoxLayout( mainwidget );
dialog->setWindowFlags( Qt::Window );
area->setWidget( mainwidget );
dialog->showMaximized();
if the mainwidget is bigger than the scrollarea, the content just overflows.
Can anyone help me?
Thanks in advance.
Some widgets don't report the area to scroll and that causes the confusion with scroll area. For QScrollarea object to adjust to the content:
myWidget->setMinimumSize(myWidget->sizeHint()); // assume the min size
scrollArea->setWidget( myWidget ); // use that widget in scroll area
Specific scrollers also might need to be enabled depending on the content:
scrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
scrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded);

Qt mouseMoveEvent not firing when mouse pressed

I have a custom QGraphicsScene in which I have a mouseMoveEvent(QGraphicsSceneMouseEvent *event);
When I hover on the scene with the mouse, the mouseMoveEvent gets properly fired. However when I hover with a mouse button pressed, then it does not get fired anymore.
This is how I setup the whole scene in the main window:
scene = new NodeScene(this); -> My Custom QGraphicsScene class
scene->setSceneRect(QRectF(0, 0, 5000, 5000));
QHBoxLayout *layout = new QHBoxLayout;
view = new QGraphicsView(scene);
layout->addWidget(view);
view->setDragMode(QGraphicsView::RubberBandDrag);
view->setMouseTracking(true);
QWidget *widget = new QWidget;
widget->setLayout(layout);
setCentralWidget(widget);
scene->setCentralWidget(widget);
And here is the code where I do handle mouse events (it's for Maya execution):
void NodeScene::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
MGlobal::displayInfo("Move");
QGraphicsScene::mouseMoveEvent(event);
}
void NodeScene::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
MGlobal::displayInfo("Press");
QGraphicsScene::mousePressEvent(event);
}
void NodeScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
MGlobal::displayInfo("Release");
QGraphicsScene::mouseReleaseEvent(event);
}
Any idea how I can get the mouseMoveEvents even when a mouse button is pressed ?
It sounds like you're not implementing all of the mouse events, just the mouseMoveEvent.
When overriding a mouse event, you should handle all of them (move, press and release events).
You can then set a boolean in the press event to know whether or not the mouse button is held down when entering the mouseMove event.
Found the issue by comparing my code to other examples, and it is due to the setDragMode(QGraphicsView::RubberBandDrag);
line. The code should by default be on QGraphicsView::NoDrag and the RubberBandDrag be enabled only upon press.

Qt C++ Display new window centered on old window

I have QWidget with button. When button is pressed, show new smaller window (Qwidget too). I want then new window is centered horizontal and veritcal on main window. Code which display new window is:
QWidget *wdg = new QWidget;
QPushButton *closeBtn = new QPushButton("Close");
QHBoxLayout *layout = new QHBoxLayout;
layout->addWidget(closeBtn);
wdg->setLayout(layout);
wdg->show();
wdg->resize(400,200);
Use the move slot. For example:
QPoint centerPoint = oldWidget->geometry()->center();
newWidget->adjustSize();
newWidget->move(centerPoint.x() - newWidget->width()/2, centerPoint.y() - newWidget->height()/2);
You may consider using frameGeometry() instead of geometry().
http://qt-project.org/doc/qt-5/application-windows.html#window-geometry
Hope that helps.