Make the objects' size of a GUI adapt to window - qt

I created a GUI with Qt5 but I will like the objects to be always contained in the window. When I change the size of the window (with the mouse), the objects dont resize themselves and therefore are hidden by the window.
Basically, I will like my GUI to act like a Web Page for example.
I think I have to use the Layout properties to do that but I dont seem to find it in Qt (in the QBBoxLayout or QWidget ?). I will like to change this in Qt and not in my PyQt script if possible.

Have you set the layout of the window ?
Normally you should have a code similar to this one
dialog = QDialog()
verticalLayout = QVBoxLayout()
label1 = QLabel("first")
label2 = QLabel("second")
verticalLayout.addWidget(label1)
verticalLayout.addWidget(label2)
dialog.setLayout(verticalLayout)
if you are creating the gui writing the necessary the code yourself.
If you are creating the gui with the designer, probably you have not applied a layout to your window.

Related

Why does Qt3DWindow on rare instances not render a scene and instead projects other parts of the GUI onto the window?

This is a rare bug, but sometimes when I start my Qt app the qt3d window which hosts my scene is blank (without any of my models), except the window itself seems to be bugged because it will mirror other parts of the GUI if I shift between tabs. So instead of my scene appearing in the window it'll contain information and widgets from another tab that's located in the same area as the window on a different tab.
This doesn't occur very often, which makes the bug hard to replicate. Has anyone else seen this behavior before or maybe it's the way I'm setting up the Qt3DExtras::Qt3DWindow?
Qt3DExtras::Qt3DWindow* view = new Qt3DExtras::Qt3DWindow();
view->defaultFrameGraph()->setClearColor(QColor(0,0,0));
view->setRootEntity(CreateScene());
// embed 3D Window in Widget
QWidget* container = QWidget::createWindowContainer(view);
ui->verticalLayout_visualizer->addWidget(container);

QML Popups not working properly with C++ application

I have an application in Qt in which I can switch between c++ app and QML app (setting central widget to one or other).
The problem is that in order to work this, I had to change my main.qml from ApplicationWindow to Page and now Dialogs and all Popups in my QML app are not working properly (modality and focus is not working at all).
My code looks like that:
QQuickWidget *view = new QQuickWidget(this);
view->setSource(QUrl(QStringLiteral("qrc:/main.qml"))); //Page
MyCppApp *myCppApp = new MyCppApp (this); //QWidget
QStackedWidget *stackedWidget = new QStackedWidget;
stackedWidget->addWidget(view);
stackedWidget->addWidget(myCppApp);
stackedWidget->setCurrentIndex(1);
setCentralWidget(stackedWidget);
I know in Popup's documentation there is that "It can be used with Window or ApplicationWindow." but is there a way to get Popups in my QML work properly?
I am using Qt 5.8 and Qt Quick Controls 2.
For the modality you should not make the dialog window part of central widget in main window (otherwise it is modal relative to what?). Invoke it from main window. And to give it modal dialog looks and behavior you can apply window flags like that:
// this view is not a part of the app main window
view->setFlags(Qt::Dialog | Qt::WindowTitleHint |
Qt::WindowStaysOnTopHint | Qt::MSWindowsFixedSizeDialogHint);
Mind that Qt::MSWindowsFixedSizeDialogHint is of course applicable on Windows only. And for setting the focus:
// to set the edit focus such trick needed
QTimer::singleShot(0, view, SLOT(setFocus()));
I use that with QML widget container. Implementation details may differ. And no QML part visible. There you may need to take care of FocusScope then.

qt qwidgets that I create programmatically (such as Labels) never show, but ones that I create with the IDE work fine. I'm new to qt

I'm new to QT. when I create a qwidget such as a qlabel, programmatically, it never shows in my main window or anywhere else. But when I create one using the IDE it works fine. What am I missing ?
By default, widgets are set to invisible, therefore, you need to call .show() to make a widget visible. For example,
QLabel *label = new QLabel("click");
label->show();
Using the debugger I noticed that my QLabel had the wrong parent. Setting the parent correctly cured all of my problems. The other answers to my question were relevant too.

Drag and drop of QDockWidget between two or more windows

I would like to know if anybody knows if it is possible to drag a QDockWidget from one window to another.
I am developping an application that has a lot of windows. Each of these windows has a specific usage. I want to use QDockWidget because of the automatic rescaling when docking a widget in the main window. But I would also like to have the possibility to have a new dock area in a new window so that I can put together all the widgets related one to each other.
Does anyone have a suggestion?
As far as I know, you cannot drag a QDockWidget from one application to another if they are developed separately.
On the other hand, I think that you can reparent a QDockWidget to another QMainWindow, only if it's part of the same application.
...aaaaand you can always try to intercept the drap/drop events and pass information between the two windows with a protocol of your own: define a new mime type for the drag and define the syntax of the data associated to the QDockWidget. Then, implement the code to analyze the data and reconstruct the contents of the QDockWidget in the target window... not an easy task!

Qt Creator -- how do I set the model of a QTreeView inside the Designer?

I'm learning Qt independently, and this may seem like an easy question (because it is). I primarily come from a Swing background, so the concepts are very similar.
My question:
I am using Qt Designer to create a QTreeView item in the UI Designer. How do I do something as simple as setting the model of the TreeView?
Usually I would do something like:
QTreeView *tree = new QTreeView();
tree->setModel( &myModel);
I don't even know how to get a reference to the QTreeView object that the UI Designer created. Any direction will be appreciated!
Depending on your version of Qt, your main window will either include as a member or privately inherit a class with all of your designer widgets. So, within MainWindow, e.g. either my_widget->show(); or ui->my_widget->show(); to show the widget, respectively.
In your case, my_tree_view->setModel(my_model);

Resources