Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi.i have a class that contains some QPushbutton.I mean i have in constructor arrays of Qpushbutton.
<qpushbutton mode="hold">p=new QPushButton[20];>
i want if user clicked on any Button,it is pigmented.now i how to understand which button is clicked that i pigment it.(I know signal is clicked and how to Implement slot)
Posted

Let me restate a few things that I think that you said:

1. You know how to write a "slot"
2. You have an array of 20 buttons.

I assume that you already know how to connect your buttons to your slot that lets you know that a button was clicked. I assume that your real problem is that you have no idea what button was clicked. It looks like you are suggesting that you can use the mouse position to determine this. Off hand, I expect that the mouse location is not related at all, since I might hover the mouse over one button and then use the keyboard to cause a totally different button to be "clicked".

It turns out that QT has a calculator button that demonstrates how to solve this:

http://qt-project.org/doc/qt-4.8/widgets-calculator.html[^]

This contains the following line of code in the slot

Button *clickedButton = qobject_cast<Button *>(sender());


The descriptive text states the following:

Quote:
First, we find out which button sent the signal using QObject::sender(). This function returns the sender as a QObject pointer. Since we know that the sender is a Button object, we can safely cast the QObject. We could have used a C-style cast or a C++ static_cast<>(), but as a defensive programming technique we use a qobject_cast(). The advantage is that if the object has the wrong type, a null pointer is returned. Crashes due to null pointers are much easier to diagnose than crashes due to unsafe casts. Once we have the button, we extract the operator using QToolButton::text().
 
Share this answer
 
It is called event handling. Please, see how it to achieve that: http://stackoverflow.com/questions/6709392/event-handling-in-qt[^]
 
Share this answer
 
Comments
mary99 3-May-14 12:41pm    
i think use of mousevent.if this class has a method for returns position of button?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900