Resizing Main Windows QML
-
Not sure I understand what you mean as resizing come for free with the Window element. You probably mean the resizing of the elements children of the window. If that what you meant, then using anchors in those elements will keep them proportional to the main window size.
Bidochon
-
I think i get what you want. I assume you are using the default MainWindow which is providet with a default widgets project.
You basically just have to access this in the sourcefile of your MainWindow class:
@this->setGeometry(const QRect &);@
That way you can set the properties of the MainWindow, in that case width, height, x and y. -
From where do you want to rescale the Window? I'll provide you a function which rescales the Window into a given size:
mainwindow.h
@#ifndef MAINWINDOW_H
#define MAINWINDOW_H#include <QMainWindow>
namespace Ui {
class MainWindow;
}class MainWindow : public QMainWindow
{
Q_OBJECTpublic: explicit MainWindow(QWidget *parent = 0); ~MainWindow(); void rescaleWindow(int w, int h, int x=0, int y=0); private: Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H@
mainwindow.cpp
@#include "mainwindow.h"
#include "ui_mainwindow.h"@MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}MainWindow::~MainWindow()
{
delete ui;
}void MainWindow::rescaleWindow(int w, int h, int x, int y)
{
this->setGeometry(x, y, w, h);
}@
For forther information please read:
"QWidget Geometry":http://qt-project.org/doc/qt-4.8/qwidget.html#geometry-prop -
I'm wanting to resize from QML, because I need to make a panel behave like a task manager to auto hide with animation, like this:
https://www.youtube.com/watch?v=m4futNHYPYI
Thanks for your attention.
-
No, this is just an example for you to understand what I do.
I will create a panel in QML (a Rectangle) and will increase it or decrease in effect, something like what happens there in the video with the Task Bar. He will occupy the all window, so I need to increase or decrease the main window because when I reduce or increase the Rectangle it does inside the window only and is not what I want, I want the window to behave interia like him, understand?
If you have another solution to my problem can also -
bq. No, this is just an example for you to understand what I do.
I will create a panel in QML (a Rectangle) and will increase it or decrease in effect, something like what happens there in the video with the Task Bar.bq. He will occupy the all window, so I need to increase or decrease the main window because when I reduce or increase the Rectangle it does inside the window only
Sorry if i might misunderstand you again but you want a Rectangle in your view which can reduce himself down or up, like in the video with the taskbar. And if it reduces itself down, it should scale the hole window or applicationwindow down, and if it scales up it should scale the hole window or applicationwindow up?
-
Oh alright, so you basically want the part with the resizing, you already got your part done with the clicking?
For the resizing part you'll still need a resize function in your mainwindow class. Let's keep the old one from above. In QML you'll just resize your top-level-component.
If it is for example a Rectangle:
@Rectangle {
id: topLevelComp
// more code ..
}@
We'll give it an id. So if we have to resize the window, like for example with the onClicked: notification of a MouseArea, we just execute:
@onClicked: {
topLevelComp.width = 0
topLevelComp.height = 0
}@
The QML-resize part is mostly done. We now need a signal in our QML so we can call a Cpp function. For that we just declare it that way:
@Rectangle {
id: topLevelComp
signal resizeMyApplication()
// more code ..
}@
Now we can emit it in our onClicked notification:
@onClicked: {
topLevelComp.width = 0
topLevelComp.height = 0
resizeMyApplication()
}@
In Cpp we have got our QML-ApplicationViewer. For that case i'll use a QQuickView. We have to get the rootObject() of our Viewer:
@QQuickView *view = new QQuickView();
// ... some code
QObject *obj = (QObject *)view->rootObject();@
Now we just have to connect the signal with a given slot or signal, for example:
@connect(obj, SIGNAL(resizeMyApplication()), this, SLOT(resizeMe()));@
You'll need the resizeMe() slot for that. In the slot you just execute the void MainWindow::rescaleWindow(int w, int h, int x, int y) function. You can also put the resize directly into the slot:
@{
this->setGeometry(x, y, w, h);
}@I hope that you'll understand what i mean and that i could help you. Feel free to ask for further informations or read this documentations:
"Qml Cpp Integration":http://qt-project.org/doc/qt-5.0/qtqml/qtqml-cppintegration-topic.html
"Interacting with QML Objects from Cpp ":http://qt-project.org/doc/qt-5.0/qtqml/qtqml-cppintegration-interactqmlfromcpp.html
"Thread on how to: invoke QML methods":http://qt-project.org/forums/viewthread/36154