Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Is there any QCharList to use
Forum Updated to NodeBB v4.3 + New Features

Is there any QCharList to use

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 3 Posters 1.6k Views 3 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • tomyT Offline
    tomyT Offline
    tomy
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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.

      tomyT 1 Reply Last reply
      2
      • Chris KawaC Chris Kawa

        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.

        tomyT Offline
        tomyT Offline
        tomy
        wrote on last edited by tomy
        #3

        @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?

        1 Reply Last reply
        0
        • Chris KawaC Offline
          Chris KawaC Offline
          Chris Kawa
          Lifetime Qt Champion
          wrote on last edited by Chris Kawa
          #4

          @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).

          tomyT 1 Reply Last reply
          2
          • Chris KawaC Chris Kawa

            @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).

            tomyT Offline
            tomyT Offline
            tomy
            wrote on last edited by
            #5

            @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?

            VRoninV 1 Reply Last reply
            0
            • Chris KawaC Offline
              Chris KawaC Offline
              Chris Kawa
              Lifetime Qt Champion
              wrote on last edited by
              #6

              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.

              1 Reply Last reply
              3
              • tomyT tomy

                @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?

                VRoninV Offline
                VRoninV Offline
                VRonin
                wrote on last edited by
                #7

                @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);
                     }
                
                

                "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                ~Napoleon Bonaparte

                On a crusade to banish setIndexWidget() from the holy land of Qt

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved