Results 1 to 8 of 8

Thread: How to Click on a Pixmap?

  1. #1
    Join Date
    Sep 2013
    Posts
    20
    Qt products
    Qt5
    Platforms
    Windows
    Thanks
    7

    Default How to Click on a Pixmap?

    I recently found a way to highlight a pixmap, now I need to highlight while the mouse cursor is positioned on it. Also, I need to click on these pixmap images.
    My problem is that I have no idea how to do this. Some people suggest to subclass QLabel. If someone could lead me to the right direction please.

    Here is my code

    Qt Code:
    1. #include <QtWidgets>
    2. #include <string>
    3.  
    4. using std::string;
    5.  
    6. #include "canvas.h"
    7. #include "player.h"
    8. #include "computer.h"
    9.  
    10. Canvas::Canvas(QWidget *parent) : QWidget(parent)
    11. {
    12. cardTable = new QListView;
    13.  
    14. startButton = new QPushButton("Start");
    15. startButton->setFixedWidth(100);
    16.  
    17. connect(startButton, SIGNAL(clicked()), this, SLOT(startClicked()));
    18.  
    19. QVBoxLayout *verticalLayout = new QVBoxLayout;
    20. verticalLayout->addWidget(cardTable);
    21. verticalLayout->addWidget(startButton);
    22.  
    23. QGridLayout *mainLayout = new QGridLayout;
    24. mainLayout->addLayout(verticalLayout, 0, 0, 1, 1);
    25.  
    26. setLayout(mainLayout);
    27.  
    28. setUpComputerIcons();
    29. setUpDeckIcons();
    30.  
    31. myGame = new Game(this);
    32. }
    33.  
    34. void Canvas::setUpPlayerIcons(string *pString)
    35. {
    36. QPixmap qpx;
    37. QSize iconSize;
    38. string str;
    39.  
    40. for (int i = 0; i < MAXCARDS; i++)
    41. {
    42. shadow = new QGraphicsDropShadowEffect(this);
    43. shadow->setBlurRadius(10);
    44.  
    45. cardIconPlayer[i] = new QLabel(cardTable);
    46. str = pString[i];
    47. qpx = QPixmap(str.c_str());
    48. iconSize = qpx.size();
    49. iconSize.scale(190, 276, Qt::KeepAspectRatio);
    50. QPixmap scaledImage = qpx.scaled(iconSize, Qt::KeepAspectRatio, Qt::SmoothTransformation);
    51. cardIconPlayer[i]->setPixmap(scaledImage);
    52. cardIconPlayer[i]->move(20 + 60 * i, 350);
    53. cardIconPlayer[i]->setGraphicsEffect(shadow);
    54.  
    55. playerPixmaps.push_back(scaledImage);
    56. }
    57.  
    58. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Sep 2008
    Location
    Bangalore
    Posts
    659
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    116
    Thanked 42 Times in 41 Posts

    Default Re: How to Click on a Pixmap?

    reimplement hoverEnterEvent( http://harmattan-dev.nokia.com/docs/...overEnterEvent of Canvas class for highlighting mouse pos

    and for mousePress reimplement mousePressEvent
    http://harmattan-dev.nokia.com/docs/...ousePressEvent

    and dont forget to use setAcceptHoverEvents
    "Behind every great fortune lies a crime" - Balzac

  3. #3
    Join Date
    Sep 2013
    Posts
    20
    Qt products
    Qt5
    Platforms
    Windows
    Thanks
    7

    Default Re: How to Click on a Pixmap?

    How would I do that, my problem is that I don't know how to use event handlers. Do I have to make a different class for event handlers?

    How would I do that, my problem is that I don't know how to use event handlers. Do I have to make a different class for event handlers?

  4. #4
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android
    Thanked 342 Times in 324 Posts

    Default Re: How to Click on a Pixmap?


  5. #5
    Join Date
    Sep 2013
    Posts
    20
    Qt products
    Qt5
    Platforms
    Windows
    Thanks
    7

    Default Re: How to Click on a Pixmap?

    Do I still have to use connect, signals, and slots to emit these functions?

  6. #6
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android
    Thanked 342 Times in 324 Posts

    Default Re: How to Click on a Pixmap?

    No, you don't "emit" those functions. They will be called automatically in response to events if you subclass the widget correctly.
    There is nothing special going on, if you know how inheritance and virtual functions work, then just apply what you know to Qt classes here.

  7. The following user says thank you to stampede for this useful post:

    LaTj (14th November 2013)

  8. #7
    Join Date
    Sep 2008
    Location
    Bangalore
    Posts
    659
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    116
    Thanked 42 Times in 41 Posts

    Default Re: How to Click on a Pixmap?

    look the basic example http://harmattan-dev.nokia.com/docs/...apedclock.html .. check just how they enable mousePress and mouseMove ..
    "Behind every great fortune lies a crime" - Balzac

  9. The following user says thank you to wagmare for this useful post:

    LaTj (14th November 2013)

  10. #8
    Join Date
    Sep 2013
    Posts
    20
    Qt products
    Qt5
    Platforms
    Windows
    Thanks
    7

    Default Re: How to Click on a Pixmap?

    Thanks for the example and the advice. It takes me awhile since I don't work on this everyday.

Similar Threads

  1. Telling apart single click and double click in QTableView
    By jgirlich in forum Qt Programming
    Replies: 1
    Last Post: 5th March 2013, 15:27
  2. Replies: 2
    Last Post: 16th July 2012, 13:40
  3. Replies: 1
    Last Post: 19th April 2011, 12:17
  4. Replies: 6
    Last Post: 5th June 2009, 10:38
  5. Replies: 2
    Last Post: 12th January 2009, 00:24

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.