0

我有两个价值清单。第一个是整数列表,第二个是字符串列表。我想根据 lineEdit 的最后内容显示这些不同的列表。例如,当用户输入 A => QCompleter 时,会显示字符串列表。如果用户输入 A1 => QCompleter 显示整数列表,因为输入的最后一个字符是 1。

我尝试这样:

 void QtGuiApplication1::maFonction(QString text)

{

    QString lastCaractor = text.mid(text.length() - 1, 1);
    if (is_number(lastCaractor.toStdString()) == true)
    {
        QStringList list = { "1","2","7" };
        StringCompleter->setModel(new QStringListModel(list, StringCompleter));
        StringCompleter->setCompletionPrefix(lastCaractor);
    }

    else
    {
        QStringList list = { "AAAA","AA","BB","AC" };
        StringCompleter->setModel(new QStringListModel(list, StringCompleter));
        StringCompleter->setCompletionPrefix(lastCaractor);
    }
}

bool QtGuiApplication1::is_number(const std::string& s)
{

    std::string::const_iterator it = s.begin();
    while (it != s.end() && std::isdigit(*it)) ++it;
    return !s.empty() && it == s.end();
}

如果最后一个字符是数字 => QCompleter show me list = { "1","2","7" };

其他 => 列表 = { "AAAA","AA","BB","AC" };

4

1 回答 1

1

QLineEdit用 2 个海关子分类你的QCompleter

ExtendedLineEdit.h

class ExtendedLineEdit : public QLineEdit
{
    Q_OBJECT
public:
    explicit ExtendedLineEdit(QWidget *parent = nullptr);

    void setWordCompleter(QCompleter* c);
    void setNumberCompleter(QCompleter* c);

protected:
    void keyPressEvent(QKeyEvent *e);

private slots:
    void insertCompletionWord(const QString& txtComp);
    void insertCompletionNumber(const QString& txtComp);

private:
    QCompleter* m_completerWord;
    QCompleter* m_completerNumber;

    void showCustomCompleter(QCompleter* completer);
};

ExtendedLineEdit.cpp

ExtendedLineEdit::ExtendedLineEdit(QWidget *parent) :
    QLineEdit(parent)
{

}

void ExtendedLineEdit::setWordCompleter(QCompleter *c)
{
    m_completerWord = c;
    m_completerWord->setWidget(this);
    connect(m_completerWord, SIGNAL(activated(QString)),
            this, SLOT(insertCompletionWord(QString)));
}

void ExtendedLineEdit::setNumberCompleter(QCompleter *c)
{
    m_completerNumber = c;
    m_completerNumber->setWidget(this);
    connect(m_completerNumber, SIGNAL(activated(QString)),
            this, SLOT(insertCompletionNumber(QString)));
}

void ExtendedLineEdit::keyPressEvent(QKeyEvent *e)
{
    QLineEdit::keyPressEvent(e);
    if (!m_completerWord || !m_completerNumber)
        return;

    QString lastCaractor = text().mid(text().length() - 1, 1);
    bool okConvertion = false;
    lastCaractor.toInt(&okConvertion);
    if (okConvertion)
    {
        //show number completer
        m_completerWord->popup()->hide();

        m_completerNumber->setCompletionPrefix(lastCaractor);

        showCustomCompleter(m_completerNumber);
    }
    else
    {
        //show word completer
        m_completerNumber->popup()->hide();

        m_completerWord->setCompletionPrefix(this->text());

        showCustomCompleter(m_completerWord);
    }


}

void ExtendedLineEdit::insertCompletionWord(const QString &txtComp)
{
    setText(txtComp);
}

void ExtendedLineEdit::insertCompletionNumber(const QString &txtComp)
{
    setText(text().mid(0, text().length() - 1) + txtComp);
}

void ExtendedLineEdit::showCustomCompleter(QCompleter *completer)
{
    if (completer->completionPrefix().length() < 1)
    {
        completer->popup()->hide();
        return;
    }
    QRect cr = cursorRect();
    cr.setWidth(completer->popup()->sizeHintForColumn(0) + completer->popup()->verticalScrollBar()->sizeHint().width());
    completer->complete(cr);
}

并使用

QStringList list = { "AAAA","AA","BB","AC" };
m_wordCompleter = new QCompleter(list, this);
QStringList numbers = { "1","2","7", "15" };
m_numberCompleter = new QCompleter(numbers, this);

ui->lineEdit->setWordCompleter(m_wordCompleter);
ui->lineEdit->setNumberCompleter(m_numberCompleter);
于 2019-05-28T11:34:20.920 回答