如何从点击事件中获取呼叫按钮

发布于 2024-10-20 12:52:45 字数 243 浏览 4 评论 0原文

我正在尝试制作一个小 GUI 来在我的设备上部署 .ear 和 .war 文件 本地玻璃鱼安装。所以我做了五行,其中包含 文件名字段、复选框和用于打开文件对话框的按钮 找到 war/ear 文件。如果所有按钮都能调用就太好了 相同的函数并从函数中找出五个中的哪一个 拨打电话的按钮(更新正确的文本字段)。不 知道这是否是以面向对象的方式执行此操作的预期方式 但我唯一的 GUI 编程经验是一些旧的 win16 事件 循环:)。

//BRG 安德斯·奥尔姆

I'm trying to make an small gui to deploy .ear and .war files on my
local glassfish installation. SO i have made five rows containing a
file name field, a checkbox and a button to bring up a file dialogbox
to locate the war/ear file. It would be nice to have all buttons call
the same function and from the function sort out which of the five
buttons who made the call ( to update the correct text fields ). Don't
know if this is the intended way of doing it in an object oriented way
but my only gui programming experience is some old win16 event
loops :).

//BRG Anders Olme

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

因为看清所以看轻 2024-10-27 12:52:45
QPushButton *buttonA = new QPushButton("A");
QPushButton *buttonB = new QPushButton("B");
QPushButton *buttonC = new QPushButton("C");

buttonA->setObjectName("A");
buttonB->setObjectName("B");
buttonC->setObjectName("C");

connect(buttonA, SIGNAL(clicked()), this, SLOT(testSlot()));
connect(buttonB, SIGNAL(clicked()), this, SLOT(testSlot()));
connect(buttonC, SIGNAL(clicked()), this, SLOT(testSlot()));

//Now in slot implementation
void QWidget::testSlot()
{
  QObject *senderObj = sender(); // This will give Sender object
  // This will give obejct name for above it will give "A", "B", "C"
  QString senderObjName = senderObj->objectName(); 

  if(senderObjName == "A")
  {
   //Implement Button A Specific 
  }
  //Similarly for "B" and "C"
  if(senderObjName == "B")
  {
   //Implement Button B Specific 
  }
  if(senderObjName == "C")
  {
   //Implement Button C Specific 
  }
}

我使用这种方法来实现这种情况,因为代码更具可读性,但随着字符串比较的到来,它可能会很耗时。
谢谢你!

QPushButton *buttonA = new QPushButton("A");
QPushButton *buttonB = new QPushButton("B");
QPushButton *buttonC = new QPushButton("C");

buttonA->setObjectName("A");
buttonB->setObjectName("B");
buttonC->setObjectName("C");

connect(buttonA, SIGNAL(clicked()), this, SLOT(testSlot()));
connect(buttonB, SIGNAL(clicked()), this, SLOT(testSlot()));
connect(buttonC, SIGNAL(clicked()), this, SLOT(testSlot()));

//Now in slot implementation
void QWidget::testSlot()
{
  QObject *senderObj = sender(); // This will give Sender object
  // This will give obejct name for above it will give "A", "B", "C"
  QString senderObjName = senderObj->objectName(); 

  if(senderObjName == "A")
  {
   //Implement Button A Specific 
  }
  //Similarly for "B" and "C"
  if(senderObjName == "B")
  {
   //Implement Button B Specific 
  }
  if(senderObjName == "C")
  {
   //Implement Button C Specific 
  }
}

I have used this method to implement such case because code is more readable but it may be time consuming as String comparison comes.
Thank you!

挽你眉间 2024-10-27 12:52:45

将每个按钮的 click() 信号与同一个插槽连接,并使用 QObject * QObject::sender () const [protected] 在此槽中查找哪个按钮发送了信号(被单击)。或者,您可以使用 QSignalMapper 这是一个特殊的类专为此类任务而设计。

Connect each button's click() signal with one and the same slot and use QObject * QObject::sender () const [protected] in this slot to find out which button sent the signal (was clicked). Alternatively you could use QSignalMapper which is a special class made just for this kind of task.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文