How to resize main window when bottom element's height change
-
I'm replying after a while because I tryed a lot of stuff since, and I don't want to let this topic falling unanswered in the abyss of the forum. The best solution I've came up with so far is using the resize function on both the main window and the control bar.
At the end of the post there is a working code of a fake video displayed with a control bar under it when the mouse hover the "video" for those interested. I've added an eventfilter to resize the "video" every time the main window is resized as this is how work my video (it made thing a bit harder)
This version is working by using setMaximumHeight() on the control bar, it works because when the window is resized either the "video" takes more space or the control bar takes more space, qt choose to expand the bottom one. For more consistency, it's also possible to use setFixedHeight() on the controlBar but you can"t animate it as easily.
main.cpp
#include "mainwindow.hpp" #include <QApplication> int main(int argc, char *argv[]) { QApplication a(argc, argv); MainWindow w; w.show(); return a.exec(); }
mainwindow.hpp
#ifndef MAINWINDOW_HPP #define MAINWINDOW_HPP #include <QMainWindow> #include <QtQml> #include <QtQuickWidgets/QQuickWidget> #include <QHBoxLayout> #include <QLabel> #include <QTimer> class MainWindow : public QMainWindow { Q_OBJECT public: MainWindow(QWidget *parent = 0); bool eventFilter(QObject *obj, QEvent *event); ~MainWindow(); private: QPixmap *fakecontent; // An image that acts as the video QLabel *video; // The QWidget containing the "video" QQuickWidget *controlbar; // The control bar in QML QVBoxLayout *vlayout; // A layout for the video and the control bar QWidget *mainWidget; // The main widget QTimer *timer; // A timer for animation bool b_isexpanded; // Is the control bar expanded QPropertyAnimation *animation; // Animation when expanding control bar public slots : void collapseControlBar (); void expandControlBar(); }; #endif // MAINWINDOW_HPP
mainwindow.cpp
#include "mainwindow.hpp" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) { setContentsMargins(0, 0, 0, 0); fakecontent = new QPixmap(); fakecontent->load( "a/path/to/cute/kitten.jpg" ); video = new QLabel(); video->setPixmap( *fakecontent ); b_isexpanded = false; controlbar = new QQuickWidget(); controlbar->setSource( QUrl( QStringLiteral( "ControlBar.qml" )) ); controlbar->setResizeMode(QQuickWidget::SizeRootObjectToView); controlbar->setMaximumHeight( 0 ); vlayout = new QVBoxLayout( ); vlayout->addWidget(video); vlayout->addWidget(controlbar); mainWidget = new QWidget(); mainWidget->setLayout(vlayout); setCentralWidget(mainWidget); qApp->installEventFilter(this); timer = new QTimer(); timer->setInterval(5000); connect( timer, SIGNAL(timeout()), this, SLOT(collapseControlBar()) ); animation = new QPropertyAnimation(this, "size"); animation->setDuration(300); } MainWindow::~MainWindow() { delete fakecontent; delete video; delete controlbar; delete vlayout; delete mainWidget; delete timer; } bool MainWindow::eventFilter(QObject *obj, QEvent *event) { if (obj == this && event->type() == QEvent::Resize) { video->setPixmap( fakecontent->scaled(video->size()) ); } else if (obj == video && event->type() == QEvent::MouseMove) { expandControlBar(); } return false; } void MainWindow::collapseControlBar() { this->resize(this->size().width(), video->size().height() + 20); b_isexpanded = false; controlbar->setMaximumHeight( 0 ); } void MainWindow::expandControlBar() { if (b_isexpanded) { timer->stop(); timer->start(); } else { animation->setStartValue( this->size() ); animation->setEndValue( QSize( this->size().width(), video->size().height() + 120 ) ); animation->start(); timer->start(); b_isexpanded = true; controlbar->setMaximumHeight( 100 ); } }
ControlBar.qml
import QtQuick 2.0 Item { Rectangle { anchors.fill: parent color: "#00FF00" } }
@MoaMoaK
Hi,
I was trying to run the code provided by you but i'm facing an error with this header file
**#include <QtQuickWidgets/QQuickWidget>" /home/ashutosh/Resize/mainwindow.hpp:6: error: QtQuickWidgets/QQuickWidget: No such file or directory
#include <QtQuickWidgets/QQuickWidget>"I'm not able tp locate this header file i q libraries. Do we need to install any other plugins or something else.
Thanks.
-
@MoaMoaK
Hi,
I was trying to run the code provided by you but i'm facing an error with this header file
**#include <QtQuickWidgets/QQuickWidget>" /home/ashutosh/Resize/mainwindow.hpp:6: error: QtQuickWidgets/QQuickWidget: No such file or directory
#include <QtQuickWidgets/QQuickWidget>"I'm not able tp locate this header file i q libraries. Do we need to install any other plugins or something else.
Thanks.
@Ashutosh_Sachdeva What Qt version do you use?
Also you should just include QQuickWidget:#include <QQuickWidget>
-
@jsulm
i'm using QT 5.12.1.I tried including only <QQuickWidget> but it's showing the same error
-
@jsulm
i'm using QT 5.12.1.I tried including only <QQuickWidget> but it's showing the same error
@Ashutosh_Sachdeva How did you install Qt?
-
@Ashutosh_Sachdeva How did you install Qt?
@jsulm
https://wiki.qt.io/Install_Qt_5_on_UbuntuThrough this link
-
@jsulm
https://wiki.qt.io/Install_Qt_5_on_UbuntuThrough this link
@Ashutosh_Sachdeva So, you used offline installer? And you really installed Qt 5.12.1, not 5.7?
Was there anything to select during installation? -
Hi,
Sorry it's 5.13.1.
Yes i used the offline installer and there were many option and i selected the latest one.
-
Hi,
Sorry it's 5.13.1.
Yes i used the offline installer and there were many option and i selected the latest one.
@Ashutosh_Sachdeva Not sure, I don't use offline installer. My Qt setup was made via online installer and I have the headers.
-
@Ashutosh_Sachdeva Not sure, I don't use offline installer. My Qt setup was made via online installer and I have the headers.
@jsulm Ok
I have tried updating the lib. but that doesn't worked any other suggestion or solution?
-
@jsulm Ok
I have tried updating the lib. but that doesn't worked any other suggestion or solution?
@Ashutosh_Sachdeva You can try online installer to install Qt. My Qt setup installed using online installer has this header file.
Also as stated in your other thread make sure you haveQT += quickwidgets
in your pro file.