将QLineEdit对象的内容保存到字符串变量(C ++) [英] Saving the content of a QLineEdit object into a string variable (C++)

查看:398
本文介绍了将QLineEdit对象的内容保存到字符串变量(C ++)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我环顾了Qt文档,但是在我的项目中,我希望程序的大多数非图形化更多思考部分都位于单独的.cpp文件中。
鉴于此,我想将键入文本的内容放入QLineEdit对象中,并在用户触发 returnPressed操作后将其另存为字符串,但是当我键入以下内容时:

I've looked around the Qt Documentation, but within my project, I'd like to having most of the non-graphical 'more thinking' part of my program be on a seperate .cpp file. Given that, I was wanting to take the text typed into a QLineEdit object and save it as a string after the user triggers the 'returnPressed' action, but when I type:

void MainWindow::on_lineEdit_returnPressed()

{
    QMessageBox msgBox;
    msgBox.setText("The entry has been modified.");
    msgBox.exec();
    //The line which should save the contents of the QLineEdit box:
    string input = QLineEdit::text();
}

...进入Qt Creator IDE提供的模板(所有必要的希望创建的插槽)编译器返回

...Into the template provided by the Qt Creator IDE (with all necessary slots hopefully created) The compiler returns

In member function 'void MainWindow::on_lineEdit_returnPressed()'
cannot call member function 'QString...'

...等等。

我应该如何重写我的代码才能正确执行此操作?

How should I rewrite my code to do this correctly?

推荐答案


  1. 您必须选择如何存储字符串。您的主要选项是: char s数组,标准库中的std :: string和Qt中的QString。如果需要在第三方库中使用字符串,则可能需要将其存储在std :: string或 char s数组中,但是如果不是在这种情况下,我建议您只使用QString,因为它可以在Qt中广泛使用,尽管您可以将QString转换为std :: string或 char s数组。

  1. You must choose how to store the string. Your main options are: array of chars, std::string from the standard library, and QString from Qt. If you need to use the string in a third party library then you might need to store it in an std::string or an array of chars, but if that's not the case then I suggest that you simply use QString as it is widely used throughout Qt, although you can convert a QString to std::string or array of chars.

您实际上必须检索文本。为此,您必须在QLineEdit实例而不是QLineEdit类本身上调用text()函数。可以通过 ui 指针访问所有窗口小部件。打开设计器并检查行编辑的名称,默认名称为 lineEdit ,因此请尝试替换行

You must actually retrieve the text. To do this you must call the text() function on the QLineEdit instance, not on the QLineEdit class itself. All widgets can be accessed through the ui pointer. Open the designer and check the name of the line edit, the default name is lineEdit, so try replacing the line

字符串输入= QLineEdit :: text();

与该行

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

这篇关于将QLineEdit对象的内容保存到字符串变量(C ++)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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