Qt Designer dock widgets children acess - qt

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

Related

QWidget stays within parent QWidget

i'm quite new to Qt and i've a question.
I've got an application with multiple windows/QFrame. I'd like them to only exist within the mainwindow (it's also the parent gadget). When I move them, I want the to stay within the parent gadget.
Is is possible ?
If yes, how ?
I've been through the Qt doc and i've found nothing. I though maybe a simple option can do that. Or do I have to create a new Widget with customs mouse Event methods ?
Thx
If you want a Multiple Document Interface (MDI) GUI you can use the QMdiArea and QMdiSubWindow classes to implement this. Have a look at the detailed description section of QMdiArea for using it with a QMainWindow example, but it also works on any other widget as well.

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.

Qt: How to connect class to custom Qt Designer Widget

Maybe I'm thinking about this completely wrong...
I've created a new widget in Qt Creator with a Designer file (I picked the Widget template, which generated a source and header file for my custom widget class, and also a designer file).
I then designed the widget with the Designer. I can now create instances of this widget and it will show up in my app.
But it's not terribly useful because I don't know how to customize the widget at runtime.
Let's say all I've got in the widget is a Label and a Button. At runtime, how can I change the text of this label? I can't figure out how to connect the designer stuff to my actual class, and I can't find any documentation on how to do this. Am I missing something?
Thanks!
A few things:
In designer, each of your widgets (the QPushButton, and the QLabel in your case) has a name assigned to it. This name is the name of the variable that you can use in C++ to reference that widget and call functions on it.
Depending on how your custom widget was implemented, you will be able to reference these variables using one of two methods:
If your class inherits from Ui::MyCustomwidget, then your variables are simply member variables of your class and can be accessed at any time (myLabel->setText())
If you have a member variable (generally named ui, of type Ui::MyCustomWidget), then you can access your widgets using the ui object (ui->myLabel->setText())

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

how can I have more than a UI for a QMainWindow?

I would like to have a QMainWindow that can change it's look at runtime, i.e when a user clicks a button. Besides keeping references to different UI classes generated by the QtDesigner, is there another way of doing that? Maybe by storing each UI in a layout ? What do you think ?
How much of the main window do you want to change? You could use a QStackWidget any place you want to change things, and change the shown page of the widget when the button is pressed. This will be quick to change, but for large or complicated UIs it may be slightly slower at startup, since it will be creating the widgets for both UIs at the same time. (There are fairly easy ways to change this, also, but they do add complications to something that could be straightforward for most people.) Also, if both layouts should have the same data, just in different places, you have the additional overhead of keeping both sets of UIs up to date while the program is running.
I think I got it now.
You have a QMainWindow and when a certain event is triggered you want to change the appearance of that particular window, like remove some buttons, add a treeview widget or what not.
Well the straight forward approach would be to do it manually, remove some widgets and add new ones using regular C++ code. This can be abit hard if you're used to Qt Designer.
The other way I can think of is using Qt Designer to generate the code for the other appearances and copy it to a special function. The code generated by Qt Designer is usually in a header file called "ui_classname.h" and isn't hard to understand. You will however need to remove some of it as not all would be necessary.
Also, instead of copying the generated code from Qt Designer you could just call it. Usually your widget has a pointer to the generated class and in your widget's constructor you see something like this:
MyWindow::MyWindow(QWidget *parent) : QMainWindow(parent), m_ui(new Ui::MyWindow)
{
m_ui->setupUi(this);
}
and in the corresponding header file:
class MyWindow : public QMainWindow {
...
private:
Ui::MyWindow *m_ui;
};
You could add the additional generated classes for the other appearances and use them when your event triggers.
It might look like this:
class MyWindow : public QMainWindow {
...
private:
void changeAppearance(int id);
Ui::MyWindow *m_ui;
Ui::MyWindowFirstAppearance *m_uiFirst;
Ui::MyWindowSecondAppearance *m_uiSecond;
...
};
void MyWindow::changeAppearance(int id)
{
// some code to remove the current appearance, basically the opposite of what setupUi is doing
if (id == 0)
m_ui->setupUi(this);
else...
m_uiFirst->setupUi(this);
...
}
This has the benefit of using the generated classes directly, so every change you do in Qt Designer doesn't require a change to your main window. The problem is I'm not sure if it's legal to call setupUi more than once and in a place other than your widget's constructor, so you'll have to check that (by looking at what's happening in the setupUi function).
You can dynamically load UI layouts at runtime. Check Qt documentation for QUiLoader class.
This also enables you to upgrade UI layouts without modifying a single line of code.
What I do is to design two UIs under two different QFrames, put the two QFrames in a layout together in the QMainWindow, and then hide() and show() the correct QFrame that you want...
You can use a QTabWidget and hide its buttons. You can then design each GUI in a separate window of the tabWidget. It has the advantage over hiding frames that you won't clutter up your Qt Creator window.
If I'm getting this right, I think you might want to take a loot at style sheets. It allows you to have "skins" for your widgets, much like CSS.
If I didn't get this right, and what you're trying to do is generate .ui files on the fly and create widgets with those using QUiLoader, then you're probably going at this the wrong way since I can't think of a good reason a regular application would need that.

Resources