[英]Integer user input in Qt
I am writing a GUI application in Qt.我正在 Qt 中编写一个 GUI 应用程序。
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.
什么可能是 go 可以在视觉上做到这一点。 This may seem easy but I don't have much experience in GUI applications.
这似乎很容易,但我在 GUI 应用程序方面没有太多经验。 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.
我正在考虑使用 lineEdit 但它将文本作为输入,我不确定在这种情况下如何处理输入。
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.将此与 QLineEdit 作为输入机制结合使用。
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)....然后稍后当您想获取数字时,使用 qlistview (或其他)来填充向量....或者只是将数据留在 Qlistview 中并且不使用向量(取决于您想要做什么)....
You also try this:你也试试这个:
int INT_NUM ;
QString STRING_NUM = ui->Line_Edit->text();
INT_NUM = STRING_NUM.toInt();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.