Results 1 to 5 of 5

Thread: How to determine desktop number?!?

  1. #1
    Join Date
    Feb 2015
    Location
    Poland
    Posts
    34
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows Android

    Angry How to determine desktop number?!?

    Hi!
    I am writing text editor in multiplatform way. In the past I most time spending on Windows, but now I am back to the Linux and I want next version of my text editor will be usable on Linux also.
    Now am working with Kubuntu 18.10 with Kde Plasma windows manager. I have 4 virtual desktops. Most of the time I am working only with first of them. But I can image to work with file manager on some other screen and I want to run my editor from file manager many times (intense working with source files in many languages). I do some basic tests and I am failed to find "right Qt way" to determine number of desktop on which my text editor is running. I try:
    1. Identify by screen pointer:
    Qt Code:
    1. MainWindow w;
    2. reinterpret_cast<quint64>(w.windowHandle()->screen()
    To copy to clipboard, switch view to plain text mode 
    Which is wrong because it is unique from instance to instance.
    2. Find siblings of current screen:
    Qt Code:
    1. QList<QScreen*> lScreens(w.windowHandle()->screen()->virtualSiblings());
    2. for(QScreen* lScreen : lScreens)
    3. qInfo() << "Detected screen: " << reinterpret_cast<quint64>(lScreen);
    To copy to clipboard, switch view to plain text mode 
    But it return only first screen (3 rest are ignored).
    3. Identify by primaryScreen pointer:
    Qt Code:
    1. qInfo() << "Detected screen: " << reinterpret_cast<quint64>(a.primaryScreen());
    To copy to clipboard, switch view to plain text mode 
    But it failed also (it is unique from instance to instance).
    4. QDesktopWidget::screenNumber()
    But it always return 0 (no mater what is current desktop).

    Do you have any other ideas?

  2. #2
    Join Date
    Jul 2012
    Posts
    244
    Thanks
    27
    Thanked 15 Times in 14 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to determine desktop number?!?

    Did you pass your text editors widget to QDesktopWidget::screenNumber(QWidget*) ? Show some code.

  3. #3
    Join Date
    Feb 2015
    Location
    Poland
    Posts
    34
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows Android

    Exclamation Re: How to determine desktop number?!?

    Qt Code:
    1. qInfo() << "Detected screen: " << QApplication::desktop()->screenNumber(&w);
    To copy to clipboard, switch view to plain text mode 
    Where "w" is MainWindow. It always return just:
    Qt Code:
    1. Detected screen: 0
    To copy to clipboard, switch view to plain text mode 
    More!
    Qt Code:
    1. qInfo() << "Screen count: " << QApplication::desktop()->screenCount();
    To copy to clipboard, switch view to plain text mode 
    Returns:
    Qt Code:
    1. Screen count: 1
    To copy to clipboard, switch view to plain text mode 


    Added after 1 29 minutes:


    I don't found Qt solution. Below is X11 specific solution.
    Note: God/right programing way is to encapsulate specific code in to easily identified blocks. So, I have Application class which handle local socket and translations. This X11 specific code I exclude in to Application.Linux.cpp (and another Application.Windwos.cpp - for similiar reasons) and in pro file I add conditional directives:

    Qt Code:
    1. linux: SOURCES += Src/Application.Linux.cpp
    2. win32: SOURCES += Src/Application.Windows.cpp
    To copy to clipboard, switch view to plain text mode 

    My Application.Linux.cpp is as follow:

    Qt Code:
    1. #include "Application.h"
    2. #include <X11/Xlib.h>
    3. #include <X11/Xos.h>
    4. #include <X11/Xlib.h>
    5. #include <X11/Xutil.h>
    6. #include <X11/Xatom.h>
    7.  
    8. quint32 Application::currentDesktopNumber()
    9. {
    10. Display* lDisplay = XOpenDisplay(nullptr);
    11. int lScreen = DefaultScreen(lDisplay);
    12. Window lRootWindow = RootWindow(lDisplay, lScreen);
    13. Atom lAtom(XInternAtom(lDisplay, "_NET_CURRENT_DESKTOP", True));
    14. if(lAtom == None)
    15. qFatal("no atom\n");
    16.  
    17. unsigned long nitems, leftover;
    18. unsigned char* lData = nullptr;
    19. int actual_format;
    20. Atom actual_type;
    21.  
    22. // if(XGetWindowProperty(lDisplay, lRootWindow, lAtom, 0L, 8, False, AnyPropertyType, nullptr, nullptr, nullptr, nullptr, &lData) != Success)
    23. if(XGetWindowProperty(lDisplay, lRootWindow, lAtom, 0L, 8, False, AnyPropertyType, &actual_type, &actual_format, &nitems, &leftover, &lData) != Success)
    24. qFatal("XGetWindowProperty failed!");
    25.  
    26. quint32 lResult = lData[0];
    27. delete [] lData;
    28.  
    29. return lResult;
    30. }
    31.  
    32. void Application::raiseAnotherInstance(quint32 /*aProcesId*/)
    33. {
    34. }
    To copy to clipboard, switch view to plain text mode 
    Solution inspired by: https://forums.gentoo.org/viewtopic-...6-start-0.html
    Last edited by Szyk; 18th November 2018 at 16:15.

  4. #4
    Join Date
    Jul 2012
    Posts
    244
    Thanks
    27
    Thanked 15 Times in 14 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to determine desktop number?!?

    Works perfectly fine for me.


    Qt Code:
    1. log(QString("screen nr: %1").arg(QApplication::desktop()->screenNumber(this)));
    To copy to clipboard, switch view to plain text mode 


    Put this in a button-click-handler of mainwindow, it outputs 0 or 1 depending on which screen I drag the window.

  5. #5
    Join Date
    Feb 2015
    Location
    Poland
    Posts
    34
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: How to determine desktop number?!?

    Great! But: What it will be happen when you create window on specific screen?!? You will get 0!

Similar Threads

  1. Determine Linux desktop version
    By RolandHughes in forum Qt Programming
    Replies: 3
    Last Post: 23rd June 2015, 20:05
  2. Replies: 1
    Last Post: 5th March 2012, 07:34
  3. Determine the Process ID
    By Jimmy2775 in forum General Programming
    Replies: 5
    Last Post: 7th October 2011, 10:16
  4. How to determine type of cd?
    By AlexanderPopov in forum Qt Programming
    Replies: 1
    Last Post: 26th May 2010, 10:20
  5. Replies: 5
    Last Post: 31st January 2009, 08: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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.