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. Why doesn’t QWinHost draw, when it has a parent?
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved General and Desktop
2 Posts 2 Posters 4.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.
  • K Offline
    K Offline
    korel
    wrote on last edited by
    #1

    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()

    //..
    }
    @

    1 Reply Last reply
    0
    • G Offline
      G Offline
      giesbert
      wrote on last edited by
      #2

      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?

      Nokia Certified Qt Specialist.
      Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

      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