A container and lineEdit that accept both strings and doubles

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

  • Lifetime Qt Champion


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

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

  • 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);

  • Lifetime Qt Champion

    About subclasses and inheritance.

    QString string;
    QTextStream stream(&string);
    stream << "AB" << "12";
    stream << 40.9;

  • @SGaist
    Thank you.

  • Lifetime Qt Champion

    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 :)

  • @SGaist
    Ow, sure!
    Thanks kind man.

Log in to reply

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