Reputation: 287
I'm new to qt4, and I'm trying to get the input text data. But I'm not getting.
Would anyone could help me? I would be very grateful.
Thank you.
Example of what I'm doing:
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)
Upvotes: 2
Views: 802
Reputation: 12331
The Qt way to do it is the following:
QMessageBox::information(this, tr("Data"), tr("Get user:" +users ));
should be
QMessageBox::information(this, tr("Data"), tr("Get user: %1").arg(users));
Upvotes: 4
Reputation: 14695
As the error says, you're passing a QString
to a function that takes const char*
:
QMessageBox::information(this, tr("Data"),tr("Get user:" +users ));
Either don't call tr, or pass it a char *
:
QMessageBox::information(this, tr("Data"),"Get user:" +users); // removed tr
or
QMessageBox::information(this, tr("Data"),tr(qPrintable("Get user:" +users)));
// get a char* from the QString with the qPrintable macro.
(Since you probably don't want to localize user input, I'd go with the first option.)
Upvotes: 2