QLineEdit setSelection不起作用

时间:2014-02-18 11:11:26

标签: qt paint qlineedit

我为时间码创建了一个自定义QLineEdit(时间码如下所示: hh:mm:ss:ff )版本。

它与键盘和鼠标反应。如果用户使用鼠标编辑时间码(向上/向下拖动几个数字),则突出显示错误:它选择从光标到结尾的所有字符。 (即:如果我拖动 mm ,选择将是 mm:ss:ff )。

为了摆脱这种情况,我使用setSelection(x,2)选择了只需要的数字(使用qDebug() << selectedText()进行验证),但突出显示仍然是错误的:enter image description here

#include "PhTimecodeEdit.h"

PhTimeCodeEdit::PhTimeCodeEdit(QWidget *parent) :
    QLineEdit(parent),
    _tcType(PhTimeCodeType25)
{
    connect(this, SIGNAL(textChanged(QString)), this, SLOT(onTextChanged(QString)));
    this->installEventFilter(this);
    _mousePressed = false;
    _selectedIndex = 0;
}

bool PhTimeCodeEdit::isTimeCode()
{
    PhFrame frame;
    QString text;
    frame = PhTimeCode::frameFromString(this->text(),_tcType);
    text = PhTimeCode::stringFromFrame(frame, _tcType);

    if(text == this->text())
        return true;
    else
        return false;
}
bool PhTimeCodeEdit::eventFilter(QObject *, QEvent *event)
{
    switch (event->type()) {
    case QEvent::MouseButtonPress:
        _mousePressed = true;
        _mousePressedLocation = static_cast<QMouseEvent *>(event)->pos();
        if(_mousePressedLocation.x() > 110 and _mousePressedLocation.x() < 145) {
            _selectedIndex = 0;
        }
        else if(_mousePressedLocation.x() > 145 and _mousePressedLocation.x() < 190) {
            _selectedIndex = 3;
        }
        else if(_mousePressedLocation.x() > 190 and _mousePressedLocation.x() < 230) {
            _selectedIndex = 6;
        }
        else if(_mousePressedLocation.x() > 230 and _mousePressedLocation.x() < 270) {
            _selectedIndex = 9;
        }
        return true;
    case QEvent::MouseButtonRelease:
        _mousePressed = false;
        return true;
    case QEvent::MouseMove:
        {
            if(_mousePressed) {
                int y = static_cast<QMouseEvent *>(event)->pos().y();
                PhFrame currentFrame = PhTimeCode::frameFromString(this->text(), _tcType);

                if(_selectedIndex == 0) {
                    if(_mousePressedLocation.y() > y)
                        currentFrame += 25 * 60 * 60;
                    else
                        currentFrame -= 25 * 60 * 60;
                }
                else if(_selectedIndex == 3) {
                    if(_mousePressedLocation.y() > y)
                        currentFrame += 25 * 60;
                    else
                        currentFrame -= 25 * 60;
                }
                else if(_selectedIndex == 6) {
                    if(_mousePressedLocation.y() > y)
                        currentFrame += 25;
                    else
                        currentFrame -= 25;
                }
                else if(_selectedIndex == 9) {
                    if(_mousePressedLocation.y() > y)
                        currentFrame++;
                    else
                        currentFrame--;
                }

                _mousePressedLocation.setY(y);
                this->setText(PhTimeCode::stringFromFrame(currentFrame, _tcType));
                            setSelection(_selectedIndex,2);
            }
            return false;
        }

    default:
        return false;
    }
}

我该怎么做才能做到正确?

1 个答案:

答案 0 :(得分:1)

我不确定,但你的eventFilter很奇怪。它与QLineEdit的鼠标处理冲突。

  1. 在默认部分尝试return QLineEdit::eventFilter();
  2. 您没有实现鼠标跟踪。您应该通过鼠标捕获和重载mouse*Event();来禁用它。手动跟踪鼠标时,应禁用鼠标事件处理。
  3. 错误原因:由QLineEdit处理的鼠标移动会覆盖您在eventFilter中设置的文本选择。它发生在鼠标释放上。

    可能的修补程序(脏):为集合选择实现自己的插槽,并通过Qt :: QueuedConnection调用它。因此,在释放鼠标后将直接调用setSelection。