Rendering QML over another window using QWidget - qt

I am currently using QWidget to embed a window from another library (Panda3D). I've been trying to see if it was possible to add a QML-based interface to Panda3D by rendering a QQuickView over the QWidget containing Panda3D's window.
When I don't initialize Panda3D, I can see my QQuickView being properly rendered over the widget: but when I do reparent Panda3D's window to the widget, then the window is rendered over the QQuickView.
I'd like to know if what I'm trying to do is possible, and if yes, how would I do such a thing ?

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.

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.

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 set QFileDialog's parent to a QQuickView

The only window in my application is a QQuickView which is a subclass of QWindow, not of QWidget, so if I want to use a QFileDialog to give the user the option to save a file, I do not know how to set its parent to be the QQuickView. I know that the QFileDialog works without a parent, but if it does not have a parent, it is not centered over the QQuickView. This is not a big problem on its own, but on my OS (Ubuntu 13.10) a QFileDialog without a parent sometimes appears under the QQuickView window and the user sees only a “flashing” taskbar icon.
I use Qt 5.2. I initially tried to use the FileDialog type in the Qt Quick Dialogs module but it does not have some features I need (such as the defaultSuffix property).
Right, this is a serious problem of QML. You cannot utilize the QFileDialog because you don't have a QWidget app and FileDialog lacks some critical features.
See: FileDialog in QTQuick (QML): Save file under given name
At the moment we are using code from deep down in the Qt source code, which is not in the public API. It only requires a Window instead of a Widget to do modality right.
Maybe I can write a blog post about that over Christmas.

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