如何使用QlineEdit输入整数值 [英] How to use QlineEdit to enter integer values

查看:1120
本文介绍了如何使用QlineEdit输入整数值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 QlineEdit

运行程序并获得该值作为变量存储以供稍后使用。到目前为止,我只找到了如何使用

How would I enter a value into the edit bar when I run the program and get that valued stored as a variable to be used later. So far I have only found out how to enter text using

void parameter_settings::on_lineEdit_textEdited(const QString &arg1)

{
    ui->lineEdit->setText("");
}



我有一个GUI需要用户输入一个特定范围内的值。该值将被存储为一个变量供以后使用。我已经阅读关于验证器,但不能让它按预期工作。

I have a GUI that requires the user to enter a value within a specific range. That value would be stored as a variable for later use. I have read about validators but can't get it to work as intended.

推荐答案

我不完全确定你的问题是什么,但是你可以从QLineEdit获得输入命令 text()

I am not entirely sure what your question is, but you can get the input from a QLineEdit with the command text():

QString input = ui->lineEdit->text();

和整数输入:

int integer_value = ui->lineEdit->text().toInt();

正如你提到的验证器:您可以使用验证器来允许用户只插入整数到QLineEdit第一名。有不同的,但我一般喜欢使用RegEx验证器。在这种情况下:

As you mentioned validators: You can use validators to allow the user to insert only integers into the QLineEdit in the first place. There are different ones but I generally like to use 'RegEx' validators. In this case:

QRegExpValidator* rxv = new QRegExpValidator(QRegExp("\\d*"), this); // only pos
QRegExpValidator* rxv = new QRegExpValidator(QRegExp("[+-]?\\d*"), this); // pos and neg
ui->lineEdit->setValidator(rxv);

这篇关于如何使用QlineEdit输入整数值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆