Qt Q_OBJECT问题出错

天半子 2014-11-27 10:45:43
//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

运行结果:


这是怎么回事?
老是遇到这种无法解析的问题?
...全文
1392 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
天半子 2014-12-02
  • 打赏
  • 举报
回复
谢谢! 刚学qt各种问题,谢谢大家的解答
晋晔 2014-12-01
  • 打赏
  • 举报
回复
首先信号槽一定要加上 Q_OBJECT,其次他是继承自 QObject类的,如果你不是他派生出的,要加上头文件
dbzhang800 2014-11-28
  • 打赏
  • 举报
回复 2
半路添加Q_OBJECT后,必须重新运行qmake!! 当然,你也可以随便修改一下 .pro 文件,哪怕是添加或删除一个空格或空行什么的都可以。使得qmake可以自动运行
蜗牛sf 2014-11-28
  • 打赏
  • 举报
回复
如果你用Qt的信号槽机制呢,就必须加上Q_OBJECT,如果编译过不去的话,把头文件从项目移出去,然后重新加进来,Qt会产生一个moc文件

16,787

社区成员

发帖
与我相关
我的任务
社区描述
Qt 是一个跨平台应用程序框架。通过使用 Qt,您可以一次性开发应用程序和用户界面,然后将其部署到多个桌面和嵌入式操作系统,而无需重复编写源代码。
社区管理员
  • Qt
  • 亭台六七座
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧