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. A container and lineEdit that accept both strings and doubles
QtWS25 Last Chance

A container and lineEdit that accept both strings and doubles

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 3 Posters 2.0k Views
  • 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 tomy
    #1

    Hi,

    I found the source of the problem. I need some container that can at least do what a stringstream does in C++. We temporarily call it a "somecontainer"
    In C++ using a stringstream we can do these:

    stringstream ss;
    ss << "ABC";
    cout << ss.str();    // here it shows its value (ABC) in standard screen
    double d =12;
    ss << d;
    cout << ss.str();    // here it shows its value (ABC12) in standard screen
    ss.clear();  // This way it will be cleaned
    cout << ss.str();    // here it shows nothing
    

    Do we have such a "somecontainer" in Qt?

    I also need a lineEdit that can accept "somecontainer" that is, both strings and doubles, like this:

    QLineEdit* l = new QLineEdit;
    l -> setText(somecontainer);  // It should shows a string when somecontainer contains a string
    l -> setNumber(somecontainer);  // It should shows a number when somecontainer contains a number
    

    Can we do it over a lineEdit?

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Isn't that a QTextStream + QString combo without the need to subclass QLineEdit ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      tomyT 1 Reply Last reply
      1
      • SGaistS SGaist

        Hi,

        Isn't that a QTextStream + QString combo without the need to subclass QLineEdit ?

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

        @SGaist
        Do you mean that I use QTextStream for inputting integers (doubles) and QString for strings and lineEdit will be able to show both of them?

        I didn't understand "without need to subclass lineEdit" part. I'm not a native speaker of English but my grammar is not bad. (subclass is a noun).

           QString s = "12";
           s += "AB";
           lineEdit1 -> setText(s);  // Untill here OK. LineEdit1 will show the contents of s well.
           QTextStream ss;
           ss 40;  // How to put that 40 into *ss*?
           lineEdit2 -> setText(ss);  // Will it be possible?
        

        I thank you for the helps.

        VRoninV 1 Reply Last reply
        0
        • tomyT tomy

          @SGaist
          Do you mean that I use QTextStream for inputting integers (doubles) and QString for strings and lineEdit will be able to show both of them?

          I didn't understand "without need to subclass lineEdit" part. I'm not a native speaker of English but my grammar is not bad. (subclass is a noun).

             QString s = "12";
             s += "AB";
             lineEdit1 -> setText(s);  // Untill here OK. LineEdit1 will show the contents of s well.
             QTextStream ss;
             ss 40;  // How to put that 40 into *ss*?
             lineEdit2 -> setText(ss);  // Will it be possible?
          

          I thank you for the helps.

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

          see QString::number()

           QString s = "12";
             s += "AB";
          lineEdit1 -> setText(s);
          s+= QString::number(40);
          lineEdit2 -> setText(s);
          

          Important to note that QString::number() does not use localised conventions (e.g. Anglosaxon decimal separator is . while "latin" decimal separator is ,, QString::number() will always use .). if you want a more user friendly result use locale()->toString()

           QString s = "12";
             s += "AB";
          lineEdit1 -> setText(s);
          s+= lineEdit2->locale()->toString(40);
          lineEdit2 -> setText(s);
          

          "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
          1
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            About subclasses and inheritance.

            QString string;
            QTextStream stream(&string);
            stream << "AB" << "12";
            
            lineEdit1->setText(string);
            
            stream << 40.9;
            
            lineEdit2->setString(string);
            

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            tomyT 1 Reply Last reply
            2
            • SGaistS SGaist

              About subclasses and inheritance.

              QString string;
              QTextStream stream(&string);
              stream << "AB" << "12";
              
              lineEdit1->setText(string);
              
              stream << 40.9;
              
              lineEdit2->setString(string);
              
              tomyT Offline
              tomyT Offline
              tomy
              wrote on last edited by
              #6

              @SGaist
              Thank you.

              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #7

                You're welcome !

                If all these answer your question, then please mark the thread as solved using the "Topic Tools" button so that other forum users may know a solution has been found :)

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                tomyT 1 Reply Last reply
                0
                • SGaistS SGaist

                  You're welcome !

                  If all these answer your question, then please mark the thread as solved using the "Topic Tools" button so that other forum users may know a solution has been found :)

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

                  @SGaist
                  Ow, sure!
                  Thanks kind man.

                  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