我是Qt Framework的新手,我还在学习。在搜索教程时,我偶然发现了一系列教授Qt基础知识的视频以及制作计算器的项目。虽然计算器似乎工作正常,但我得到了这种警告
QMetaObject::connectSlotsByName: No matching signal for on_pushButton_unary_released()
QMetaObject::connectSlotsByName: No matching signal for on_pushButton_binary_released()
我已经尝试了一切,但似乎没有任何工作。我真的很沮丧。任何帮助都将不胜感激。顺便说一下,我附上了mainwindow.cpp的代码
#include "mainwindow.h"
#include "ui_mainwindow.h"
double firstnum ;
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
connect(ui->pushButton_0 , SIGNAL(released()),this,SLOT(digits_released()));
connect(ui->pushButton_1 , SIGNAL(released()),this,SLOT(digits_released()));
connect(ui->pushButton_2 , SIGNAL(released()),this,SLOT(digits_released()));
connect(ui->pushButton_3 , SIGNAL(released()),this,SLOT(digits_released()));
connect(ui->pushButton_4 , SIGNAL(released()),this,SLOT(digits_released()));
connect(ui->pushButton_5 , SIGNAL(released()),this,SLOT(digits_released()));
connect(ui->pushButton_6 , SIGNAL(released()),this,SLOT(digits_released()));
connect(ui->pushButton_7 , SIGNAL(released()),this,SLOT(digits_released()));
connect(ui->pushButton_8 , SIGNAL(released()),this,SLOT(digits_released()));
connect(ui->pushButton_9 , SIGNAL(released()),this,SLOT(digits_released()));
connect(ui->pushButton_plusminus , SIGNAL(released()),this,SLOT(on_pushButton_unary_released()));
connect(ui->pushButton_percent , SIGNAL(released()),this,SLOT(on_pushButton_unary_released()));
connect(ui->pushButton_plus , SIGNAL(released()),this,SLOT(on_pushButton_binary_released()));
connect(ui->pushButton_minus, SIGNAL(released()),this,SLOT(on_pushButton_binary_released()));
connect(ui->pushButton_multiply , SIGNAL(released()),this,SLOT(on_pushButton_binary_released()));
connect(ui->pushButton_divide , SIGNAL(released()),this,SLOT(on_pushButton_binary_released()));
ui->pushButton_divide->setCheckable(true);
ui->pushButton_minus->setCheckable(true);
ui->pushButton_plus->setCheckable(true);
ui->pushButton_multiply->setCheckable(true);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::digits_released(){
QPushButton *button = (QPushButton*)sender();
double labelNumber ;
QString newLabel ;
if(ui->pushButton_plus->isChecked() || ui->pushButton_minus->isChecked()
|| ui->pushButton_divide->isChecked()
|| ui->pushButton_multiply->isChecked())
{
labelNumber = button->text().toDouble();
}
else
{
labelNumber = (ui->label->text() + button->text()).toDouble();
newLabel = QString::number(labelNumber, 'g' , 15);
ui->label->setText(newLabel);
}
}
void MainWindow::on_pushButton_decimal_released()
{
QString decimal = QString(".");
if (ui->label->text().contains(decimal))
{
}
else
{
ui->label->setText(ui->label->text() + ".");
}
}
void MainWindow::on_pushButton_unary_released()
{
QPushButton * button = (QPushButton*) sender();
double labelNumber ;
QString newLabel ;
if (button->text() == "+/-")
{
labelNumber = ui->label->text().toDouble();
labelNumber = labelNumber * -1 ;
newLabel = QString::number(labelNumber, 'g' , 15);
ui->label->setText(newLabel);
}
else if (button->text() == "%") {
labelNumber = ui->label->text().toDouble();
labelNumber = labelNumber * 0.01 ;
newLabel = QString::number(labelNumber, 'g' , 15);
ui->label->setText(newLabel);
}
}
void MainWindow::on_pushButton_C_released()
{
ui->label->setText("0");
}
void MainWindow::on_pushButton_binary_released()
{
QPushButton * button = (QPushButton*) sender();
firstnum = ui->label->text().toDouble();
button->setChecked(true);
}
void MainWindow::on_pushButton_equal_released()
{
double secondnum , labelNumber ;
QString newlabel ;
secondnum = ui->label->text().toDouble();
if (ui->pushButton_plus->isChecked())
{
labelNumber = firstnum + secondnum ;
newlabel = QString::number(labelNumber , 'g' , 15);
ui->label->setText(newlabel);
ui->pushButton_plus->setChecked(false);
}
else if (ui->pushButton_minus->isChecked())
{
labelNumber = firstnum - secondnum ;
newlabel = QString::number(labelNumber , 'g' , 15);
ui->label->setText(newlabel);
ui->pushButton_minus->setChecked(false);
}
else if (ui->pushButton_divide->isChecked())
{
labelNumber = firstnum / secondnum ;
newlabel = QString::number(labelNumber , 'g' , 15);
ui->label->setText(newlabel);
ui->pushButton_divide->setChecked(false);
}
else if (ui->pushButton_multiply->isChecked())
{
labelNumber = firstnum * secondnum ;
newlabel = QString::number(labelNumber , 'g' , 15);
ui->label->setText(newlabel);
ui->pushButton_multiply->setChecked(false);
}
}
答案 0 :(得分:0)
名为*" on_something **的插槽由QMetaObject以特殊方式处理,它使用It的反射机制将它们连接到正确的控制和信号。
sintax实际上是on_objectName_signal。因此,您在控制台中看到的错误是因为moc没有找到名为" pushButton_unary"的对象。尝试自动连接。
由于您手动将信号连接到插槽,因此只需重命名插槽即可避免使用该语法(例如,将插槽命名为" onUnaryButtonRelesed")。