在qt中获取标签的鼠标点击位置
我用谷歌搜索了一下,发现 这个论坛帖子,其中OP似乎遇到了我遇到的确切问题。问题是,我如何继承 QLabel 并重新实现 mousepressed 事件?我猜它会是这样的:
class CustomLabel : public QLabel
{
public:
//what about the constructors?
void mousePressEvent ( QMouseEvent * ev );
}
void CustomLabel::mousePressEvent ( QMouseEvent * ev )
{
QPoint = ev->pos();
//I want to have another function get the event position.
//How would I achieve this? It's void!
//Is there perhaps some way to set up a signal and slot with the position?
}
在我成功创建一个 CustomLabel
类之后,我如何才能将它放入设计视图中?
I googled around and found this forum thread in which the OP seems to have had the exact problem I am having. The question is, how would I inherit from QLabel
and reimplement the mousepressed event? I'm guessing it would be something like this:
class CustomLabel : public QLabel
{
public:
//what about the constructors?
void mousePressEvent ( QMouseEvent * ev );
}
void CustomLabel::mousePressEvent ( QMouseEvent * ev )
{
QPoint = ev->pos();
//I want to have another function get the event position.
//How would I achieve this? It's void!
//Is there perhaps some way to set up a signal and slot with the position?
}
And after I have successfully created a CustomLabel
class, how would I be able to put it in design view?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
是的,您可以在
CustomLabel
类上设置信号,并让覆盖版本的mousePressEvent
发出该信号。即构造函数只是模仿基本 QLabel 的构造函数,因此只需将它们的参数直接传递给相应的基本构造函数。
Yes, you can set up a signal on your
CustomLabel
class and have your overridden version ofmousePressEvent
emit it. i.e.The constructors just mimic those of the base
QLabel
and therefore simply pass their arguments straight on to the corresponding base constructors.就像这样:D
just like this :D
是我一个人的问题,还是
QMouseEvent
没有提供您需要的信息?参考:http://doc.qt.nokia.com/4.7.old /qmouseevent.html#x
Is it just me, or doesn't
QMouseEvent
already give the information you need?Ref: http://doc.qt.nokia.com/4.7.old/qmouseevent.html#x