2014-02-11 127 views
0

我是新來的qt.I'm製作計算器,我做了一個函數來添加2個數字,但它給了我一些奇怪的錯誤。函數首先將輸入作爲2個字符串從行編輯並且將它們轉換通過toInt函數int和增加他們然後通過文字把它放在一個標籤()我的功能:
沒有匹配的函數調用qlinedit

void calculator::on_pushButton_clicked() 
{ 
    ui->ansLabel->setText("+"); 
    ui->firstNo->text(string1); 
    ui->secondNo->text(string2); 
    int firstno = string1.toInt(); 
    int secondno = string2.toInt(); 
    int ans = firstno + secondno; 
    ui->ansLabel->setText(ans); 
} 

錯誤:

\Calculator\calculator.cpp:20: error: no matching function for call to 'QLineEdit::text(QString&)' 
    ui->firstNo->text(string1); 
\Calculator\calculator.cpp:25: error: invalid user-defined conversion from 'int' to 'const QString&' [-fpermissive] 
    ui->ansLabel->setText(ans); 
          ^
\Calculator\calculator.cpp:25: error: invalid conversion from 'int' to 'const char*' [-fpermissive] 
    ui->ansLabel->setText(ans); 
         ^

如何修復這些錯誤。什麼即時做錯了?

回答

1

編譯器告訴你到底什麼是錯的。根據Qt文檔,QLineEdit::text()函數不接受任何參數,但返回一個值。同樣,QLabel::setText()不接受整數作爲參數,但QString。因此,您需要編寫如下功能:

void calculator::on_pushButton_clicked() 
{ 
    ui->ansLabel->setText("+"); 
    QString string1 = ui->firstNo->text(); 
    QString string2 = ui->secondNo->text(); 
    int firstno = string1.toInt(); 
    int secondno = string2.toInt(); 
    int ans = firstno + secondno; 
    ui->ansLabel->setText(QString::number(ans)); 
}