Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Why doesn’t QWinHost draw, when it has a parent?



  • I’m using QWinMigrate library to draw something MFC on Qt widgets.
    When I create HostWindow (subclass QWinHost) independently – all is fine!
    But When I set a parent to HostWindow (Qt QMainWindow – in my case) only black rectangle appear
    instead … Please, tell what’s wrong I do.

    Considered examples on http://doc.trolltech.com/solutions/qtwinmigrate/winmigrate-win32-in-qt-example.html
    I have written code bellow:
    ————-Declaration————-
    @
    class HostWindow : public QWinHost
    {
    Q_OBJECT

    private:
    static CWnd* mp_wnd;

    public:
    HostWindow(QWidget* ip_parent = 0, Qt::WFlags f = 0);
    ~HostWindow();

    HWND createWindow(HWND i_parent, HINSTANCE i_instance);
    

    protected:
    static LRESULT CALLBACK WndProc(HWND i_hWnd, UINT i_message, WPARAM i_wParam, LPARAM i_lParam);
    };
    @

    ///////////////////////////////////////
    ————-Definition—————-

    @
    CWnd* HostWindow::mp_wnd = 0;

    HostWindow::HostWindow(QWidget* ip_parent, Qt::WFlags f) : QWinHost(ip_parent, f)
    {}

    HostWindow::~HostWindow()
    {}

    HWND HostWindow::createWindow(HWND i_parent, HINSTANCE i_instance)//todo check for if needless
    {
    static ATOM windowClass = 0;
    if ( !windowClass ) {
    WNDCLASSEX wcex;
    wcex.cbSize = sizeof(WNDCLASSEX);
    wcex.style = CS_HREDRAW | CS_VREDRAW;
    wcex.lpfnWndProc = (WNDPROC)WndProc;
    wcex.cbClsExtra = 0;
    wcex.cbWndExtra = 0;
    wcex.hInstance = i_instance;
    wcex.hIcon = NULL;
    wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
    wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
    wcex.lpszMenuName = NULL;
    wcex.lpszClassName = L"qtest";
    wcex.hIconSm = NULL;

    windowClass = RegisterClassEx(&wcex);
    }
    

    HWND hwnd = CreateWindow((TCHAR*)windowClass, 0, WS_CHILD | WS_VISIBLE,
    CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, i_parent, NULL, i_instance, NULL);

    mp_wnd = new CWnd;
    mp_wnd->SubclassWindow(hwnd);
    return mp_wnd->GetSafeHwnd();
    }

    LRESULT CALLBACK HostWindow::WndProc(HWND i_hWnd, UINT i_message, WPARAM i_wParam, LPARAM i_lParam)
    {
    QWidget *widget = QWidget::find(GetParent(i_hWnd));
    HostWindow window = NULL;
    if(widget->inherits("HostWindow"))
    window = (HostWindow
    )widget;

    if(window)
    {
    switch (i_message)
    {
    case WM_PAINT:
    {
    qDebug()<<"WM_PAINT\n";
    /////////////////////////////////////
    CRect rect;
    mp_wnd->GetClientRect(rect);
    CDC* c_dc = CDC::FromHandle(GetDC(mp_wnd->GetSafeHwnd()));
    CBrush brush;
    brush.CreateSolidBrush(RGB(255,0,0));
    CBrush *pOldBrush= c_dc->SelectObject(&brush);
    c_dc->Ellipse(rect);
    c_dc->SelectObject(pOldBrush);

    /////////////////////////////////////
    }
    break;
    default:
    return DefWindowProc(i_hWnd, i_message, i_wParam, i_lParam);
    }
    }
    return 0;
    }

    class MainWindow : public QMainWindow
    {
    //..
    HostWindow* mp_host_window;
    //..
    }

    MainWindow::MainWindow(QWidget *parent, Qt::WFlags flags): QMainWindow(parent, flags)
    {
    //... doesn't work (reproduces a black rect area)

    mp_host_window = new HostWindow(this);

    //..

    //.. shows separated window with right drawed ellipse

    mp_host_window = new HostWindow;
    mp_host_window->show()

    //..
    }
    @



  • If you look at the example, they create a windows and return the pointer, they donÄt use subclass windows. Why don't you to it the same way, just with CWnd?


Log in to reply