Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Storing enum in a list?



  • I'm making a simple find and replace function for my text editor. And there are a few checkbox for the flnd flags like "match case" and "whole words". When the find button is clicked, the checkbox selections will be stored in a QMap<QString, bool> and send along with the search text to the find function.

    In the find function I will unpack the findFlags QMap and check their state to add flags like QTextDocument::FindCaseSensitively to the find function. I want to store these enum in something like a QStringList that at the end I can paste them in the find function in one go like this.

    editor->find(a_text, findFlagsList.join(" | "))
    
    // I want it to print out like this
    editor->find(a_text, QTextDocument::FindCaseSensitively | QTextDocument::FindWholeWords)
    

  • Lifetime Qt Champion

    Hi
    But why a string list ?
    You can just store them as QFlags<FindFlag>
    And give that directly to editor->find
    You can no do that with strings.



  • @mrjj

    Hi I tried QFlags<FindFlag> findFlags but I got the error unknown type name 'FindFlag'


  • Lifetime Qt Champion

    Hi
    Its

    QFlags<QTextDocument::FindFlag> findFlags;
    


  • @mrjj

    Hi how do I add QTextDocument::FindFlag enum to this variable? It only allows me to assign the flags in one time.

    findFlags = QTextDocument::FindCaseSensitively | QTextDocument::FindWholeWords;

    I want to do it like

    if (matchCaseChecked) {
        findFlags += QTextDocument::FindCaseSensitively;
    }
    
    if (wholeWordsChecked) {
        findFlags += QTextDocument::FindWholeWords;
    }
    

  • Lifetime Qt Champion

    @lansing
    Hi
    You can still just OR them together
    if (matchCaseChecked) {
    findFlags |= QTextDocument::FindCaseSensitively;
    }



  • @mrjj

    This is what I'm looking for, thank you.


Log in to reply