Results 1 to 15 of 15

Thread: Get pixel color using WINAPI not working in QT-QML app?

  1. #1
    Join Date
    Oct 2013
    Location
    VietNam
    Posts
    41
    Thanks
    3
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Windows Android

    Question Get pixel color using WINAPI not working in QT-QML app?

    Hi all!
    I created example to get pixel color using WINAPI. When I get full screen, it's work fine
    My source
    Qt Code:
    1. #ifndef GETCOLOR_H
    2. #define GETCOLOR_H
    3.  
    4. #include <QObject>
    5. #include <QDebug>
    6. #include <windows.h>
    7. #include <QPoint>
    8. #include <QString>
    9.  
    10. class getColor : public QObject
    11. {
    12. Q_OBJECT
    13. public:
    14. explicit getColor(QObject *parent = 0);
    15. Q_INVOKABLE void getMyPixel(int _x, int _y);
    16.  
    17. signals:
    18. void colorPixel(QString _color);
    19.  
    20. public slots:
    21. void posApplication(QPoint _pos);
    22. void posXApplication(int _posX);
    23. void posYApplication(int _posY);
    24. private:
    25. int posX;
    26. int posY;
    27. QString colorR;
    28. QString colorG;
    29. QString colorB;
    30. QString colorsend;
    31.  
    32.  
    33. };
    34.  
    35. #endif // GETCOLOR_H
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. #include "getcolor.h"
    2.  
    3. getColor::getColor(QObject *parent) :
    4. QObject(parent)
    5. {
    6. posX = posY = 0;
    7.  
    8. }
    9. void getColor::getMyPixel(int _x, int _y)
    10. {
    11. // HDC dc = GetDC(NULL);
    12. HDC dc = GetWindowDC(NULL);
    13. COLORREF color = GetPixel(dc, _x + posX, _y + posY);
    14.  
    15. int _red = GetRValue(color);
    16. int _green = GetGValue(color);
    17. int _blue = GetBValue(color);
    18. // qDebug()<< "red"<< _red << "green"<<_green << "blue" << _blue;
    19. ReleaseDC(NULL, dc);
    20. colorR = QString::number( _red, 16 );
    21. if(colorR.length() == 1) {
    22. colorR.insert(0,"0");
    23. }
    24. colorG = QString::number( _green, 16 );
    25. if(colorG.length() == 1) {
    26. colorG.insert(0,"0");
    27. }
    28. colorB = QString::number( _blue, 16 );
    29. if(colorB.length() == 1) {
    30. colorB.insert(0,"0");
    31. }
    32. colorsend = "#";
    33. colorsend.append(colorR).append(colorG).append(colorB);
    34. emit colorPixel(colorsend);
    35. }
    36.  
    37. void getColor::posApplication(QPoint _pos)
    38. {
    39. posX = _pos.x();
    40. posY = _pos.y();
    41. }
    42.  
    43. void getColor::posXApplication(int _posX)
    44. {
    45. posX = _posX;
    46. }
    47.  
    48. void getColor::posYApplication(int _posY)
    49. {
    50. posY = _posY;
    51. }
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. #include <QApplication>
    2. #include "qtquick2applicationviewer.h"
    3. #include <QQmlApplicationEngine>
    4. #include <QQmlApplicationEngine>
    5. #include <QQmlEngine>
    6. #include <QString>
    7. #include <QQmlContext> // for setContextProperty
    8. #include "getcolor.h"
    9. #include "qwindow.h"
    10.  
    11. int main(int argc, char *argv[])
    12. {
    13. QApplication app(argc, argv);
    14. getColor m_getColor;
    15. QWindow m_window;
    16.  
    17. QQmlApplicationEngine engine;
    18. QtQuick2ApplicationViewer viewer;
    19. viewer.setSource(QUrl("qrc:///main.qml"));
    20. viewer.rootContext()->setContextProperty("_getColor",&m_getColor);
    21. viewer.activeFocusItem();
    22. viewer.show();
    23.  
    24. m_getColor.posApplication(viewer.position());
    25. QObject::connect(&viewer,SIGNAL(xChanged(int)),&m_getColor,SLOT(posXApplication(int)));
    26. QObject::connect(&viewer,SIGNAL(yChanged(int)),&m_getColor,SLOT(posYApplication(int)));
    27. return app.exec();
    28. }
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. import QtQuick 2.3
    2. import QtQuick.Controls 1.2
    3.  
    4. Item {
    5. id: mainwindows
    6. visible: true
    7. width: 640
    8. height: 580
    9. property int r : 20
    10. property color colorPK: "white"
    11. Rectangle {
    12. width: 640
    13. height: 480
    14.  
    15. gradient: Gradient {
    16. GradientStop {
    17. position: 0.0
    18. id: id1
    19. SequentialAnimation on color {
    20. id: update1
    21. loops: Animation.Infinite
    22. ColorAnimation { from: "red"; to: "magenta"; duration: 1000 }
    23. ColorAnimation { from: "magenta"; to: "blue"; duration: 1000 }
    24. ColorAnimation { from: "blue"; to: "cyan"; duration: 1000 }
    25. ColorAnimation { from: "cyan"; to: "lime"; duration: 1000 }
    26. ColorAnimation { from: "lime"; to: "yellow"; duration: 1000 }
    27. ColorAnimation { from: "yellow"; to: "red"; duration: 1000 }
    28. }
    29. }
    30. GradientStop {
    31. position: 1
    32. id: id2
    33. SequentialAnimation on color {
    34. id: update2
    35. loops: Animation.Infinite
    36. ColorAnimation { from: "yellow"; to: "red"; duration: 1000 }
    37. ColorAnimation { from: "red"; to: "magenta"; duration: 1000 }
    38. ColorAnimation { from: "magenta"; to: "blue"; duration: 1000 }
    39. ColorAnimation { from: "blue"; to: "cyan"; duration: 1000 }
    40. ColorAnimation { from: "cyan"; to: "lime"; duration: 1000 }
    41. ColorAnimation { from: "lime"; to: "yellow"; duration: 1000 }
    42. }
    43.  
    44. }
    45. }
    46.  
    47. }
    48. Rectangle {
    49. id: pickerCursor
    50. x: r; y: r
    51. width: r*2; height: r*2
    52. radius: r
    53. border.color: "black"; border.width: 2
    54. color: "transparent"
    55. Rectangle {
    56. anchors.fill: parent; anchors.margins: 2;
    57. border.color: "white"; border.width: 1
    58. radius: width/2
    59. color: "transparent"
    60. }
    61. MouseArea{
    62. anchors.fill: parent
    63. id: dragConsolWindowAreaa
    64. drag.target: parent
    65. drag.minimumX: 0
    66. drag.maximumX: mainwindows.width - pickerCursor.width
    67. drag.minimumY: 0
    68. drag.maximumY: mainwindows.height - pickerCursor.height
    69. onPositionChanged: {
    70.  
    71. }
    72. }
    73. }
    74. Rectangle{
    75. id: colorReciver
    76. anchors.bottom: parent.bottom; anchors.bottomMargin: 0
    77. anchors.horizontalCenter: parent.horizontalCenter
    78. width: 100
    79. height: 100
    80. border.color: "black"
    81. color: colorPK
    82. }
    83.  
    84. Connections{
    85. target: _getColor
    86. onColorPixel:{
    87. colorPK = _color
    88. console.log(_color)
    89. }
    90. }
    91.  
    92. Timer{
    93. interval: 30
    94. repeat: true
    95. running: true
    96. onTriggered: {
    97. _getColor.getMyPixel(pickerCursor.x + pickerCursor.width / 2,pickerCursor.y + pickerCursor.height / 2)
    98. }
    99. }
    100. }
    To copy to clipboard, switch view to plain text mode 
    But when I get on QT-QML app, it's not work
    code edit
    Qt Code:
    1. #include "getcolor.h"
    2.  
    3. getColor::getColor(QObject *parent) :
    4. QObject(parent)
    5. {
    6. posX = posY = 0;
    7.  
    8. }
    9. void getColor::getMyPixel(int _x, int _y)
    10. {
    11. // HDC dc = GetDC(NULL);
    12. HWND name = FindWindowA(0,"GetpixelColor");
    13. HDC dc = GetWindowDC(name);
    14. COLORREF color = GetPixel(dc, _x, _y);
    15.  
    16. int _red = GetRValue(color);
    17. int _green = GetGValue(color);
    18. int _blue = GetBValue(color);
    19. // qDebug()<< "red"<< _red << "green"<<_green << "blue" << _blue;
    20. ReleaseDC(NULL, dc);
    21. colorR = QString::number( _red, 16 );
    22. if(colorR.length() == 1) {
    23. colorR.insert(0,"0");
    24. }
    25. colorG = QString::number( _green, 16 );
    26. if(colorG.length() == 1) {
    27. colorG.insert(0,"0");
    28. }
    29. colorB = QString::number( _blue, 16 );
    30. if(colorB.length() == 1) {
    31. colorB.insert(0,"0");
    32. }
    33. colorsend = "#";
    34. colorsend.append(colorR).append(colorG).append(colorB);
    35. emit colorPixel(colorsend);
    36. }
    37.  
    38. void getColor::posApplication(QPoint _pos)
    39. {
    40. posX = _pos.x();
    41. posY = _pos.y();
    42. }
    43.  
    44. void getColor::posXApplication(int _posX)
    45. {
    46. posX = _posX;
    47. }
    48.  
    49. void getColor::posYApplication(int _posY)
    50. {
    51. posY = _posY;
    52. }
    To copy to clipboard, switch view to plain text mode 
    I try get color in another app, it's work very fine, but only do not work in QT - QML app. I don't know why?
    Please help me to solve this problem.
    Thank you for reading!
    My project!
    https://www.dropbox.com/s/554no9q9eg...Color.rar?dl=0

  2. #2
    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: Get pixel color using WINAPI not working in QT-QML app?

    Isn't the first application, which you say works, also a QtQuick application?

    Cheers,
    _

  3. #3
    Join Date
    Oct 2013
    Location
    VietNam
    Posts
    41
    Thanks
    3
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Windows Android

    Default Re: Get pixel color using WINAPI not working in QT-QML app?

    Hi anda_skoa!
    when I get full screen to get color. It's work fine.
    HDC dc = GetWindowDC(NULL);
    COLORREF color = GetPixel(dc, _x + posX, _y + posY);
    when I changed code, I do not work
    HWND name = FindWindowA(0,"GetpixelColor");
    HDC dc = GetWindowDC(name);
    COLORREF color = GetPixel(dc, _x, _y);
    After that I changed "GetpixelColor" by another applycation different QtQuick application . It work fine!. It only do not work with QtQuick application.

  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: Get pixel color using WINAPI not working in QT-QML app?

    Does it work for other non-fullscreen OpenGL windows?
    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
    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: Get pixel color using WINAPI not working in QT-QML app?

    Quote Originally Posted by tanthinh1510 View Post
    Hi anda_skoa!
    when I get full screen to get color. It's work fine.

    when I changed code, I do not work
    Then why change the code?

    Have you tried using your working code and just translating the postions into global coordinates using QWindow::mapToGlobal()?

    Cheers,
    _

  6. #6
    Join Date
    Oct 2013
    Location
    VietNam
    Posts
    41
    Thanks
    3
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Windows Android

    Default Re: Get pixel color using WINAPI not working in QT-QML app?

    Hi wysota!
    I don't know whether or not OpenGL windows. but I try QT-GUI and some app non-fullscreen, it's work.
    It only do not work with QtQuick application.
    How I can solve it?


    Added after 5 minutes:


    Hi anda_skoa!
    Then why change the code?
    because I only want get color inside app!
    Last edited by tanthinh1510; 14th March 2015 at 10:18.

  7. #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: Get pixel color using WINAPI not working in QT-QML app?

    Quote Originally Posted by tanthinh1510 View Post
    I don't know whether or not OpenGL windows.
    So check that because QtQuick uses OpenGL for rendering.
    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.


  8. #8
    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: Get pixel color using WINAPI not working in QT-QML app?

    Quote Originally Posted by tanthinh1510 View Post
    because I only want get color inside app!
    Sure, that's where the position mapping comes in.
    You have a point in your window, you map it to global coordinates and then use the code that is known to work with fullscreen coordinates.

    Cheers,
    _

  9. #9
    Join Date
    Oct 2013
    Location
    VietNam
    Posts
    41
    Thanks
    3
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Windows Android

    Default Re: Get pixel color using WINAPI not working in QT-QML app?

    Quote Originally Posted by anda_skoa View Post
    Sure, that's where the position mapping comes in.
    You have a point in your window, you map it to global coordinates and then use the code that is known to work with fullscreen coordinates.

    Cheers,
    _
    When I get fullscreen and translating the postions to point I waint get. it's work fine
    But have a problem when I minimize app or open another app overlaps my application.It's not working correct.
    It's will get color of screen rather my app.

  10. #10
    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: Get pixel color using WINAPI not working in QT-QML app?

    Have you tried using the window's native handle instead of FindWindowA()?

    Also, as wysota suggested twice, make sure your code works with non-fullscreen OpenGL windows in general.

    Cheers,
    _

  11. #11
    Join Date
    Oct 2013
    Location
    VietNam
    Posts
    41
    Thanks
    3
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Windows Android

    Default Re: Get pixel color using WINAPI not working in QT-QML app?

    Quote Originally Posted by anda_skoa View Post
    Have you tried using the window's native handle instead of FindWindowA()?

    Also, as wysota suggested twice, make sure your code works with non-fullscreen OpenGL windows in general.

    Cheers,
    _
    Yes! I tried using the window's native handle instead of FindWindowA().
    Code works with non-fullscreen window but not works with non-fullscreen OpenGL windows in general.
    Maybe it's not support?

  12. #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: Get pixel color using WINAPI not working in QT-QML app?

    The code would have to read the color from your graphics card so I'm not suprised this doesn't work. What you could do is that you could render the scene to an FBO, download the texture as image and read the interesting pixel directly from the image using QImage::pixel().
    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.


  13. #13
    Join Date
    Oct 2013
    Location
    VietNam
    Posts
    41
    Thanks
    3
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Windows Android

    Default Re: Get pixel color using WINAPI not working in QT-QML app?

    Quote Originally Posted by wysota View Post
    So check that because QtQuick uses OpenGL for rendering.
    If I build in Qt 5.0 and earlier, QtQuick uses OpenGL for rendering it?

  14. #14
    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: Get pixel color using WINAPI not working in QT-QML app?

    Yes and no.
    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.


  15. #15
    Join Date
    Oct 2013
    Location
    VietNam
    Posts
    41
    Thanks
    3
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Windows Android

    Default Re: Get pixel color using WINAPI not working in QT-QML app?

    I build with MSVC2013 kit and it work OK Many thanks!

Similar Threads

  1. How to get color pixel in QML?
    By tanthinh1510 in forum Qt Quick
    Replies: 9
    Last Post: 29th October 2014, 06:31
  2. How to get color of pixel or point?
    By qt_developer in forum Newbie
    Replies: 2
    Last Post: 28th June 2012, 19:14
  3. How to get pixel color from QPainter ??
    By rameshg87 in forum Qt Programming
    Replies: 1
    Last Post: 10th August 2008, 08:58
  4. getting color from a pixel on canvas
    By teeshift in forum Qt Programming
    Replies: 1
    Last Post: 9th January 2007, 04:12
  5. How to get color of pixel or point by QMouseEvent
    By Krishnacins in forum Newbie
    Replies: 4
    Last Post: 28th May 2006, 01:46

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.