Suppress Specific Qt Warning
-
I'm trying to understand whether I can suppress the following warning:
QQuickWidget cannot be used as a native child widget. Consider setting Qt::AA_DontCreateNativeWidgetSiblings
Basically, this warning seems to come about because I'm using a
QQuickWidget
as a container widget for aQWidget
. As soon as Ihide
the childQWidget
then the warning disappears.I have tried setting the
AA_DontCreateNativeWidgetSiblings
attribute for the application but this makes not a jot of difference and the warning still remains. Each time a mouse/paint/other event occurs, this warning is displayed and makes it difficult to see any other warnings, etc.Any help would be greatly appreciated.
-
@webzoid said in Suppress Specific Qt Warning:
AA_DontCreateNativeWidgetSiblings
This post http://lists.qt-project.org/pipermail/interest/2015-June/017391.html seems to say you need an extra flag. Having said that, the reply http://lists.qt-project.org/pipermail/interest/2015-June/017392.html implies it still didn't work for him, but it might be worth a try....
Also, https://git.merproject.org/mer-core/qtdeclarative/commit/c9553ad7d8041c7b0fd769dc8cd0f5335d9408a3 says explicitly:
Detect and warn when using QQuickWidget as a native child
This is not supported.though this seems to be the opposite way round from your:
Basically, this warning seems to come about because I'm using a QQuickWidget as a container widget for a QWidget.
-
@JNBarchan thanks for your comments.
Just to clarify, I have the following heirarchy on my
QMainWindow
:QWidget
>QQuickWidget
>QWidget
> otherQWidgets
The child
QWidget
of theQQuickWidget
fills the parent area entirely so that the contents of theQQuickWidget
cannot be seen (but still must be "visible" so that the QML is rendered). At regular intervals, theQQuickWidget
is grabbed and the resultingQPixmap
is then drawn onto the childQWidget
(along with some other stuff, which uses thewinId
of theQWidget
to enable a 3rd party COM object to draw).Complex, I know but it works.
Looking at the
Qt::WA_DontCreateNativeAncestors
, would this attribute need to be set in theQWidget
child of theQQuickWidget
? -
@webzoid
I have no idea, since I have never usedQQuick
anything :) You can doubtless experiment.However, you now say you have:
QWidget > QQuickWidget > QWidget > other QWidgets
So that means you are using "QQuickWidget as a native child", aren't you? Which the git project explicitly states is "not supported" and put in the error message you see for. So I don't know whether your code works or not, but it seems the warning message is there deliberately for this case....
-
@JNBarchan Yes, the
QQuickWidget
is a "native child" of the parentQWidget
AND the project works perfectly, no issues at all other than this warning.What is odd though, is that if this warning is to do with the
QQuickWidget
being a native child, then its strange that the warning disappear when I hide the childQWidget
- theQQuickWidget
isn't hidden and its parent hasn't changed. -
@kshegunov There's a LOT of source code and its not going to be easy picking it apart to get the relevant bits. I have, however, re-created the problem in a separate
QWidgets
application:Steps to re-create:
- Create a new QWidgets application
- Add
qml quick quickwidgets
to the .pro file - Add a new class called
MyQuickWidget
and derive it fromQQuickWidget
- Add a new class called
MyWidget
and derive it fromQWidget
- In the
MainWindow
designer, add aWidget
to thecentralWidget
and change the layout of thecentralWidget
to grid. Remove all margins and promote this widget toMyQuickWidget
- Drop a
Widget
on top of the new widget from above. Change the layout of theMyQuickWidget
to grid and again, remove all padding. Promote this newWidget
toMyWidget
- Add a QML file to the project called
MyQml.qml
- Add a resource file to the project called
MyResources
and addMyQml.qml
to the resources - Time to add some code:
In the
MyQuickWidget
add the following into the constructor aftersetupUI
:setSource(QUrl("qrc:/MyQml.qml");
In the
MyWidget
header file, add an override to thepaintEvent
and include any necessary header files. Implement thispaintEvent
function in the cpp file as follows:void MyWidget::paintEvent(QPaintEvent *event) { QPainter p(this); // Get the HWND (this is needed for the COM object of my application) WId hWnd = this->winId(); p.fillRect(event->rect(), QColor("red")); }
Finally, add the following to the
MyQml.qml
file:import QtQuick 2.0 Rectangle { id: rect color: "blue" width: 240 height: 240 }
Now if you run
qmake
and then debug the application, the warning from the original post starts to appear. Resizing the window or forcing a repaint generates hundreds of warnings.Adding the following attribute into
main.cpp
also makes no difference:QApplication a(argc, argv); a.setAttribute(Qt::AA_DontCreateNativeWidgetSiblings);
But obviously, commenting out the following line from
MyWidget.cpp
solves the problem://WId hWnd = this->winId();
However, my application NEEDS the
hwnd
of the widget in order to paint the 3rd party control as well as the QML in the background in order to pull in and paint the QML-only stuff.EDIT: If you change the margins of the
MyQuickWidget
on the form, you will see that the QML rendering is "broken" and a black window is show behind theMyWidget
widget. I can still successfullygrab
theMyQuickWidget
as it still renders correctly to its framebuffer. -
@webzoid said in Suppress Specific Qt Warning:
// Get the HWND (this is needed for the COM object of my application) WId hWnd = this->winId();
This will create a native handle for that widget (if not done already) and will thus generate your warning.
Qt::WA_DontCreateNativeAncestors
doesn't have any effect in this case, as you explicitly require the widget to be a native one. Consider using QQuickRenderControl for your task instead.A note of caution about this:
You can ignore the warning and it may work fine now, but there's no guarantee that a future version of Qt won't break this code. Warnings reported from Qt should be considered bugs in the user code and ought to be fixed.