2012-12-29 114 views
-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(); 
    } 
+1

您是否嘗試調試代碼? –

+0

我是Qt新手,我不知道如何在qt中進行調試。 – Amar

+0

現在可能是學習的好時機,否則你將如何修復下一個bug,之後的錯誤以及之後的錯誤?... –

回答

1

我固定bug.Actually我顯示該對話框時按下菜單action.so我不能表示it.that是我錯開設置文本的一部分到問題。當前設置在LineEdit文本該行在dialog-> show()之後編輯,對我來說工作正常。