QPlainTextEdit - 更改移位和返回行为

发布于 2024-11-18 12:14:49 字数 280 浏览 0 评论 0原文

我使用 QPlainTextEdit 作为代码编辑器,它还显示行号。 但是,当我按 shift+return a 时,编辑器会中断,但行号不会增加。

我认为在 html 中它只是一个
而不是一个新的

标签...

看看屏幕截图...

看看截图

I use a QPlainTextEdit for a code editor that also shows line numbers.
But when I press shift+return a the editor makes a break, but the line number don't increases.

I think in html it would just be a <br/> instead of a new <p> tag...

Have a look at the screenshot...

Have a look at the screenshot

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

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

发布评论

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

评论(3

行至春深 2024-11-25 12:14:49

您可能应该使用 QTextEdit,因为我们正在讨论的是富文本。

覆盖virtual void keyPressEvent (QKeyEvent * e)。您可以在实现中调用QTextEdit::keyPressEvent来委托非特殊情况。

You should probably be using QTextEdit since this is rich text we're talking about.

Override virtual void keyPressEvent ( QKeyEvent * e ). You can call QTextEdit::keyPressEvent in the implementation to delegate non-special cases.

对你的占有欲 2024-11-25 12:14:49

实际上,您可以将对象与 eventFilter 和 installEventFilter 函数一起使用。

#ifndef SHIFTENTERFILTER_H
#define SHIFTENTERFILTER_H

#include <QObject>
#include <QEvent>
#include <QKeyEvent>

class ShiftEnterFilter : public QObject
{
    protected:
        virtual bool eventFilter(QObject *, QEvent *event) {
            if(event -> type() == QEvent::KeyPress)
            {
                QKeyEvent *keyEvent = static_cast <QKeyEvent> (event);

                if((keyEvent -> modifiers() & Qt::ShiftModifier) && ((keyEvent -> key() == Qt::Key_Enter) || (keyEvent -> key() == Qt::Key_Return)))
                    return true;
            }

            return false;
        }      
    public:
        ShiftEnterFilter(QObject *parent = 0) : QObject(parent) {}
};  

#endif 

只需将此过滤器安装到您的 QPlainTextEdit

// code
ui -> plainTextEdit -> installEventFilter(new ShiftEnterFilter(this));
// code

You can, actually, use object with eventFilter and installEventFilter function.

#ifndef SHIFTENTERFILTER_H
#define SHIFTENTERFILTER_H

#include <QObject>
#include <QEvent>
#include <QKeyEvent>

class ShiftEnterFilter : public QObject
{
    protected:
        virtual bool eventFilter(QObject *, QEvent *event) {
            if(event -> type() == QEvent::KeyPress)
            {
                QKeyEvent *keyEvent = static_cast <QKeyEvent> (event);

                if((keyEvent -> modifiers() & Qt::ShiftModifier) && ((keyEvent -> key() == Qt::Key_Enter) || (keyEvent -> key() == Qt::Key_Return)))
                    return true;
            }

            return false;
        }      
    public:
        ShiftEnterFilter(QObject *parent = 0) : QObject(parent) {}
};  

#endif 

Just install this filter to your QPlainTextEdit

// code
ui -> plainTextEdit -> installEventFilter(new ShiftEnterFilter(this));
// code
葬花如无物 2024-11-25 12:14:49

试试这个(CodeEdit继承QPlainTextEdit):

/**
 * override keyPressEvent, change behaviour of shift + enter(return)
 * @brief CodeEdit::keyPressEvent
 * @param event
 */
void CodeEdit::keyPressEvent(QKeyEvent *event)
{
    QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);

    // disable shift + enter(return)
    if ((keyEvent->modifiers() & Qt::ShiftModifier) && (keyEvent->key() == Qt::Key_Enter || keyEvent->key() == Qt::Key_Return)) {
        event->ignore();
        return;
    }
    QPlainTextEdit::keyPressEvent(event);
}

Try this (CodeEdit inherits QPlainTextEdit):

/**
 * override keyPressEvent, change behaviour of shift + enter(return)
 * @brief CodeEdit::keyPressEvent
 * @param event
 */
void CodeEdit::keyPressEvent(QKeyEvent *event)
{
    QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);

    // disable shift + enter(return)
    if ((keyEvent->modifiers() & Qt::ShiftModifier) && (keyEvent->key() == Qt::Key_Enter || keyEvent->key() == Qt::Key_Return)) {
        event->ignore();
        return;
    }
    QPlainTextEdit::keyPressEvent(event);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文