如何将几个按钮连接到Qt中的插槽? [英] how to connect several button to a slot in Qt ?

查看:126
本文介绍了如何将几个按钮连接到Qt中的插槽?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

hi.i有一个包含一些QPushbutton的类。我的意思是我在Qpushbutton的构造函数数组中。

< qpushbutton mode =hold> p = new QPushButton [20]; >

i想要用户点击任何按钮,它是有色的。现在我如何理解点击哪个按钮我颜色。(我知道信号被点击以及如何实现插槽)

解决方案

让我重申一些我认为你说过的话:



1.你知道怎么做写一个槽

2.你有一个20个按钮的数组。



我假设你已经知道如何连接你的按钮到你的插槽,让你知道点击了一个按钮。我认为你真正的问题是你不知道点击了什么按钮。看起来您建议您可以使用鼠标位置来确定这一点。在手边,我希望鼠标的位置根本不相关,因为我可能将鼠标悬停在一个按钮上,然后使用键盘使完全不同的按钮被点击。



事实证明,QT有一个计算器按钮,演示如何解决这个问题:



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



这包含插槽中的以下代码行



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





描述性文字说明如下:



Quote:

首先,我们找出哪个按钮使用QObject :: sender()发送信号。此函数将发送方作为QObject指针返回。由于我们知道发送者是Button对象,因此我们可以安全地转换QObject。我们可以使用C风格的演员表或C ++ static_cast<>(),但作为一种防御性编程技术,我们使用qobject_cast()。优点是如果对象的类型错误,则返回空指针。由于不安全的强制转换,由空指针引起的崩溃比崩溃更容易诊断。一旦我们有了按钮,我们使用QToolButton :: text()提取运算符。


它被称为事件处理。请看看如何实现这一目标: http://stackoverflow.com/questions/6709392/event-handling -in-qt [ ^

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)

解决方案

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().


It is called event handling. Please, see how it to achieve that: http://stackoverflow.com/questions/6709392/event-handling-in-qt[^]


这篇关于如何将几个按钮连接到Qt中的插槽?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆