Results 1 to 3 of 3

Thread: Does Q*Application always needed?

  1. #1
    Join Date
    Dec 2016
    Posts
    1

    Default Does Q*Application always needed?

    Hello.
    Please excuse me for my poor english.

    I am trying to write a program, and one of its functions will be manipulations with windowses of other programs.
    In particular, moving them and making its screenshots.

    I am trying to make screenshot with code like this one:
    Qt Code:
    1. for (const auto& screen : QGuiApplication::screens()) {
    2. if (screen) {
    3. QPixmap screenshot = screen->grabWindow(window_id);
    4. if (!screenshot.isNull() && screenshot.save("screenshot.png")) {
    5. cout << "saved" << endl;
    6. }
    7. }
    8. }
    To copy to clipboard, switch view to plain text mode 

    However, to make it work, I have to create a Q*Application object:
    Qt Code:
    1. QApplication a(argc, argv);
    To copy to clipboard, switch view to plain text mode 

    Without it, nothing works (QGuiApplication::screens() is empty).

    In case where a program activelu uses Qt it is not a problem.
    However, I am trying to make a small library, which can manipulate with windovs via qt, and use the library in my program.

    So I want to make my program qt-independent (with exception of the manipulating library ofc).
    In this case making global object of a qt type is looking bad.

    So what is correct way to solve the task?
    Make a singleton with QApplication inside the library?

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,345
    Qt products
    Qt5
    Platforms
    Windows
    Thanks
    318
    Thanked 872 Times in 859 Posts

    Default Re: Does Q*Application always needed?

    An application class instance (something derived from QCoreApplication) is needed because it supplies an event loop to process events (Windows messages on Windows, X-Windows messages on linux, etc.). Almost everything in Qt depends on having an event loop running. There is only one QCoreApplication instance allowed, so if you implement one in your library, it means your library probably can't be used with another Qt app.

    If you want to do anything GUI-related, you need a class based on QGuiApplication, because this class handles messages from the windowing system.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

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

    Default Re: Does Q*Application always needed?

    QGuiApplication also loads the QPA (Qt Platform Adapter) plugin, which provides the intergration with the windowing system.

    The QPA is the place that actually interfaces with the native API to get the screens, etc.

    A Q*Application instance is in effect a singleton, i.e. only one instance can be created.

    Cheers,
    _

Similar Threads

  1. Qt Application for Android that uses pythonscripts, What is needed?
    By ntn12eon in forum Qt for Embedded and Mobile
    Replies: 0
    Last Post: 17th December 2015, 12:22
  2. dll's needed to launch application
    By valdemar593 in forum Qt Programming
    Replies: 4
    Last Post: 28th May 2011, 12:19
  3. Advice needed for QT application architecture
    By hubbobubbo in forum Qt Programming
    Replies: 0
    Last Post: 14th December 2009, 12:30
  4. Qt Application + Web Integration -- Help needed
    By swamyonline in forum Qt Programming
    Replies: 0
    Last Post: 17th February 2009, 12:59
  5. Mac Style for Qt Application---help needed
    By swamyonline in forum Qt Programming
    Replies: 3
    Last Post: 15th June 2008, 22:49

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.