Results 1 to 2 of 2

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

  1. #1
    Join Date
    May 2010
    Posts
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default 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/q...t-example.html

    I have written code bellow:
    ————-Declaration————-
    Qt Code:
    1. class HostWindow : public QWinHost
    2. {
    3. Q_OBJECT
    4.  
    5. private:
    6. static CWnd* mp_wnd;
    7.  
    8. public:
    9. HostWindow(QWidget* ip_parent = 0, Qt::WFlags f = 0);
    10. ~HostWindow();
    11.  
    12. HWND createWindow(HWND i_parent, HINSTANCE i_instance);
    13.  
    14. protected:
    15. static LRESULT CALLBACK WndProc(HWND i_hWnd, UINT i_message, WPARAM i_wParam, LPARAM i_lParam);
    16. };
    To copy to clipboard, switch view to plain text mode 

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

    Qt Code:
    1. CWnd* HostWindow::mp_wnd = 0;
    2.  
    3. HostWindow::HostWindow(QWidget* ip_parent, Qt::WFlags f) : QWinHost(ip_parent, f)
    4. {}
    5.  
    6. HostWindow::~HostWindow()
    7. {}
    8.  
    9. HWND HostWindow::createWindow(HWND i_parent, HINSTANCE i_instance)//todo check for if needless
    10. {
    11. static ATOM windowClass = 0;
    12. if ( !windowClass ) {
    13. WNDCLASSEX wcex;
    14. wcex.cbSize = sizeof(WNDCLASSEX);
    15. wcex.style = CS_HREDRAW | CS_VREDRAW;
    16. wcex.lpfnWndProc = (WNDPROC)WndProc;
    17. wcex.cbClsExtra = 0;
    18. wcex.cbWndExtra = 0;
    19. wcex.hInstance = i_instance;
    20. wcex.hIcon = NULL;
    21. wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
    22. wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
    23. wcex.lpszMenuName = NULL;
    24. wcex.lpszClassName = L"qtest";
    25. wcex.hIconSm = NULL;
    26.  
    27. windowClass = RegisterClassEx(&wcex);
    28. }
    29.  
    30. HWND hwnd = CreateWindow((TCHAR*)windowClass, 0, WS_CHILD | WS_VISIBLE,
    31. CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, i_parent, NULL, i_instance, NULL);
    32.  
    33.  
    34. mp_wnd = new CWnd;
    35. mp_wnd->SubclassWindow(hwnd);
    36. return mp_wnd->GetSafeHwnd();
    37. }
    38.  
    39.  
    40. LRESULT CALLBACK HostWindow::WndProc(HWND i_hWnd, UINT i_message, WPARAM i_wParam, LPARAM i_lParam)
    41. {
    42. QWidget *widget = QWidget::find(GetParent(i_hWnd));
    43. HostWindow *window = NULL;
    44. if(widget->inherits("HostWindow"))
    45. window = (HostWindow*)widget;
    46.  
    47. if(window)
    48. {
    49. switch (i_message)
    50. {
    51. case WM_PAINT:
    52. {
    53. qDebug()<<"WM_PAINT\n";
    54. /////////////////////////////////////
    55. CRect rect;
    56. mp_wnd->GetClientRect(rect);
    57. CDC* c_dc = CDC::FromHandle(GetDC(mp_wnd->GetSafeHwnd()));
    58. CBrush brush;
    59. brush.CreateSolidBrush(RGB(255,0,0));
    60. CBrush *pOldBrush= c_dc->SelectObject(&brush);
    61. c_dc->Ellipse(rect);
    62. c_dc->SelectObject(pOldBrush);
    63.  
    64. /////////////////////////////////////
    65. }
    66. break;
    67. default:
    68. return DefWindowProc(i_hWnd, i_message, i_wParam, i_lParam);
    69. }
    70. }
    71. return 0;
    72. }
    73.  
    74.  
    75. class MainWindow : public QMainWindow
    76. {
    77. //..
    78. HostWindow* mp_host_window;
    79. //..
    80. }
    81.  
    82. MainWindow::MainWindow(QWidget *parent, Qt::WFlags flags): QMainWindow(parent, flags)
    83. {
    84. //... doesn't work (reproduces a black rect area)
    85.  
    86. mp_host_window = new HostWindow(this);
    87.  
    88. //..
    89.  
    90. //.. shows separated window with right drawed ellipse
    91.  
    92. mp_host_window = new HostWindow;
    93. mp_host_window->show()
    94.  
    95. //..
    96. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Mar 2011
    Posts
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

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

    I realize this post is rather stale, but in the interest of future reference, I think I had the same problem you're having when I started using QWinHost. I fixed it by overriding focusInEvent() to make sure the Win32 window would redraw when it needed to:

    Qt Code:
    1. void HostWindow::resizeEvent(QResizeEvent* e)
    2. {
    3. QWinHost::resizeEvent(e);
    4. mp_wnd->Invalidate(TRUE);
    5. }
    To copy to clipboard, switch view to plain text mode 

    It'd be nice if QWinHost did this itself!

Similar Threads

  1. Qt Designer Visual Studio Plugin: Qt Designer doesn’t work
    By worstnbro in forum Qt Tools
    Replies: 3
    Last Post: 4th April 2012, 13:36
  2. What does *parent=0 mean?
    By veraS in forum Newbie
    Replies: 9
    Last Post: 27th June 2010, 20:32
  3. Replies: 5
    Last Post: 21st April 2010, 21:36
  4. Replies: 2
    Last Post: 21st March 2010, 08:20
  5. Using QAbstractTableModel.parent
    By Max Yaffe in forum Newbie
    Replies: 7
    Last Post: 15th June 2007, 15:21

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.