Results 1 to 7 of 7

Thread: Help with QWidget::grab

  1. #1
    Join Date
    Aug 2014
    Posts
    11
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Help with QWidget::grab

    In the following code the commented QPixmap::grab works, but is depreciated. I am trying to get QWidget::grab to work. I am wanting to grab the entire dialog so I am supplying no rect argument, wanting to use the default. I get the following error:

    **C:\My Libraries\Programming\CppProj\ptest\printtest\main .cpp:16: error: cannot call member function 'QPixmap QWidget::grab(const QRect&)' without object
    QPixmap pm = ArticleDialog::grab();
    ^
    but I thought grab was static? If I replace the class "ArticleDialog" with the instance myWidget, I get the error:

    **C:\My Libraries\Programming\CppProj\ptest\printtest\main .cpp:15: error: 'myWidget' is not a class or namespace
    QPixmap pm = myWidget::grab();
    ^

    What am I doing wrong?
    Qt Code:
    1. #include "articledialog.h"
    2. #include <QApplication>
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication a(argc, argv);
    7. ArticleDialog myWidget;
    8. myWidget.show();
    9. QPrinter printer;
    10. QPrintDialog printDialog(&printer);
    11. if (printDialog.exec() == QDialog::Accepted) {
    12.  
    13. QPainter p(&printer);
    14. //QPixmap pm = QPixmap::grabWidget(&myWidget);
    15. QPixmap pm = ArticleDialog::grab();
    16. p.drawPixmap(0, 0, pm);
    17. }
    18.  
    19. return a.exec();
    20. }
    To copy to clipboard, switch view to plain text mode 
    _______________________________

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Help with QWidget::grab

    QWidget::grab() is not a static method. (Think about it - if it were a static method, that means it doesn't need a QWidget instance in order to be called. If that were the case, then which QWidget's content would it grab?)

    If I replace the class "ArticleDialog" with the instance myWidget: QPixmap pm = myWidget::grab();
    That's not what this code is doing. It's saying, call the static method grab() of the class named "myWidget". So, this is basic C++. If you want to call a non-static member function, you need to call it through an instance of the object:

    Qt Code:
    1. QPixmap pm = myWidget.grab();
    To copy to clipboard, switch view to plain text mode 

    But your code, even if you do get it to compile, will not work. Your dialog will not be shown until the Qt event loop starts running, and a.exec() is what does that. You can't grab the dialog's contents until the event loop is running because there's nothing there to grab.

  3. #3
    Join Date
    Aug 2014
    Posts
    11
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: Help with QWidget::grab

    Thanks d_stranz,
    When I replaced the class name by an instance, I forgot to change the :: to . -how dumb! It did however run as written and printed correctly.

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Help with QWidget::grab

    It did however run as written and printed correctly.
    It did? The same code you posted above after fixing it? I didn't think it should have, but maybe Qt queued up all those events until the event loop was running, and then started executing them. That does make sense, actually - the myWidget.show() call occurs before the loop is running, so the event that it triggers must just sit there in the queue waiting until it can be processed. Learned something here.

  5. #5
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Help with QWidget::grab

    QWidget::grab() might just render the widget into a pixmap buffer instead of the usual backing store buffer, thus be independent of event processing.
    Also the show() and respective show event will have likely have been processed by the dialog's nested event loop.

    Cheers,
    _

  6. The following user says thank you to anda_skoa for this useful post:

    d_stranz (19th September 2014)

  7. #6
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Help with QWidget::grab

    Also the show() and respective show event will have likely have been processed by the dialog's nested event loop.
    Very good point. Forgot that QDialog runs its own event loop. So it must be independent of the QApplication event loop (which implies not nested, and sharing the same queue) and also not require that the QApplication event loop be running. This is more interesting than I thought - a lot of side-effects happening which just happen to cause the example to work.

  8. #7
    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: Help with QWidget::grab

    I think a lot depends on the content of the widget being grabbed, as if it has a layout, then the layout is (by default) executed when the widget is first shown. So either grab() would have to force the layout to be processed or you'd need to do it yourself for all this to work without the event loop.

    As for the event loop itself, both QCoreApplication::exec() and QDialog::exec() are merely interfaces to QAbstractEventDispatcher which is a kind-of singleton (or actually a per-thread-instance) that does event processing. So to be precise, a dialog does not have its own event loop but rather its own event loop entry point (triggering the same event loop).
    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. how to grab mouse Event without QWidget
    By waiter in forum Qt Programming
    Replies: 1
    Last Post: 21st March 2013, 10:36
  2. How to grab screen on thread?
    By Alex Snet in forum Qt Programming
    Replies: 6
    Last Post: 18th May 2009, 22:08
  3. grab images from Phonon video
    By Kraut~salat in forum Qt Programming
    Replies: 3
    Last Post: 19th January 2009, 10:53
  4. Grab Focus for QSplashScreen:Urgent
    By csvivek in forum Qt Programming
    Replies: 0
    Last Post: 24th April 2008, 08:43
  5. Grab idea from Amarok
    By jiveaxe in forum Qt Programming
    Replies: 8
    Last Post: 6th November 2007, 19:11

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.