简体   繁体   中英

Integer user input in Qt

I am writing a GUI application in Qt.

I need to get from the user a set of integers which will be loaded in an array. What might be the go to way to do this visually. This may seem easy but I don't have much experience in GUI applications. I'm thinking of using a lineEdit but that takes text as input and I'm not sure how to handle the input in that case.

For future reference if somebody has a similar question this is how I did it using line edit:

//Read input as string from line edit
QString input_text = ui->lineEdit->text();

if(input_text.isEmpty())
    return;

//Check for letters or symbols
for (int i = 0; i < input_text.length(); i++)
    if(input_text.at(i).isLetter() || input_text.at(i).isSymbol() || input_text.at(i).isPunct())
        return;

//Split input in parts around 'space'
QStringList input_list = input_text.split(" ", QString::SkipEmptyParts);

//Copy those parts to array as integers
for (int i = 0; i < input_list.length() && i < current_array_size; i++)
    array.replace(i, input_list.at(i).toInt() );

Probably a good start point is something like a list view or table view or list widget:

Couple this with an QLineEdit as an entry mechanism.

User types in a number - hits enter (link that signal to validate and enter the number into which even list you want).

Then later when you want to get at the numbers use the qlistview (or whatever) to populate the vector....or just leave the data in the Qlistview and dont use a vector (depending what you want to do)....

You also try this:

int INT_NUM ; 
QString STRING_NUM = ui->Line_Edit->text();
INT_NUM = STRING_NUM.toInt();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM