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

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

Related

Use condition in the Qt Creator designer

I'm pretty new to Qt, and I can't find an answer to very basic question:
is it possible to use conditions when using the ui designer?
for example, that if a var x is set to true widget 1 will be displayed, otherwise widget b will be displayed in the same area?
I know how to do it in the code but wondered if it possible using the design tool...
In my experience there is not a way to do it using the designer in a QWidgets application, though I have to admit I never really tried to do that. In a Qt Quick QML application you can probably use a binding on the visible property to hide/show the desired item.

qt equivalent for docking control to it's container in winforms

I'm trying out qt creator lately and i hate reading hours of docs and tutorials just for doing simple tasks.
So, in winforms i can drag & drop a control from toolbox and set the dock property so it automatically maximizes itself to it's container's size.
What is the equivalent of this behaviour in qt?
I googled this and found it strange that nobody wondered the same before.
There are differences between the concepts of WinForms and Qt. You need to study the concept of layouts. Take a look at the relevant part of the documentation of Qt Designer: Using Layouts in Qt Designer
Before a form can be used, the objects on the form need to be placed
into layouts. This ensures that the objects will be displayed properly
when the form is previewed or used in an application. Placing objects
in a layout also ensures that they will be resized correctly when the
form is resized.
Qt uses layouts. Add QHBoxLayout or QVBoxLayout to the parent widget.

Qt Designer dock widgets children acess

I made a form using the Qt Designer which has some dockwidgets, these dockwidgets have some children widgets. How I can access the dockwidget and these child widgets in my mainwindow.cpp?
I highly recommend reading the docs for these kinds of things, but to give you a little head start, QDockWidget inherits from QWidget, which inherits from QObject:
https://doc.qt.io/qt-4.8/qobject.html#children
widget->children() would simply tell you the children of this widget. This would be needed if you didn't already know the names of the objects to be accessed directly, or had no reference to them.
Update
When you create objects in Qt Designer, and you run the setupUi(this) that is generated for you, inside of your MainWindow, you will then have access to all of the widgets you had set up as members. You can access them directly as they were named in Qt Designer. Please check out one of the numerous tutorials on getting started with Qt. Here is one that shows you how to make use of your ui file, and access the members from it: http://sector.ynet.sk/qt4-tutorial/my-first-qt-gui-application.html
You can also get a list of all the dockWidgets from the mainwindow with
QList<QDockWidget *> dockWidgets = findChildren<QDockWidget *>();
A similar technique works for getting toolbars etc. so you don't have to manually store a list as you create them

How to use subclassed class in qt

I've subclassed qt's pushbutton and now I would like to use it in my project (instead of qpushbutton) - the problem I have is that even if I add #include "mybutton.h" in ui_class it gets overwritten and I don't know what else can I do it.
Is there a way to have this button within designer on a panel just like the ordinary qpushbutton is?
Never modify the ui_Class file yourself. Any change you make there will be overwritten when the .ui gets compiled. Instead use the promote to functionality within QtDesigner.
If some forms must be designed, but certain custom widgets are
unavailble to the designer, we can substitute similar widgets to
represent the missing widgets. For example, we might represent
instances of a custom push button class, MyPushButton, with instances
of QPushButton and promote these to MyPushButton so that uic generates
suitable code for this missing class.

OpenGL rendering to a QML item

I have a QML file which contains a layout of QML items and now I want one of those items to be a QGLWidget. i.e. I want to render to a specific QML item.
Is anyone aware of how to do this?
The simplest way I suppose it to provide QML a new custom component implemented in C++. I couldn't find anything ready.
You could subclass the QDeclarativeItem and implement your OpenGL code in the paint function after using the QPainter::beginNative() function. After that you can "export" your new custom item to QML this way. This is quite simple and should work, but you'll have to setup the viewport of you QDeclarativeView to be a QGLWidget, something like this:
QDeclarativeView view;
// This is needed because OpenGL viewport doesn't support partial updates.
view.setViewportUpdateMode(QGraphicsView::FullViewportUpdateMode);
view.setViewport(new QGLWidget);
or you'll have to use the opengl graphics system for the entire application.
Another way is using QML/3D.
This thread will give you some other information.

Resources