A container and lineEdit that accept both strings and doubles
I found the source of the problem. I need some container that can at least do what a
stringstreamdoes in C++. We temporarily call it a "somecontainer"
In C++ using a
stringstreamwe 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
lineEditthat 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?
Do you mean that I use
QTextStreamfor inputting integers (doubles) and
QStringfor strings and
lineEditwill 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.
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
QString s = "12"; s += "AB"; lineEdit1 -> setText(s); s+= lineEdit2->locale()->toString(40); lineEdit2 -> setText(s);
About subclasses and inheritance.
QString string; QTextStream stream(&string); stream << "AB" << "12"; lineEdit1->setText(string); stream << 40.9; lineEdit2->setString(string);
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 :)
Thanks kind man.