Qt Q_OBJECT问题出错
//mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include<QPushButton>
#include<QLabel>
#include<QDialog>
#include<QInputDialog>
#include<QWidget>
#include<QVBoxLayout>
#include<QHBoxLayout>
class StandardInputDialog:public QDialog
{
// Q_OBJECT ///////注意这里
public:
StandardInputDialog(QWidget*parent=0,Qt::WindowFlags f=0);
private:
QLabel *nameLabel;
QLabel*nameLableChenge;
QLabel*sexLabel;
QLabel*sexLabelChenge;
QLabel *ageLabel;
QLabel*ageLabelChenge;
QLabel*highLabel;
QLabel*highLabelChenge;
QPushButton *namePB;
QPushButton*sexPB;
QPushButton*agePB;
QPushButton*highPB;
private slots:
void slotNamePB();
void slotSexPB();
void slotAgePB();
void slotHighPB();
};
#endif // MAINWINDOW_H
//mainwindow.cpp
#include "mainwindow.h"
StandardInputDialog::StandardInputDialog(QWidget *parent, Qt::WindowFlags f)
:QDialog(parent,f)
{
// QLabel *nameLabel,*nameLableChenge,*sexLabel,*sexLabelChenge;
nameLabel=new QLabel;
nameLabel->setText("姓名");
nameLableChenge=new QLabel;
nameLableChenge->setText("阿敏");
sexLabel=new QLabel;
sexLabel->setText("性别");
sexLabelChenge=new QLabel;
sexLabelChenge->setText("女生");
//QLabel *ageLabel,*ageLabelChenge,*highLabel,*highLabelChenge;
ageLabel=new QLabel;
ageLabel->setText("年龄");
ageLabelChenge=new QLabel;
ageLabelChenge->setText("20");
highLabel=new QLabel;
highLabel->setText("身高");
highLabelChenge=new QLabel;
highLabelChenge->setText("164");
//QPushButton *namePB,*sexPB,*agePB,*highPB;
namePB=new QPushButton;
namePB->setText("修改");
sexPB=new QPushButton;
sexPB->setText("修改");
agePB=new QPushButton;
agePB->setText("修改");
highPB=new QPushButton;
highPB->setText("修改");
//用布局将各个部件加载到dialog上
QHBoxLayout *hlayout=new QHBoxLayout;
hlayout->addWidget(nameLabel);
hlayout->addWidget(nameLableChenge);
hlayout->addWidget(namePB);
QHBoxLayout *h2layout=new QHBoxLayout;
h2layout->addWidget(sexLabel);
h2layout->addWidget(sexLabelChenge);
h2layout->addWidget(sexPB);
QHBoxLayout *h3layout=new QHBoxLayout;
h3layout->addWidget(ageLabel);
h3layout->addWidget(ageLabelChenge);
h3layout->addWidget(agePB);
QHBoxLayout *h4layout=new QHBoxLayout;
h4layout->addWidget(highLabel);
h4layout->addWidget(highLabelChenge);
h4layout->addWidget(highPB);
QVBoxLayout *vlayout=new QVBoxLayout;
vlayout->addLayout(hlayout);
vlayout->addLayout(h2layout);
vlayout->addLayout(h3layout);
vlayout->addLayout(h4layout);
this->setLayout(vlayout);
//将信号与槽连接起来
QObject::connect(namePB,SIGNAL(clicked()),this,SLOT(slotNamePB()));
QObject::connect(sexPB,SIGNAL(clicked()),this,SLOT(slotSexPB()));
QObject::connect(agePB,SIGNAL(clicked()),this,SLOT(slotAgePB()));
QObject::connect(highPB,SIGNAL(clicked()),this,SLOT(slotHighPB()));
}
void StandardInputDialog::slotNamePB()
{
bool ok;
QString str=QInputDialog::getText(this,"name","please input name:",
QLineEdit::Normal,nameLabel->text(),&ok);
if(ok&&!str.isEmpty())
nameLabel->setText(str);
}
void StandardInputDialog::slotSexPB()
{
QStringList strList;
strList<<"man"<<"women";
bool ok;
QString sex=QInputDialog::getItem(this,"sex","please select sex:",
strList,0,false,&ok);
if(ok)
sexLabel->setText(sex);
}
void StandardInputDialog::slotAgePB()
{
bool ok;
int age=QInputDialog::getInt(this,"age","please input age:",
ageLabel->text().toInt(),0,100,1,&ok);
if(ok)
ageLabel->setText(QString("%1").arg(age));
}
void StandardInputDialog::slotHighPB()
{
bool ok;
double high=QInputDialog::getDouble(this,"high","please input high:",
highLabel->text().toDouble(),50,300,1,&ok);
if(ok)
highLabel->setText(QString("%1").arg(high));
}
//main.cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
StandardInputDialog w;
w.show();
return a.exec();
}
运行结果:

为什么会有那个QObject::connect: No such slot QDialog::slotNamePB()
如果在头文件里加上
Q_OBJECT
运行结果:

这是怎么回事?
老是遇到这种无法解析的问题?
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include<QPushButton>
#include<QLabel>
#include<QDialog>
#include<QInputDialog>
#include<QWidget>
#include<QVBoxLayout>
#include<QHBoxLayout>
class StandardInputDialog:public QDialog
{
// Q_OBJECT ///////注意这里
public:
StandardInputDialog(QWidget*parent=0,Qt::WindowFlags f=0);
private:
QLabel *nameLabel;
QLabel*nameLableChenge;
QLabel*sexLabel;
QLabel*sexLabelChenge;
QLabel *ageLabel;
QLabel*ageLabelChenge;
QLabel*highLabel;
QLabel*highLabelChenge;
QPushButton *namePB;
QPushButton*sexPB;
QPushButton*agePB;
QPushButton*highPB;
private slots:
void slotNamePB();
void slotSexPB();
void slotAgePB();
void slotHighPB();
};
#endif // MAINWINDOW_H
//mainwindow.cpp
#include "mainwindow.h"
StandardInputDialog::StandardInputDialog(QWidget *parent, Qt::WindowFlags f)
:QDialog(parent,f)
{
// QLabel *nameLabel,*nameLableChenge,*sexLabel,*sexLabelChenge;
nameLabel=new QLabel;
nameLabel->setText("姓名");
nameLableChenge=new QLabel;
nameLableChenge->setText("阿敏");
sexLabel=new QLabel;
sexLabel->setText("性别");
sexLabelChenge=new QLabel;
sexLabelChenge->setText("女生");
//QLabel *ageLabel,*ageLabelChenge,*highLabel,*highLabelChenge;
ageLabel=new QLabel;
ageLabel->setText("年龄");
ageLabelChenge=new QLabel;
ageLabelChenge->setText("20");
highLabel=new QLabel;
highLabel->setText("身高");
highLabelChenge=new QLabel;
highLabelChenge->setText("164");
//QPushButton *namePB,*sexPB,*agePB,*highPB;
namePB=new QPushButton;
namePB->setText("修改");
sexPB=new QPushButton;
sexPB->setText("修改");
agePB=new QPushButton;
agePB->setText("修改");
highPB=new QPushButton;
highPB->setText("修改");
//用布局将各个部件加载到dialog上
QHBoxLayout *hlayout=new QHBoxLayout;
hlayout->addWidget(nameLabel);
hlayout->addWidget(nameLableChenge);
hlayout->addWidget(namePB);
QHBoxLayout *h2layout=new QHBoxLayout;
h2layout->addWidget(sexLabel);
h2layout->addWidget(sexLabelChenge);
h2layout->addWidget(sexPB);
QHBoxLayout *h3layout=new QHBoxLayout;
h3layout->addWidget(ageLabel);
h3layout->addWidget(ageLabelChenge);
h3layout->addWidget(agePB);
QHBoxLayout *h4layout=new QHBoxLayout;
h4layout->addWidget(highLabel);
h4layout->addWidget(highLabelChenge);
h4layout->addWidget(highPB);
QVBoxLayout *vlayout=new QVBoxLayout;
vlayout->addLayout(hlayout);
vlayout->addLayout(h2layout);
vlayout->addLayout(h3layout);
vlayout->addLayout(h4layout);
this->setLayout(vlayout);
//将信号与槽连接起来
QObject::connect(namePB,SIGNAL(clicked()),this,SLOT(slotNamePB()));
QObject::connect(sexPB,SIGNAL(clicked()),this,SLOT(slotSexPB()));
QObject::connect(agePB,SIGNAL(clicked()),this,SLOT(slotAgePB()));
QObject::connect(highPB,SIGNAL(clicked()),this,SLOT(slotHighPB()));
}
void StandardInputDialog::slotNamePB()
{
bool ok;
QString str=QInputDialog::getText(this,"name","please input name:",
QLineEdit::Normal,nameLabel->text(),&ok);
if(ok&&!str.isEmpty())
nameLabel->setText(str);
}
void StandardInputDialog::slotSexPB()
{
QStringList strList;
strList<<"man"<<"women";
bool ok;
QString sex=QInputDialog::getItem(this,"sex","please select sex:",
strList,0,false,&ok);
if(ok)
sexLabel->setText(sex);
}
void StandardInputDialog::slotAgePB()
{
bool ok;
int age=QInputDialog::getInt(this,"age","please input age:",
ageLabel->text().toInt(),0,100,1,&ok);
if(ok)
ageLabel->setText(QString("%1").arg(age));
}
void StandardInputDialog::slotHighPB()
{
bool ok;
double high=QInputDialog::getDouble(this,"high","please input high:",
highLabel->text().toDouble(),50,300,1,&ok);
if(ok)
highLabel->setText(QString("%1").arg(high));
}
//main.cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
StandardInputDialog w;
w.show();
return a.exec();
}
运行结果:

为什么会有那个QObject::connect: No such slot QDialog::slotNamePB()
如果在头文件里加上
Q_OBJECT
运行结果:

这是怎么回事?
老是遇到这种无法解析的问题?
...全文
相关推荐
Qt之Q_PROPERTY学习
: public QWidget { Q_OBJECT Q_PROPERTY(ColorMode colorMode READ colorMode WRITE setColorMode NOTIFY
Qt面试题_QT_
本文档涵盖C++核心语言特性和Qt框架关键技术的面试问题,重点包括多态、const、static、指针与引用的区别、STL容器使用及单例模式实现;深入探讨Qt信号与槽机制、元对象系统(moc)、Q_O
vs2019配置完qt出现问题解决
随着编译器的更新,这个问题可能在未来版本中得到解决。因此,如果遇到类似问题,首先检查你的编译器版本和更新选项,以及Qt的版本是否兼容。
Qt编程探秘:Q_OBJECT宏与元对象系统的深度剖析.docx
它不仅能够提升编程的效率,还能让开发者在遇到复杂的应用需求时,如图形界面中的组件通信,能够快速而高效地解决问题。不仅如此,Q_OBJECT宏所赋予的能力,也让代码结构更加清晰,易于理解和维护。
Qt4迁移至Qt5完全指南
##### 3.4 处理Q_OBJECT宏与元类型清理**背景:** Qt5对于QObject子类及其元类型管理有了新的要求。**操作:**- 确保所有需要的对象都声明了`Q_OBJECT`宏。


