Qt Q_OBJECT问题出错

天半子 天半子 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
//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

运行结果:


这是怎么回事?
老是遇到这种无法解析的问题?
...全文

相关推荐

评论 4
收藏 已收藏
分享
返回顶部
天半子
Qt 21,503 成员 26,693 内容