Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Getting a CWnd to use as a parent
Forum Updated to NodeBB v4.3 + New Features

Getting a CWnd to use as a parent

Scheduled Pinned Locked Moved Solved General and Desktop
2 Posts 1 Posters 303 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • PerdrixP Offline
    PerdrixP Offline
    Perdrix
    wrote on last edited by Perdrix
    #1

    I have an MFC dialog that I want to create as a child window (WS_CHILD). To do that I need a CWnd to be the parent.

    My immediate thought was this:

    winHost = new QWinHost(stackedWidget);
    winHost->setObjectName("winHost");
    stackedWidget->addWidget(winHost);
    	
    //
    // Attach the hwnd of the stacked widget to a CWnd that we use as the parent for the 
    // processing dialog
    //
    HWND hwnd{ reinterpret_cast<HWND>(stackedWidget->effectiveWinId()) };
    hostWnd.Attach(hwnd);
    
    auto result = processingDlg->Create(IDD_PROCESSING, &hostWnd);
    if (FALSE == result)
    {
    	int lastErr = GetLastError();
    	ZTRACE_RUNTIME("lastErr = %d", lastErr);
    }
    processingDlg->setParent(winHost);			// Provide a Qt object to be parent for any Qt Widgets this creates
    
    hwnd = processingDlg->GetSafeHwnd();
    ZASSERT(NULL != hwnd);
    winHost->setWindow(hwnd);
    

    where hostWnd is a CWnd that is a member variable of the class. However, the call to stackedWidget->effectiveWinId() returned a NULL HWND, so it all fell apart.

    So how can I get this to work please?

    I was working round the problem by defining the dialogue IDD_PROCESSING as WS_POPUP, but that has side effects (amongst others it gets created with a sizing border).

    Thanks,
    David

    PerdrixP 1 Reply Last reply
    0
    • PerdrixP Perdrix

      I have an MFC dialog that I want to create as a child window (WS_CHILD). To do that I need a CWnd to be the parent.

      My immediate thought was this:

      winHost = new QWinHost(stackedWidget);
      winHost->setObjectName("winHost");
      stackedWidget->addWidget(winHost);
      	
      //
      // Attach the hwnd of the stacked widget to a CWnd that we use as the parent for the 
      // processing dialog
      //
      HWND hwnd{ reinterpret_cast<HWND>(stackedWidget->effectiveWinId()) };
      hostWnd.Attach(hwnd);
      
      auto result = processingDlg->Create(IDD_PROCESSING, &hostWnd);
      if (FALSE == result)
      {
      	int lastErr = GetLastError();
      	ZTRACE_RUNTIME("lastErr = %d", lastErr);
      }
      processingDlg->setParent(winHost);			// Provide a Qt object to be parent for any Qt Widgets this creates
      
      hwnd = processingDlg->GetSafeHwnd();
      ZASSERT(NULL != hwnd);
      winHost->setWindow(hwnd);
      

      where hostWnd is a CWnd that is a member variable of the class. However, the call to stackedWidget->effectiveWinId() returned a NULL HWND, so it all fell apart.

      So how can I get this to work please?

      I was working round the problem by defining the dialogue IDD_PROCESSING as WS_POPUP, but that has side effects (amongst others it gets created with a sizing border).

      Thanks,
      David

      PerdrixP Offline
      PerdrixP Offline
      Perdrix
      wrote on last edited by
      #2

      I have resolved this problem.

      I moved all bar the first two lines of the code in question from the ctor of the QMainWindow to the showEvent() handler. The HWND for the stacked widget was valid at that point whereas it wasn't in the ctor.

      I also added a line to the dtor which read:

      	hostWnd.Detach();
      

      I hope someone finds this helpful.
      David

      1 Reply Last reply
      0
      • PerdrixP Perdrix has marked this topic as solved on

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved