我似乎无法将线条文本转换为Qtcreator中的数字

时间:2014-12-12 17:17:58

标签: c++ qt

我正在尝试创建一个程序,它接受3个用户输入和一个计算按钮,将它们全部放入一个等式并打印出答案。我现在遇到的问题是输入似乎无法转换为数字,我无法弄清楚原因。

错误在第(int numN0 = QString::number(N0);):

上读取
error: no matching function for call to 'QString::number(QString&)'

这是我的代码:

标题

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

private slots:
    void on_NtButton_clicked();

    void on_N0Button_clicked();

    void on_kButton_clicked();

    void on_tButton_clicked();

    void on_quitButton_clicked();

    void on_pushButton_5_clicked();

    void on_equation_linkActivated(const QString &link);

private:
    Ui::MainWindow *ui;
    int N;
    int N0;
    int k;
    int t;
};

Main.cpp的

#include "mainwindow.h"
#include <QApplication>
#include <QPushButton>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    /*QPushButton *button = new QPushButton("Quit the program!");
    QObject::connect(button, SIGNAL(clicked()), &app, SLOT(quit()));
    button->show();
    */
    MainWindow w;
    w.show();

    return a.exec();
}

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QtCore>
#include <QtGui>
#include <QMessageBox>


MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_N0Button_clicked()
{
    QString N0 = ui->lineEdit_2->text();
    int numN0 = QString::number(N0);

    if (numN0 < 1000){
        QMessageBox::information(this,"Error","Can't be over 1000");
    }

    if (ui->lineEdit_2->text() > 0)
    {
        QMessageBox::information(this,"Error","Can't be under 0");
    }
}

void MainWindow::on_kButton_clicked()
{
    int k = ui->lineEdit_3->text();

    if (QString::number(k) > 1)
    {
        QMessageBox::information(this,"Error","Can't be over 1");
    }

    if (ui->lineEdit_3->text() < 0)
    {
        QMessageBox::information(this,"Error","Can't be under 0");
    }
}

void MainWindow::on_tButton_clicked()
{
    QString t = ui->lineEdit_4->text();
}

void MainWindow::on_pushButton_5_clicked()
{
    for (int x = 0; x < t; x++)
    {
        int ans = N*x == N0*10^(k*x);
        ui->equation->setText(QString::number(ans));
    }
}

1 个答案:

答案 0 :(得分:2)

您应该使用:

QString N0 = ui->lineEdit_2->text();
int numN0 = N0.toInt();

QString::number(N0)需要int并返回QString,但您需要转换为int。如果您想知道转换是否成功,也可以使用bool ok

例如:

QString str = "FF";
bool ok;
int hex = str.toInt(&ok, 16);       // hex == 255, ok == true
int dec = str.toInt(&ok, 10);       // dec == 0, ok == false

Information