Results 1 to 11 of 11

Thread: grabWidget problem

  1. #1
    Join Date
    Nov 2009
    Posts
    10
    Thanks
    2

    Default grabWidget problem

    Hello all,

    This is supposed to grab a section of the parent widget when this widget is moved and display the grab. It isn't working.
    Any insights will be greatly appreciated!!!

    TIA,

    gonzoboy!

    void ZoomWindow::moveFrame()
    {
    move(mp.x(), mp.y());

    QWidget w(this->parentWidget());
    w.show();
    // grab a 40x40 chunk of parent widget
    pm = QPixmap::grabWidget(&w, 0, 0, 39, 39);
    }

    void ZoomWindow:aintEvent(QPaintEvent* pe)
    {
    QPainter painter(this);
    QRect r(0, 0, 199, 199);
    QPen pen(Qt::black);

    // paint the pixmap from parent zoomed 5x in ZoomWindow
    painter.drawPixmap(r, pm);
    painter.drawRect(r);

    }

  2. #2
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: grabWidget problem

    try this
    Qt Code:
    1. ...
    2. pm = QPixmap::grabWidget(parentWidget(), 0, 0, 39, 39);
    3. ...
    To copy to clipboard, switch view to plain text mode 
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  3. #3
    Join Date
    Nov 2009
    Posts
    10
    Thanks
    2

    Unhappy Re: grabWidget problem

    Thanks, that actually gave me the same strange result that grabWindow returns. Any other notions?
    Last edited by gonzoboy!; 17th November 2009 at 19:12. Reason: spelling error

  4. #4
    Join Date
    Sep 2009
    Posts
    140
    Thanks
    4
    Thanked 17 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: grabWidget problem

    Hi,

    When does your moveFrame() method is called?

    Be careful that if it is in response of a mouseMove event,
    then moving your widget inside will trigger another mouseMove, since it is moved relatively to the mouse!!

    Hard to get rid of this and get expected behaviour. Think well your design.

  5. #5
    Join Date
    Nov 2009
    Posts
    10
    Thanks
    2

    Default Re: grabWidget problem

    The mouse event is in the parent. This triggers the grab in the child.

  6. #6
    Join Date
    Sep 2009
    Posts
    140
    Thanks
    4
    Thanked 17 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: grabWidget problem

    I wonder if the widget your are creating in moveFrame has the time to be painted before go out the method. The event is queued, no?

    That is why spirit told you to use direclty the parent instead of creating a temporary widget.

    But what about moving a simple colored QPixmap?

  7. #7
    Join Date
    Nov 2009
    Posts
    10
    Thanks
    2

    Default Re: grabWidget problem

    Scascio,

    It isn't queued. How would one go about doing that?

    g

  8. #8
    Join Date
    Nov 2009
    Posts
    10
    Thanks
    2

    Default Re: grabWidget problem

    Here is the trigger (in main.cpp):
    QObject::connect(&w, SIGNAL(mouseMoved()), &zw, SLOT(moveFrame()));

  9. #9
    Join Date
    Nov 2009
    Posts
    10
    Thanks
    2

    Default Re: grabWidget problem

    Yes, I think you've nailed the problem. When I do the grab from the child constructor for a fixed part of the parent, it grabs fine. SO, how do I "slow it down" enough to do an updated grab after a mouseMove without really slowing the app down?


    pm = QPixmap::grabWidget(parentWidget(), 0, 0, 50, 50);

  10. #10
    Join Date
    Sep 2009
    Posts
    140
    Thanks
    4
    Thanked 17 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: grabWidget problem

    In tried to solve your zoom window need and I encountered several problems

    • A factor 5 seems a lot.
    • You need to set a size for the ZoomWindow to have it painted correctly
    • You dont need to get through SIGNAL/SLOT since mouseMoveEvent in parent widget is a virtual method.


    I think your main problem is that when you grab the widget in the child window, you will grab all the parent, so with the child(the zoom) in it!! so you need to make it invisible and restore it after.

    And ones I didnt solve :
    • the zoom is painted under the layout in background (=>maybe a window flag?),
    • the mouseMoveEvent is not trigger on child widget of the layout (=>use eventFilter?)

    Here is the parent widget :

    Qt Code:
    1. #ifndef MYMAINWIDGET_H
    2. #define MYMAINWIDGET_H
    3.  
    4. #include <QtGui/QDialog>
    5. #include "ui_MyMainWidget.h"
    6.  
    7. #include <QMouseEvent>
    8.  
    9. #include "ZoomWindow.h"
    10.  
    11. class MyMainWidget : public QDialog {
    12.  
    13. Q_OBJECT
    14.  
    15. public:
    16.  
    17. MyMainWidget(QWidget *parent = 0)
    18. : QDialog(parent), m_zoomWindow(this) {
    19. ui.setupUi(this);
    20. setMouseTracking(true);
    21. }
    22.  
    23. virtual ~MyMainWidget() {
    24. }
    25.  
    26. protected:
    27.  
    28. void mouseMoveEvent(QMouseEvent * event){
    29. QWidget::mouseMoveEvent(event);
    30. m_zoomWindow.update(event->pos());
    31. }
    32.  
    33. private:
    34.  
    35. Ui::MyMainWidgetClass ui;
    36.  
    37. ZoomWindow m_zoomWindow;
    38. };
    39.  
    40. #endif // MYMAINWIDGET_H
    To copy to clipboard, switch view to plain text mode 

    And here is the Zoom Window

    Qt Code:
    1. #ifndef ZOOMWINDOW_H
    2. #define ZOOMWINDOW_H
    3.  
    4. #include <QtGui/QWidget>
    5.  
    6. #include <QPixmap>
    7. #include <QPainter>
    8.  
    9. class ZoomWindow : public QWidget {
    10.  
    11. Q_OBJECT
    12.  
    13. public:
    14.  
    15. ZoomWindow(QWidget *parent = 0, int factor=2)
    16. :QWidget(parent), m_factor(factor) {
    17. resize(200, 200);
    18. }
    19.  
    20. virtual ~ZoomWindow() {
    21. }
    22.  
    23. void update(const QPoint& point) {
    24. setVisible(false);
    25. m_pixmap = QPixmap::grabWidget(parentWidget(), QRect(point, size()/m_factor));
    26. setVisible(true);
    27. move(point);
    28. QWidget::update();
    29. }
    30.  
    31. protected:
    32.  
    33. virtual void paintEvent(QPaintEvent * /*event*/) {
    34. QRect r(rect());
    35. QPainter painter(this);
    36. painter.setPen(Qt::black);
    37. painter.drawPixmap(r, m_pixmap);
    38. painter.drawRect(r);
    39. }
    40.  
    41. private:
    42.  
    43. int m_factor;
    44. QPixmap m_pixmap;
    45. };
    46.  
    47. #endif // ZOOMWINDOW_H
    To copy to clipboard, switch view to plain text mode 

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

    gonzoboy! (17th November 2009)

  12. #11
    Join Date
    Nov 2009
    Posts
    10
    Thanks
    2

    Default Re: grabWidget problem

    Scoscio,

    Thanks so much. I will try what you have suggested. I am working on an assignment and it specified zoom factor 5. I like the update overload approach. Stay tuned...

    gonzoboy!

Similar Threads

  1. Problem with QAbstractListModel
    By eekhoorn12 in forum Qt Programming
    Replies: 3
    Last Post: 26th August 2009, 14:26
  2. Replies: 1
    Last Post: 23rd April 2009, 09:05
  3. Replies: 19
    Last Post: 3rd April 2009, 23:17
  4. Grid Layout Problem
    By Seema Rao in forum Qt Programming
    Replies: 2
    Last Post: 4th May 2006, 12:45
  5. fftw problem
    By lordy in forum General Programming
    Replies: 1
    Last Post: 16th March 2006, 21:36

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.