Qt:单独输入并在新行上显示

时间:2019-03-08 11:04:43

标签: c++ qt qstring qmessagebox

我正在尝试让用户输入以逗号分隔的地址。这个想法是程序将在新行上以正确的格式显示地址。我是使用qtCreator的新手。我试图将输入转换为列表,然后在QMessage中显示该列表。

我遇到的错误是:

C:\Qt\5.11.2\mingw53_32\include\QtCore\qstring.h:275: candidate function not viable: no known conversion from 'QStringList' to 'qlonglong' (aka 'long long') for 1st argument

C:\Users\Nickitaes\Desktop\Misc\UNISA\COS2614\Assignment 01\header\main.cpp:25: error: no matching conversion for functional-style cast from 'QStringList' to 'QString'

下面是我的代码。我不确定我要去哪里,我发现的文档使用整数。

int main (int argc, char* argv[]) {
    QApplication app(argc, argv);
    QMessageBox msgBox;
    QString enteredAddress = QInputDialog::getText(0, "User Address",
                                                 "Enter address each field separated by a comma "
                                                 "and a space: ");
    QStringList lines = enteredAddress.split(",/n ");

    QString response = QString("The new address format is ").arg(lines);
    msgBox.setText("Message Box", + QString(enteredAddress.split(",/n")))

    return 0;
}

1 个答案:

答案 0 :(得分:1)

您的代码中有多个错字:例如,"Message Box", +应该为"Message Box" +。 这是一个很好的起点:

int main (int argc, char* argv[]) {
    QApplication app(argc, argv);
    QString enteredAddress = QInputDialog::getText(
        0,
        "User Address",
        "Enter address each field separated by a comma and a space: ");
    QMessageBox msgBox;
    msgBox.setText("Message Box:\n" + enteredAddress.replace(", ", "\n"));
    msgBox.show();
    return app.exec();
}