How to connect QPushButton to a promoted QWidget? - qt

I have a QWidget container that contains my custom widget. This QWidget container was promoted to the custom widget I created. I have a QPushButton on my MainWindow.ui. I want to connect this QPushButton to the promoted QWidget. How will I do this?
For example, I have a function on my promoted QWidget class, and I want to call this function when I click the QPushButton located at the MainWindow.ui.
Thanks in advance!

Have you considered using a connect??
connect the QPushButton singal clicked to that function (make sure its a slot where its declared).

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 to add a UI QWidget to a UI QWidget?

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.

Include button into QComboBox or QCompleter

How to include a QPushButton into popup created by QCompleter?
I would use QFrame with a Listbox or QTableView and QPushButton included. But method void QCompleter::setPopup(QAbstractItemView *popup) requires QAbstractItemView (not QWidget).
What is desired:
I never used a QCompleter, but all visual items in Qt are derived from QWidget.
If you check the doc for QAbstractItemView you see it is derived from QAbstractScrollArea -> QFrame -> QWidget, so it is a QWidget after all, but you have to use specialized form. The QAbstractItemView has some child classes you can use, like QListView or QColumnView. Check the Qt doc for more info: http://doc.qt.io/qt-5.8/qabstractitemview.html

How to embed QFontDialog and QColorDialog into QWidget?

I need to pop up Qwidget with some properties functions from QMainWindow. This QWidget has to embed QFontDialog and QColorDialog.
I want it pop everything with single click, not via i.e. QFontComboBox... Is it possible? Can these dialogs be embedded into QWidget
You can add a QMdiArea in your QMainWindow and add any dialog as a subwindow. You can make subwindow maximized and frameless if you want.
QMdiSubWindow* w = ui->mdiArea->addSubWindow(new QColorDialog());
w->setWindowFlags(Qt::FramelessWindowHint);
w->showMaximized();

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.

Resources