1

我想在我输入时将所有小写字符转换为QPlainTextEdit. 在QLineEdit我通过验证器做同样的事情,但似乎没有验证器QPlainTextEdit

我试过ui->pte_Route->setInputMethodHints(Qt::ImhUppercaseOnly);了,但它什么也没做,很可能是用错了。

使用我的“自己的”课程有什么更好的选择吗?

4

3 回答 3

1

使用事件过滤器的快速测试似乎工作得相当好......

class plain_text_edit: public QPlainTextEdit {
  using super = QPlainTextEdit;
public:
  explicit plain_text_edit (QWidget *parent = nullptr)
    : super(parent)
    {
      installEventFilter(this);
    }
protected:
  virtual bool eventFilter (QObject *obj, QEvent *event) override
    {
      if (event->type() == QEvent::KeyPress) {
        if (auto *e = dynamic_cast<QKeyEvent *>(event)) {

          /*
           * If QKeyEvent::text() returns an empty QString then let normal
           * processing proceed as it may be a control (e.g. cursor movement)
           * key.  Otherwise convert the text to upper case and insert it at
           * the current cursor position.
           */
          if (auto text = e->text(); !text.isEmpty()) {
            insertPlainText(text.toUpper());

            /*
             * return true to prevent further processing.
             */
            return true;
          }
        }
      }
      return super::eventFilter(obj, event);
    }

如果它确实工作得很好,那么事件过滤器代码总是可以单独拉出以重新使用。

于 2019-04-12T18:09:35.290 回答
1

为这样一个简单的任务使用事件过滤器看起来不是一个好主意,因为您被迫实现一个单独的继承 QPlainTextEdit 的类或创建一些单独的类作为过滤器。相反,您还可以执行以下操作:

// Note. This is just a sample. Assume that 'this' is context of some class (e.g. class implementing QDialog/QMainWindow)
auto lineEdit = new QLineEdit();
/*
Here, you can use also &QLineEdit::textChanged, and it would not cause any stackoverflow,
since Qt is pretty optimized here, i.e. if text does not change actually (value of QString
remains the same), Qt won't fire the signal. However, it is probably better to use
&QLineEdit::textEdited, since you expect the user to enter the text.
*/
connect(lineEdit, &QLineEdit::textEdited, this, [lineEdit](const QString& text)
{
    lineEdit->setText(text.toUpper());
});

换句话说,您可以通过 Qt 提供的简单信号和槽机制实现所需的相同行为。如果您可以通过标准框架机制实现您想要的,那么您应该尝试这样做,而不是尝试实现可能导致您甚至可能不知道的问题的事件过滤器。请记住,事件过滤器是 Qt 提供的另一种机制,它使您可以更自由地做您想做的事情,但您也必须处理更多的极端情况。

于 2019-04-15T15:19:50.473 回答
1

我遇到了 eventFilter 方法的问题,我使用了一个更简单的解决方案:

protected:
    void keyPressEvent(QKeyEvent* e) override {
        if (!e->text().isNull() && !e->text().isEmpty() &&
            e->modifiers() == Qt::NoModifier &&
            e->key() >= Qt::Key_A && e->key() <= Qt::Key_Z)
        {
            insertPlainText(e->text().toUpper());
        }
        else
            QPlainTextEdit::keyPressEvent(e);
    }

我正在使用继承自 QPlainTextEdit 的 Qt 示例中的 CodeEditor 类。

于 2021-06-28T12:33:22.553 回答