我正在尝试制作一个简单的程序来计算2个数字,并在Qt的标签上给出总和。 但我得到了一个错误,我不知道我做错了什么。
我收到了这个错误:
K:\QtSDK\QT_files\les4-build-desktop-Qt_4_8_1_for_Desktop_- _MSVC2010__Qt_SDK__Debug\..\les4\calcu.cpp:40: error: C2664: 'bool QObject::connect(const QObject *,const char *,const QObject *,const char *,Qt::ConnectionType)' : cannot convert parameter 4 from 'void' to 'const char *'
Expressions of type void cannot be converted to other types
所以基本上我在第一个textedit中的值更改时会进入bereken函数,这将获得textedits的2个值并计算它们。
Calcu.h
#ifndef CALCU_H
#define CALCU_H
#include <QWidget>
#include <QLineEdit>
#include <QLabel>
namespace Ui {
class calcu;
}
class calcu : public QWidget
{
Q_OBJECT
public:
explicit calcu(QWidget *parent = 0);
~calcu();
public slots:
void bereken(void);
private:
Ui::calcu *ui;
QLineEdit *number1 ;
QLineEdit *number2 ;
QLabel *sum;
};
#endif // CALCU_H
calcu.ccp
#include "calcu.h"
#include "ui_calcu.h"
#include <QLineEdit>
#include <QLabel>
#include <QHBoxLayout>
#include <QMessageBox>
#include <QString>
calcu::calcu(QWidget *parent) :
QWidget(parent),
ui(new Ui::calcu)
{
ui->setupUi(this);
QGridLayout *grid = new QGridLayout;
number1 = new QLineEdit;
number2 = new QLineEdit;
QLabel *sign = new QLabel("+");
QLabel *equal = new QLabel("=");
sum = new QLabel;
QHBoxLayout *layout = new QHBoxLayout;
layout->addWidget(number1);
layout->addWidget(sign);
layout->addWidget(number2);
layout->addWidget(equal);
layout->addWidget(sum);
// this->setLayout(layout);
ui->groupBox->setLayout(layout);
ui->groupBox->setTitle("Enter som");
setWindowTitle(tr("Group Boxes"));
// resize(480, 320);
setLayout(grid);
QObject::connect(number1, SIGNAL(textChanged(QString)),this,bereken());
// bereken();
}
calcu::~calcu()
{
delete ui;
}
void calcu::bereken(){
int som;
QString number;
number = number1->text();
som = number.toInt();
number = number2->text();
som = som + number.toInt();
sum->setText(QString::number(som));
}
答案 0 :(得分:3)
我想你想要这条线:
QObject::connect(number1, SIGNAL(textChanged(QString)),this,bereken());
是:
QObject::connect(number1, SIGNAL(textChanged(QString)),this, SLOT(bereken()));
SLOT宏返回const char *。
答案 1 :(得分:0)
似乎SIGNAL返回void,所以行
QObject::connect(number1, SIGNAL(textChanged(QString)),this,bereken());
错误地使用了这个功能。