c++ - 如何使用 QWhatsThis 在 Qt 中启用动态 whatsthis-string?

标签 c++ qt

为了让 Qt 中的小部件具有动态的 what's this string,以下几乎可以工作(遵循文档 http://doc.qt.io/qt-5/qtwidgets-widgets-tooltips-example.html):

class MyEdit : public QLineEdit {
  Q_OBJECT
public:
  bool event(QEvent*e) {
    if (e && e->type() == QEvent::WhatsThis) {
      if (QHelpEvent *helpEvent = reinterpret_cast<QHelpEvent *>(e)) {
       QWhatsThis::showText(helpEvent->globalPos(), "My text...");
       return true;
      }
    }
    return QLineEdit::event(e);
  }
};

激活 what's this for the window 并点击 widget 显示“My text”(实际文本更复杂)。

问题:

  • 激活 what's this for the window 并将悬停在此小部件上显示死光标
  • Shift-F1 在小部件内不起作用。

第一个问题可以通过使用非空字符串调用 setWhatsThis("Dummy text"); 来解决,但感觉就像是 hack 并且此小部件中的 Shift-F1 显示“Dummy文本”。

是否有一种非破解的方式来处理它 - 特别是这样它就不会被更新破坏?

最佳答案

如果其他人偶然发现这个问题,以下似乎可以处理 Shift-F1:

class MyEdit : public QLineEdit {
  Q_OBJECT
public:
  bool event(QEvent*e) {
    if (e && e->type() == QEvent::WhatsThis) {
      if (QHelpEvent *helpEvent = reinterpret_cast<QHelpEvent *>(e)) {
       QWhatsThis::showText(helpEvent->globalPos(), "My text...");
       return true;
      }
    }
    if (e && e->type() == QEvent::KeyPress) {
      if (QKeyEvent *qk = reinterpret_cast<QKeyEvent *>(e)) {
        if (qk->key() == Qt::Key_F1 && (qk->modifiers()&Qt::ShiftModifier)) {
          QWhatsThis::showText(w.mapToGlobal(w.inputMethodQuery(Qt::ImCursorRectangle).toRect().center()), "My text...");
          qk->accept();
          return true;
        }
      }
    }
    return QLineEdit::event(e);
  }
};

并结合调用 setWhatsThis("Dummy text"); 也是悬停部分。然而,这不是一个优雅的解决方案。

关于c++ - 如何使用 QWhatsThis 在 Qt 中启用动态 whatsthis-string?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45084722/

相关文章:

c++ - 将指向成员函数的指针转换为 intptr_t

c++ - 使用 std::uniform_int_distribution 并稍后定义其范围

c++ - 解决堆栈溢出错误的问题

c++ - 如何使用 MFC 将记录插入到 Microsoft Access 中?

c++ - 在屏幕右侧显示主窗口(Qt 5.1.0)

c++ - 在 Qt 中连接一个按钮

c++ - 已填充 QSet 作为可选参数

c++ - 异常消息为空

c++ - 执行示例项目时出现 libvlc-qt 错误

c++ - 无法使用 QStandardPaths 打开文件