我是qt4的新手,我正在尝试获取输入文本数据。但我没有得到。
有人可以帮助我吗?我将非常感激。
谢谢。
我在做什么的例子:
adduser.cpp
#include <QtGui>
#include "adduser.h"
myQt_user::myQt_user(QDialog *parent)
{
setupUi(this); // this sets up GUI
connect(pushButton_adduser, SIGNAL(clicked()), this, SLOT(add_user()));
}
void myQt_user::add_user()
{
users = lineEdit_user->text();
QMessageBox::information(this, tr("Data"),tr("Get user:" +users ));
}
adduser.h
#ifndef ADDUSER_H
#define ADDUSER_H
#include "ui_dialog_useradd.h"
class myQt_user: public QDialog, private Ui::windows_add
{
Q_OBJECT
public:
myQt_user(QDialog *parent = 0);
QLineEdit *lineEdit_user;
QString users;
public slots:
void add_user();
};
#endif
ERRO:
adduser.cpp:-1: In member function 'void myQt_user::add_user()':
adduser.cpp:13: error: no matching function for call to 'myQt_user::tr(const QString)'
adduser.h:9: candidates are: static QString myQt_user::tr(const char*, const char*)
adduser.h:9: note: static QString myQt_user::tr(const char*, const char*, int)
答案 0 :(得分:4)
Qt的方法如下:
QMessageBox::information(this, tr("Data"), tr("Get user:" +users ));
应该是
QMessageBox::information(this, tr("Data"), tr("Get user: %1").arg(users));
答案 1 :(得分:2)
如错误所示,您将QString
传递给了const char*
的函数:
QMessageBox::information(this, tr("Data"),tr("Get user:" +users ));
要么不调用tr,要么传递char *
:
QMessageBox::information(this, tr("Data"),"Get user:" +users); // removed tr
或
QMessageBox::information(this, tr("Data"),tr(qPrintable("Get user:" +users)));
// get a char* from the QString with the qPrintable macro.
(因为你可能不想本地化用户输入,我会选择第一个选项。)