QInputDialog
-
Hi, I have to calculate an exam mark by getting the 3 marks with QInputDialog as a single string in the form of 27, 6, 55. How do I get the QInputDialog to work and how do I convert the single string to 3 values?
@
#include <QtGui>int main (int argc, char* argv[]) {
QApplication app(argc, argv);
QTextStream cout(stdout); /* Create a QTextStream to standard output. */// Declarations of variables do { // local variables to the loop: // QString text; bool ok; QString text = QInputDialog::getText(this, "Exam mark","Percentages: ",QLineEdit::Normal ,&ok); /* Pop up dialog, wait for user to enter an integer, return it. */ getline(cin,text); cout << "User entered: " << text << endl; while (!text.isEmpty()) {
@
Edit: please use @ tags around code sections; Andre
-
Hi,
Andre is totally right, as always, but isn't using a custom dialog with three different input fields much better.
One thing to consider is that a user is always liable to input error. When three input boxes are used you are able to set the validator on each one for the minimum and maximum scores. This to void input errors.
Greetz -
Thank you, I am a beginner but I have improved the code a bit, still the editor says it is an invalid use of "this" in a non-member function. Unfortunately I have to use QInputDialog and a single string as input - it is an assignment
@#include <QtGui>
#include <QTextStream>int main (int argc, char* argv[]) {
QApplication app(argc, argv);
QTextStream cout(stdout);
QTextStream cin(stdin);do { int assign1,assign2,assign3; double yearmark,exammark; bool ok; QString text = QInputDialog::getText(this, "Exam mark Calculator"," Percentages: ",QLineEdit::Normal ,&ok); QStringList list; list = text.split(","); assign1 = list[0].toInt(); assign2 = list[1].toInt(); assign3 = list[2].toInt(); yearmark = ((assign1*0.2) +(assign2*0.5) +(assign3*0.3))*0.2; exammark = (50-(yearmark))/0.8; cout << "Final mark is" << exammark<< endl; QString response = QString("The assingment marks and final mark is " %1 %2 %3 %4 %5) .arg(assign1).arg(assign2).arg(assign3).arg(exammark.toInt()).arg("Calculate another mark"?); /* Each %n is replaced with an arg() value. */ answer = QMessageBox::question(0, "Calculate another?", response, QMessageBox::Yes | QMessageBox::No); } while (answer == QMessageBox::Yes); //int i=2; while} (ok&& and (!text.isEmpty())) }@
-
On line 16, you're using this without being in an object. You're in your main() function. Simply don't set a parent, use 0 instead.
It looks like you have a syntax error on line 39.
Your code is not secure. What happens if the user makes a mistake, perhaps entering only one or two values? In real code, such mistakes can lead to security issues.
As you're using QInputDialog to gather your input, why are you using cout for your output? I'd use a QMessageBox or something like that. And why are you using the name cout, that might conflict with the standard libraries as your variable name? It's highlighted here for a reason...
-
Thank you I managed to get the program running. but can you please explain the use of the following parameteres to me:
QLineEdit::EchoMode mode = QLineEdit::Normal
bool * ok = 0
Qt::WindowFlags flags = 0QString getText ( QWidget * parent, const QString & title, const QString & label, QLineEdit::EchoMode mode = QLineEdit::Normal, const QString & text = QString(), bool * ok = 0, Qt::WindowFlags flags = 0 )
-
Please use code tags around your code sections.
I'm a bit lost. You posted the code to feign with. Since you wrote it, I'd assume you understand it. Unless you just blindly copy/pasted it from somewhere? Did you already use the Qt documentation to just look them up?
-
This is the parameter list specified in the Qt documentation:
QString getText ( QWidget * parent, const QString & title, const QString & label, QLineEdit::EchoMode mode = QLineEdit::Normal, const QString & text = QString(), bool * ok = 0, Qt::WindowFlags flags = 0 )
but I don't understand the purpose/meaning of the following parameters: May you please explain it to me
QLineEdit::EchoMode mode = QLineEdit::Normal
bool * ok = 0
Qt::WindowFlags flags = 0