Important: Please read the Qt Code of Conduct -

Replacing QMacCocoaViewContainer with QWidget::createWindowContainer().

  • Hi guys. I'm trying to replace QMacCocoaViewContainer with QWidget::createWindowContainer().

    The code is:

    static inline void setupLayout(NSView *cocoaView, QWidget *parent) {
    QVBoxLayout *layout = new QVBoxLayout(parent);
    layout->addWidget(new QMacCocoaViewContainer(cocoaView, parent));

    I've tried QWidget::createWindowContainer(QWindow::fromWinId(parent->winId()), parent);

    The code is here:
    This is just a helper function that is used in The class is QSearchField that was written by Mike McQuaid, the class is different for macOS, written in objective C using Qocoa to have a native macOS look.

  • Lifetime Qt Champion


    What is happening ?
    What is the issue ?
    Which version Of Qt ?
    Which version of macOS ?

    And out of curiosity, why do you want to switch ?

  • I had to look into the source code of QMacCocoaViewContainer to find the answer here.

    The solution is: QWidget::createWindowContainer(QWindow::fromWinId(WId(cocoaView))
    Where cocoaView is the NSView.

    (6 questions to a simple question like this is very unhelpful, if you knew how to help with it you would have known the answer by looking at the code I shared).

  • Posting a piece of code without sufficient context (working environment, version of the software) and then being rude if someone asks is also unhelpful.

    Things change both in Qt and in the system, it is often hard to give correct answer without knowing the above.

  • This was, in fact, very helpful - I came here looking for how to replace QMacCocoaViewContainer which is no longer available in Qt 6.
    Or is there a way to bring it back in Qt 6.2?

    P. S. The method described here works in Qt 5.15 and 6.2, tested on macOS 11.6 and 12.0 The only downside is you can't inherit from the native widget, i. e. make it an instance of your subclass.

Log in to reply