Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Installation and Deployment
  4. CWND to QWidget*
Forum Updated to NodeBB v4.3 + New Features

CWND to QWidget*

Scheduled Pinned Locked Moved Unsolved Installation and Deployment
9 Posts 4 Posters 5.4k 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.
  • MassiM Offline
    MassiM Offline
    Massi
    wrote on last edited by
    #1

    Good day,

    I'm using MFC to build a GUI on vs2012 and I recently integrated Qt5.4.1 library in my project. Does anyone know how to connect an CWND pointer to QWidget pointer? I can get the HWND from a widget using winId() but how to get a widget from a HWND or a CWND?

    CWnd* pMfc to the argument of myQtfunction(QWidget* pwidget)

    Thanks in advance!

    Massi

    Software Design Engineer at Ford - Canada

    1 Reply Last reply
    0
    • JKSHJ Offline
      JKSHJ Offline
      JKSH
      Moderators
      wrote on last edited by JKSH
      #2

      Hello @Massi,

      A QWidget is quite a complex class; many of its internals are managed by QApplication. Therefore, you cannot convert a CWND/HWND window directly to a QWidget, because those are managed outside of QApplication.

      Now, it is possible to create a QWindow object to manipulate external windows. It is also possible to embed a QWindow inside a QWidget. You can kind of combine them like this:

      WId externalWindow = (WId)FindWindow(NULL, L"Untitled - Notepad"); // Get HWND by window title
      QWindow *myWindow = QWindow::fromWinId(externalWindow);
      QWidget *myWidget = QWidget::createWindowContainer(myWindow);
      

      However, QWidget::createWindowContainer() was not really designed to handle external windows. You will likely encounter strange issues. See https://bugreports.qt.io/browse/QTBUG-40320 for details.

      Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

      1 Reply Last reply
      3
      • MassiM Offline
        MassiM Offline
        Massi
        wrote on last edited by
        #3

        Thanks for your reply @JKSH

        Unfortunately it didn't work and even if I have a pointer to the window as shown below, it crashes.

        BOOL myFMCclass::OnFileOpen(LPCTSTR szPathname, CWnd* pParent)
        {
        	QFile file(szPathname);
        	if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
        		return false;
        	//Embed a QWindow inside a QWidget
        	QWindow* myWindow = QWindow::fromWinId((WId)pParent);
        	QWidget* myWidget = QWidget::createWindowContainer(myWindow);
        
        	myQtclass* m_widgetptr = new myQtclass(myWidget);
        
           return true;
        }
        

        Well I'm trying to display a QGraphicView within an FMC GUI. and if I create a new QWidget instead of using QWidget::createWindowContainer() I get the following error:
        QWidget: Must construct a QApplication before a QWidget

        Again thanks for you help!!!

        Massi

        Software Design Engineer at Ford - Canada

        JKSHJ 1 Reply Last reply
        0
        • MassiM Massi

          Thanks for your reply @JKSH

          Unfortunately it didn't work and even if I have a pointer to the window as shown below, it crashes.

          BOOL myFMCclass::OnFileOpen(LPCTSTR szPathname, CWnd* pParent)
          {
          	QFile file(szPathname);
          	if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
          		return false;
          	//Embed a QWindow inside a QWidget
          	QWindow* myWindow = QWindow::fromWinId((WId)pParent);
          	QWidget* myWidget = QWidget::createWindowContainer(myWindow);
          
          	myQtclass* m_widgetptr = new myQtclass(myWidget);
          
             return true;
          }
          

          Well I'm trying to display a QGraphicView within an FMC GUI. and if I create a new QWidget instead of using QWidget::createWindowContainer() I get the following error:
          QWidget: Must construct a QApplication before a QWidget

          Again thanks for you help!!!

          Massi

          JKSHJ Offline
          JKSHJ Offline
          JKSH
          Moderators
          wrote on last edited by JKSH
          #4

          @Massi said in CWND to QWidget*:

          even if I have a pointer to the window as shown below, it crashes.

          BOOL myFMCclass::OnFileOpen(LPCTSTR szPathname, CWnd* pParent)
          

          My example is for a HWND, not a CWnd*. You need to convert your pointer to a HWND first. See http://forums.codeguru.com/showthread.php?328037-Converting-CWND*-to-HWND

          Well I'm trying to display a QGraphicView within an FMC GUI...

                                      **QWidget: Must construct a QApplication before a QWidget**
          

          As the error message says, you need to create a QApplication before you can create a QWidget. Furthermore, you must call QApplication::exec() before you can interact with any QWidgets -- this requirement makes it tricky to add QWidgets to an existing non-Qt program, because QApplication::exec() blocks the calling thread.

          You could work around this by creating your QApplication and QWidgets in a different thread (see http://stackoverflow.com/questions/22289423/how-to-avoid-qt-app-exec-blocking-main-thread/22290909#22290909 ). However, I have only done that with "native" QWidgets, not using QWidget::createWindowContainer(). it sounds like you want to embed an MFC window inside a QWidget, and then use that QWidget inside the external MFC application...? This is very convoluted, and I have a feeling it won't work nicely.

          Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

          1 Reply Last reply
          3
          • MassiM Offline
            MassiM Offline
            Massi
            wrote on last edited by Massi
            #5

            Hello @JKSH ,

            Well I want to embed a QGraphicView widget inside a MFC application. I don't know if QWidget class is enough to handle CWnd and use it as a simple QWidget. should I create a Qdialog window and insert the QGraphicView in it and then try to connect it with my MFC GUI?

            BOOL myFMCclass::OnFileOpen(LPCTSTR szPathname, CWnd* pParent)
            {
            	QFile file(szPathname);
            	if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
            		return false;
            
            	//Embed a QWindow inside a QWidget
            	HWND hWind = pParent->GetSafeHwnd();
            	if(hWind != NULL)
            	{	
            		QWindow* myWindow = QWindow::fromWinId((WId)hWind);
            		QWidget* myWidget = QWidget::createWindowContainer(myWindow);
            		if(myWidget)
            			myQtclass* m_widgetptr = new myQtclass(myWidget);
            	}
               return true;
            }
            

            Many thanks!

            Massi

            Software Design Engineer at Ford - Canada

            1 Reply Last reply
            0
            • E Offline
              E Offline
              EmmanuelC
              wrote on last edited by
              #6

              Hello,
              I have to do exactly the same as you, so I tried to code it the same way. but when I run
              QWindow *myWindow = QWindow::fromWinId(externalWindow);
              I am getting an access violation error. Have you had this problem? Do you know how to fix it?
              Thank you,
              Emmanuel

              JonBJ 1 Reply Last reply
              0
              • E EmmanuelC

                Hello,
                I have to do exactly the same as you, so I tried to code it the same way. but when I run
                QWindow *myWindow = QWindow::fromWinId(externalWindow);
                I am getting an access violation error. Have you had this problem? Do you know how to fix it?
                Thank you,
                Emmanuel

                JonBJ Online
                JonBJ Online
                JonB
                wrote on last edited by
                #7

                @EmmanuelC
                If your code is also like:

                	QWindow* myWindow = QWindow::fromWinId((WId)pParent);
                	QWidget* myWidget = QWidget::createWindowContainer(myWindow);
                

                are you sure that your access violation comes during fromWinId() rather than from createWindowContainer? If it's the latter it might be worth looking at https://stackoverflow.com/questions/30162091/drawing-errors-and-crash-with-qwidgetcreatewindowcontainer.

                1 Reply Last reply
                0
                • E Offline
                  E Offline
                  EmmanuelC
                  wrote on last edited by
                  #8

                  Hello JonB, thank you for your answer. Yes I am sure because I didn't even include QWidget::createWindowContainer yet. (I wanted to test first if the four first functions worked.)

                  For now my code is like this:

                  CWnd* cwnd= GetDlgItem(IDC_STATIC);
                  HWND hWnd = cwnd->GetSafeHwnd();
                  WId externalWindow = (WId)hWnd;
                  QWindow *myWindow = QWindow::fromWinId(externalWindow);
                  

                  I don't call any other function after that, and really don't understand why I have an access violation here!

                  JonBJ 1 Reply Last reply
                  0
                  • E EmmanuelC

                    Hello JonB, thank you for your answer. Yes I am sure because I didn't even include QWidget::createWindowContainer yet. (I wanted to test first if the four first functions worked.)

                    For now my code is like this:

                    CWnd* cwnd= GetDlgItem(IDC_STATIC);
                    HWND hWnd = cwnd->GetSafeHwnd();
                    WId externalWindow = (WId)hWnd;
                    QWindow *myWindow = QWindow::fromWinId(externalWindow);
                    

                    I don't call any other function after that, and really don't understand why I have an access violation here!

                    JonBJ Online
                    JonBJ Online
                    JonB
                    wrote on last edited by
                    #9

                    @EmmanuelC
                    I know nothing about this, but...

                    The example above (@JKSH) using fromWinId() called it on the result of a FindWindow() for a window from another process, externalWindow. https://doc.qt.io/qt-5/qwindow.html#fromWinId says:

                    Creates a local representation of a window created by another process or by using native libraries below Qt.

                    You have a CWnd, from an internal window I believe. I don't know how that plays.

                    As I said, I know nothing, so I probably can't say much more than this....

                    1 Reply Last reply
                    0

                    • Login

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