Results 1 to 15 of 15

Thread: Problem with moving QWinWidget

  1. #1
    Join Date
    May 2011
    Posts
    10
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Problem with moving QWinWidget

    Hi,

    I'm using QWinWidget from Qt's Windows Migration Toolkit to extend my old MFC application with Qt widgets. Because I wanted to keep the code as clean as possible, I created a template class that behaves as a placeholder for the different Qt widgets. Code:

    Qt Code:
    1. #ifndef QPlaceHolder_H
    2. #define QPlaceHolder_H
    3.  
    4. #include <QWinWidget>
    5. #include <QHBoxLayout>
    6.  
    7. template <typename QWidgetType>
    8. class QPlaceHolder : public QWinWidget {
    9. public:
    10. QPlaceHolder(CWnd* parentWnd) : QWinWidget(parentWnd)
    11. {
    12. myWidget = new QWidgetType(this);
    13. QHBoxLayout* hbox = new QHBoxLayout(this);
    14. hbox->setContentsMargins(0, 0, 0, 0);
    15. hbox->addWidget(myWidget);
    16. myWidget->show();
    17. QWinWidget::show();
    18.  
    19. }
    20.  
    21. ~QPlaceHolder ()
    22. {
    23. }
    24.  
    25. QWidgetType* widget()
    26. {
    27. return myWidget;
    28. }
    29.  
    30. private:
    31. QWidgetType* myWidget;
    32. };
    33.  
    34. #endif //QPlaceHolder_H
    To copy to clipboard, switch view to plain text mode 

    So example use of my code would be e.g.

    Qt Code:
    1. // somewhere in my main MFC window code
    2. QPlaceHolder<QLabel>* label = new QPlaceHolder<QLabel>(this);
    3. label->widget()->setText("Hello!");
    4. label->move(200, 100); //moving command must be done for the base widget (...right?)
    5. //delete label; //this would delete also the QLabel object
    To copy to clipboard, switch view to plain text mode 

    The problem is:
    Running this code makes the label visible, BUT it can't be moved (it stays in location (0, 0). I've tried different solutions without success. Do you have any suggestions that I could try?

    And second problem is:
    I'd like to use the graphical UI editor of MFC framework to locate the Qt widgets. Any suggestions how to achieve this?

    Also any other comments/improvements are highly appreciated!

    Big thanks!
    Rgs,
    jazuju
    Last edited by jazuju; 30th August 2011 at 12:25.

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Problem with moving QWinWidget

    I don't think you can call move on a "top level" (from Qt perspective relative to your MFC GUI) since that is the domain of MFC.
    Unless you make it really top level (i.e no parent).
    You can move your widget in bounds of your QWinWidget, but for moving your QWinWidget you will probably have to write MFC code.
    Last edited by high_flyer; 31st August 2011 at 09:23.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    May 2011
    Posts
    10
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Problem with moving QWinWidget

    Hi,

    sorry for late response

    Thanks for the response - I think you're right: moving the QWinWidget has to be done with MFC code. However, I'm not very familiar with MFC or Win32 programming and thus my solutions have not worked so far.

    Here's an example from my latest non-functional solution. Do you think I'm on a right path? Suggestions?

    Qt Code:
    1. //in QPlaceHolder implementation (subclass of QWinWidget)
    2. void move(int x, int y)
    3. {
    4. HWND wnd = QWidget::winId();
    5. HWND pwnd = GetParent(wnd);
    6. POINT wndpoint;
    7. wndpoint.x = x;
    8. wndpoint.y = y;
    9. ScreenToClient(pwnd, &wndpoint);
    10. RECT wndrect;
    11. GetWindowRect(wnd, &wndrect);
    12. MoveWindow(wnd, wndpoint.x, wndpoint.y,
    13. wndrect.right-wndrect.left, wndrect.bottom-wndrect.top, true);
    14. UpdateWindow(pwnd);
    15. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Problem with moving QWinWidget

    Why are you embedding MFC code in a Qt app?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    May 2011
    Posts
    10
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Problem with moving QWinWidget

    I'm trying to extend an existing MFC app with Qt widgets. I'm doing this by using the QWinWidget from Windows Migration Toolkit as a placeholder for the QWidgets. QWinWidget itself is a QWidget that can be placed as a child of CWnd. The problem is, I haven't found a way to move the QWinWidget.

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Problem with moving QWinWidget

    Maybe it's easier to just rewrite MFC code in Qt? Remember that you can use all regular WinAPI calls in a Qt app, you just need to rewrite MFC-specific parts.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    May 2011
    Posts
    10
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Problem with moving QWinWidget

    Quote Originally Posted by wysota View Post
    Maybe it's easier to just rewrite MFC code in Qt?
    In our case that's not practical because our software has >200k rows of MFC code and immediate transfer to Qt would mean a huge work (and risk). So we're trying to replace the program little by little with Qt code and hopefully eventually move entirely into Qt. And I think that's a really appealing way to go.

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Problem with moving QWinWidget

    I'm sure it is not >200k lines of MFC code, I bet only a couple hundred of lines are MFC-specific. You need to remember that MFC has a different philosophy than Qt so if you just stuff MFC controls into a Qt app, you're in practice dealing with two totally separate applications that use a different approach. You should start your work with small steps that make big differences and then replace code that is too hard to adapt. If you can't move controls written in MFC then either don't move them or replace those particular windows with Qt code or write MFC code that performs the work (as high_flyer suggested).
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. #9
    Join Date
    May 2011
    Posts
    10
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Problem with moving QWinWidget

    Quote Originally Posted by wysota View Post
    I'm sure it is not >200k lines of MFC code, I bet only a couple hundred of lines are MFC-specific.
    Well, maybe the truth lies somewhere between those :P Thanks for your advice! Despite the cons of mixing MFC and Qt, the pros are much more remarkable in our situation. For example, our software uses lot of customized MFC controls that are tedious to reimplement in Qt. Thus, with all respect to your advice, we're trying to go the way I described.

    I've had some progress. With the following code the QPlaceHolder widget (subclass of QWinWidget) can alse be moved as a child of CWnd.


    Qt Code:
    1. #ifndef QPlaceHolder_H
    2. #define QPlaceHolder_H
    3.  
    4. #include <QWinWidget>
    5. #include <QHBoxLayout>
    6. #include <QMessageBox>
    7.  
    8. template <typename QWidgetType>
    9. class QPlaceHolder : public QWinWidget {
    10. public:
    11. QPlaceHolder(CWnd* parentWnd) : QWinWidget(parentWnd)
    12. {
    13. myWidget = new QWidgetType(this);
    14. QHBoxLayout* hbox = new QHBoxLayout(this);
    15. hbox->setContentsMargins(0, 0, 0, 0);
    16. hbox->addWidget(myWidget);
    17. myWidget->show();
    18. QWinWidget::show();
    19.  
    20. }
    21.  
    22.  
    23.  
    24. void move(int x, int y)
    25. {
    26. HWND wnd = QWidget::winId();
    27. HWND parentwnd = GetParent(wnd);
    28. QMessageBox::information(NULL, "Before moving QPlaceHolder", QString("curx=%1, cury=%2, curwidth=%3, curheight=%4").arg(this->pos().x()).arg(this->pos().y()).arg(this->width()).arg(this->height()));
    29. RECT parentRect;
    30. RECT myRect;
    31. GetWindowRect(parentwnd, &parentRect);
    32. GetWindowRect(wnd, &myRect);
    33. POINT topleft;
    34. topleft.x = parentRect.left + x;
    35. topleft.y = parentRect.top + y;
    36. ScreenToClient(parentwnd, &topleft);
    37. int width = myRect.right - myRect.left;
    38. int height = myRect.bottom - myRect.top;
    39. MoveWindow(wnd, topleft.x, topleft.y, width, height, TRUE);
    40. UpdateWindow(wnd);
    41. }
    42.  
    43.  
    44. QWidgetType* widget()
    45. {
    46. return myWidget;
    47. }
    48.  
    49. private:
    50. QWidgetType* myWidget;
    51. };
    52.  
    53.  
    54. #endif //QPlaceHolder_H
    To copy to clipboard, switch view to plain text mode 


    There's still a problem that I can't resolve: moving the widgets is working only if the line 28 (QMessageBox::information....) is executed. If I comment that row out, moving doesn't work anymore. Weird. Do you have any clue what's going on here?

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Problem with moving QWinWidget

    Does it work if you replace the line with QApplication::processEvents()?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  11. The following user says thank you to wysota for this useful post:

    jazuju (15th September 2011)

  12. #11
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Problem with moving QWinWidget

    It sounds to me that the popping up of the information message box forces an update on your widget, which without it doesn't happen, as I also don't see it in your code (Qt side, not MFC).
    Try calling myWidget->upate() at the end of move(), see if it helps.

    EDIT:
    @wasota: was also my first thought, but it would mean that the event loop is busy, and I don't see a reason why it should be, based on the code he posted so far.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  13. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Problem with moving QWinWidget

    It doesn't have to mean it is busy, some attributes might just have improper values for the move to succeed. However I think the situation is strictly MFC-specific and Qt has little to do with this so you might be right.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  14. #13
    Join Date
    May 2011
    Posts
    10
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Problem with moving QWinWidget

    Hooray! Putting
    Qt Code:
    1. QApplication::processEvents()
    To copy to clipboard, switch view to plain text mode 
    in the beginning of move() made the magic and now everything seems to work. Big thanks for you, guys!

    I also tried the update() method but that didn't help. So what do you think - was this problem caused by not-yet-initialized Qt widgets?

  15. #14
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Problem with moving QWinWidget

    some attributes might just have improper values for the move to succeed.
    So you mean, that the events that bring about the correct state first need to be processed before the move is completed, is that it?
    Hmm... seems to makes sense, as the information dialog is modal, so the event loop returns and does a bit work right after the dialog is dismissed, which is not happening when the dialog is not popped...
    Or did you mean something else?
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  16. #15
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Problem with moving QWinWidget

    Something like that. I don't know how the MFC-migration framework works, it is just a guess.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Transparency with QWinWidget
    By martian in forum Qt Programming
    Replies: 7
    Last Post: 28th February 2015, 06:13
  2. QWinWidget & transparency
    By kinju in forum Qt Programming
    Replies: 1
    Last Post: 8th June 2010, 12:07
  3. Replies: 0
    Last Post: 24th May 2010, 12:19
  4. Problem using QWinWidget..
    By Muffin in forum Newbie
    Replies: 3
    Last Post: 8th March 2010, 16:02
  5. Deleteing QWinWidget
    By elizabeth.h1 in forum Qt Programming
    Replies: 0
    Last Post: 11th October 2009, 19:26

Tags for this Thread

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.