-1

这是代码片段来自我为我的项目修改的财富客户端示例。我创建了一个财富客户端对话框,当我在我的一个项目中使用此代码时,它输出的是,Windows 调试窗口打开并说 exe 遇到问题需要关闭。谁能告诉我是什么问题??

 QString ipAddress;
    QList<QHostAddress> ipAddressesList = QNetworkInterface::allAddresses();
    // use the first non-localhost IPv4 address
    for (int i = 0; i < ipAddressesList.size(); ++i) {
        if (ipAddressesList.at(i) != QHostAddress::LocalHost &&
            ipAddressesList.at(i).toIPv4Address()) {
            ipAddress = ipAddressesList.at(i).toString();
            break;
        }
    }
    // if we did not find one, use IPv4 localhost
    if (ipAddress.isEmpty())
        ipAddress = QHostAddress(QHostAddress::LocalHost).toString();

    Ui_client.hostLineEdit->setText(ipAddress);
    Ui_client.portLineEdit->setValidator(new QIntValidator(1, 65535, this));


    Ui_client.okButton->setDefault(true);
    Ui_client.okButton->setEnabled(false);

    //! [1]
    tcpSocket = new QTcpSocket(this);
    //! [1]
    ;
    connect(Ui_client.hostLineEdit, SIGNAL(textChanged(QString)),
            this, SLOT(enableokButton()));
    connect(Ui_client.portLineEdit, SIGNAL(textChanged(QString)),
            this, SLOT(enableokButton()));

    connect(Ui_client.okButton, SIGNAL(clicked()),this, SLOT(requestNewFortune()));//connectclose()));
    connect(tcpSocket, SIGNAL(readyRead()), this, SLOT(readFortune()));

    //! [2] //! [4]
    connect(tcpSocket, SIGNAL(error(QAbstractSocket::SocketError)),
            //! [3]
            this, SLOT(displayError(QAbstractSocket::SocketError)));
    Ui_client.portLineEdit->setFocus();

    QNetworkConfigurationManager manager;
    if (manager.capabilities() & QNetworkConfigurationManager::NetworkSessionRequired) {
        // Get saved network configuration
        QSettings settings(QSettings::UserScope, QLatin1String("Trolltech"));
        settings.beginGroup(QLatin1String("QtNetwork"));
        const QString id = settings.value(QLatin1String("DefaultNetworkConfiguration")).toString();
        settings.endGroup();

        // If the saved network configuration is not currently discovered use the system default
        QNetworkConfiguration config = manager.configurationFromIdentifier(id);
        if ((config.state() & QNetworkConfiguration::Discovered) !=
            QNetworkConfiguration::Discovered) {
            config = manager.defaultConfiguration();
        }

        networkSession = new QNetworkSession(config, this);
        connect(networkSession, SIGNAL(opened()), this, SLOT(sessionOpened()));
        Ui_client.okButton->setEnabled(false);
        //ui->statusLabel->setText(tr("Opening network session."));
        networkSession->open();
    }
4

1 回答 1

1

我修复了这个错误。实际上,当我按下菜单操作时,我正在显示对话框。所以在显示之前我无法在 LineEdit 中设置文本。这就是问题。当我将设置文本的部分转移到对话框后的行编辑时 - >显示(),它对我来说很好。

于 2012-12-29T12:17:57.707 回答