//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
运行结果:
这是怎么回事?
老是遇到这种无法解析的问题?