【问题标题】:Get lineEdit Input data获取 lineEdit 输入数据
【发布时间】:2011-12-22 01:42:11
【问题描述】:

我是 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

错误:

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)

【问题讨论】:

  • 请描述您遇到的实际问题。如果不知道你的 UI 文件中有什么,就不可能修复你的代码。

标签: c++ qt sdk qt4


【解决方案1】:

正如错误所说,您将 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.

(由于您可能不想本地化用户输入,我会选择第一个选项。)

【讨论】:

    【解决方案2】:

    Qt 的实现方式如下:

    QMessageBox::information(this, tr("Data"), tr("Get user:" +users ));
    

    应该是

    QMessageBox::information(this, tr("Data"), tr("Get user: %1").arg(users));
    

    【讨论】:

    猜你喜欢
    • 2020-06-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-10
    • 1970-01-01
    • 1970-01-01
    • 2023-04-04
    相关资源
    最近更新 更多