这段代码有什么问题吗?

时间:2012-12-29 09:27:42

标签: qt4

这是代码片段来自我为我的项目修改过的fortune客户端示例。我已经创建了一个fortune客户端对话框,当我在我的一个项目中使用这个代码输出时,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();
    }

1 个答案:

答案 0 :(得分:1)

我修复了错误。实际上我在按菜单操作时显示对话框。因此我无法在显示之前在LineEdit中设置文本。这就是问题。当我将设置文本的一部分移到行后编辑时dialog-&gt; show(),它对我来说很好。