Is there any QCharList to use



  • I'm using a QStringList named texts for my program. What I need is characters. So I do,

    QChar ch = texts[0];
    

    I don't know it works well or not. The program is not well enough.

    All entries of texts are characters and I have used them as strings like: "c", "+" and so on.
    Don't we have some QCharList or whatsoever to use for characters in Qt please?


  • Moderators

    A unicode character in Qt is represented by QChar.
    A container of such characters is QString.
    A container of such containers is QStringList.

    So if texts is a QStringList then texts[0] is the first QString in it and texts[0][0] is the first QChar in the first QString in the QStringList.



  • @Chris-Kawa
    These are my characters:

    QStringList texts;
       texts << "C" << "S" << "^" << "*" << "("
              << "7" << "8" << "9" << "-" << ")" << "4"
               << "5" << "6" << "+" << "%" << "1" << "2"
                << "3" << "/" << "." << "0" << "=" ;
    

    Here is a part of My_First_Calculator.cpp file:

    ....
    for(int i=0; i<texts.size(); ++i)
         {
            QPushButton* button = new QPushButton(texts[i]);
    
            connect(button, SIGNAL(clicked(bool)),
                    signalMapper, SLOT(map()));
    
            signalMapper -> setMapping(button, texts[i]);
            gridLayout -> addWidget(button, i/5, i%5);
         }
    
        connect(signalMapper, SIGNAL(mapped(QString)), this, SLOT(get(QString)));
    ....
    

    And it is get(const QString& ):

    void My_First_Calculator::get(const QString& text)
    {
        QChar _ch = text[0];
    
            if (_ch == 'C') {
    ....
    

    Is there any problem?


  • Moderators

    @tomy said in Is there any QCharList to use:

    Is there any problem?

    Not that I can see, but your comment about the first code snippet "these are my characters" is not really true. These are not characters. These are strings consisting of one character, but they are of type QString (well.. converted to QString anyway), not QChar i.e. "C" is a string and 'C' is the first and only character of that string (not counting the null terminator).



  • @Chris-Kawa said in Is there any QCharList to use:

    @tomy said in Is there any QCharList to use:

    Is there any problem?

    Not that I can see, but your comment about the first code snippet "these are my characters" is not really true. These are not characters. These are strings consisting of one character, but they are of type QString (well.. converted to QString anyway), not QChar i.e. "C" is a string and 'C' is the first and only character of that string (not counting the null terminator).

    I would know these and thank you.

    @Chris-Kawa

    A unicode character in Qt is represented by QChar.
    A container of such characters is QString.

    All my characters are in ASCII type. So can I simply use a QString instead of a QStringList as follows please:

    QString texts;
       texts = "CS^*(789-)456+%123/.0=" ;
    

    Then the codes would be:

    ....
    for(int i=0; i<texts.size(); ++i)
         {
            QPushButton* button = new QPushButton(texts[i]);
    
            connect(button, SIGNAL(clicked(bool)),
                    signalMapper, SLOT(map()));
    
            signalMapper -> setMapping(button, texts[i]);
            gridLayout -> addWidget(button, i/5, i%5);
         }
    
        connect(signalMapper, SIGNAL(mapped(QString)), this, SLOT(get(QChar)));
    ....
    

    And in get(const QChar& )

    void My_First_Calculator::get(const QChar& _ch)
    {
            if (_ch == 'C') {
    ....
    

    Is it right?


  • Moderators

    No, you can't do it like that. There's no setMapping() overload in QSignalMapper that takes a QChar and you can't connect mapped(QString) to get(QChar). There's no direct cast from QString to QChar.



  • @tomy said in Is there any QCharList to use:

    So can I simply use a QString instead of a QStringList

    yes as long as you use lambdas instead of QSignalMapper:

    for(int i=0; i<texts.size(); ++i)
         {
            QPushButton* button = new QPushButton(texts[i]);
    
            connect(button, &QPushButton::clicked,
    [texts,i,this]()->void{get(texts[i]);}               
    );
            gridLayout -> addWidget(button, i/5, i%5);
         }
    
    

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.