How to add a UI QWidget to a UI QWidget? - qt

Completely using QT Designer. I've created a QWidget to encapsulate a collection of controls. I now want to use this QWidget several times in the QMainWindow form.
How do I 'put' my QWidget onto the QMainWindow using QT Designer only?
Shouldn't this just be a simple drag and drop operation? What am I missing?

You can promote widgets.
Simply drag and drop QWidget into your mainwindow with QtDesigner.
Right click on it and press promote to In the dialog fill in your created widget's Class name (header should be filled in automatically if it doesn't match edit accordingly) press add and promote.
Now build and run your application and you should see your widget with collection of controls.

Related

Can a QToolbar be added to a QDockWidget?

I have setup my app to have various dock windows within the main window. I am also able to add a toolbar to the main window. However, I would ideally like to add the QToolBar inside one of the QDockWindow instances (or the QWidget that it houses) as the toolbar will be specific to that window.
Is this possible? I'm using a recent version of Qt, 5.10.
I think it is possible.
1.QDockWidget can set a QMainWindow by setWidget() method.
QMainWindow is made for just a mainwindow but it is not prevented from being used as a subwidget.
2.QToolBar can be attached to the main-subwindow by addToolBar() method.
3.The subwidget-mainwindow can naturally have its own QToolbar.
If you don't want to use QMainWindow as the widget of its QDockWidget,you can attach the QToolBar as a child widget of QDockWidget. But The toolbar is not movable as QMainWindow's.
I think you want to add QToolBar and use it as QMainWindow.
So I recommend that you set a QMainWindow as the widget of QDockWidget.And you attach any widget you like to the mainwindow after that.

How do I add a QWidget designed in Qt Designer inside a designed QMainWindow?

I started developing an application, I designed a empty QMainWindow wit just the menu bar. and created two new QWidgets and designed the features of my application on each.
Here is the thing: How do I add and interchange the last two QWidgets inside my QMainWindow?, so QMainWindow will show one set of features of my application at once.
Perhaps you are looking for QStackedWidget
(from Qt: The QStackedWidget class provides a stack of widgets where only one widget is visible at a time.)
You can find it insde the Containers group
Add a StackedWidget into your MainWindow, then
Directly edit the widget at present page by Qt designer(use the upper-right arrows to switch between differetn widgets). Notice that
in StackedWidget you have to create another signal sender, ex: a
combobox to decide which widget should be shown), or
If your custom widget contains some customized and hand-crafted
funcationalites that Qt doesn't have, you might need the widget
promotion. (otherwise, skip this)

How to detect if focus shift from a widget inside a QWidget to one outside?

I'm programming in Python using Qt with PySide and I have custom QWidget defined in a file called editor.py which is inserted in my ui in windowUi.py using the promotion method in the Qt Designer.
The custom QWidget class defined in editor.py doesn't do much besides using Elixir to edit items in a sqlite3 database and importing a ui file (editor.ui). Inside editor.ui there are a couple of QLineEdits and QDateTime widgets.
This widget is originally hidden in the main window and showed when needed. So far so good, but the problem is that I cannot make it hide when not needed. I decided that the widget is not needed when the user clicks anywhere else in the main window that is not the editor widget imported, that is, focus shift from the QWidget.
I looked upon this question: QWidget focusOutEvent not received and realized that the QWidget is really not getting focus.
If I call setFocusPolicy(StrongFocus) on it then I can make it hide if, and only if, the user clicks on the QWidget background (not on any widget inside it) and then clicks outside.
The question is then, how can I make it such that when the user clicks outside of this widget, shifting focus from any QLineEdit or QDateTime that's inside it to something else, the QWidget then hides itself?
Doesn't QApplication:::focusChanged ( QWidget * old, QWidget * now ) do what you need? You can check if QWidget *now is one of your QLineEdits/QDateTime or not (f.e. by going up by the QObject::parent)
Simply connect to this signal before showing and disconnect after hiding.

How to use my treeview subclass in qt designer?

I have a class myTreeView which is a subclass of QTreeView, which I am using in other widget and doing layout manually. now I want to include myTreeView in the new widget using designer so that I can avoid layout code. any suggestions/reference, how to do this ?
Place a QTreeView into your layout in Qt Designer. Right click the QTreeView, click Promote to... add a New Promoted Class definition using the form at the bottom of the dialog.
i.e. specify the base class of your derived class as QTreeView, give the widget a name, and specify where Qt Design can find the header file for your derived class.
That should allow you, at a minimum, to place your widget on the form as you lay it out. It will most likely show up as a grey empty box (much like a QWidget) on the layout however when you compile and build a project using your .ui file your widget will appear.

How can I insert a widget into a mainwindow generated by Qt designer?

I have a Main window build with Qt Designer and I also have a widget built with Qt designer (both in a separate ui file). How can I instantiate my widget into my mainwindow at runtime?
The easiest way (using Designer) is to open your main window, drag a QWidget into it, and position/name the QWidget like you would your custom widget. Once that is done, right-click on the QWidget, and select Promote to.... A dialog will show up with the widgets it can be promoted to. At the bottom of that dialog, you can add a new widget for promotion. Type in the class name and include file information, and add that widget. Then select the entry in the list, and click the Promote button.
At the end of this process, you should be able to recompile, and your custom widget will be where you placed it in the main window.
Can't you use QMainWindow::setCentralWidget function?

Resources