当前位置: 代码迷 >> QT开发 >> Qt Q_OBJECT有关问题出错
  详细解决方案

Qt Q_OBJECT有关问题出错

热度:122   发布时间:2016-04-25 03:06:21.0
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

运行结果:


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

------解决思路----------------------
如果你用Qt的信号槽机制呢,就必须加上Q_OBJECT,如果编译过不去的话,把头文件从项目移出去,然后重新加进来,Qt会产生一个moc文件
------解决思路----------------------
半路添加Q_OBJECT后,必须重新运行qmake!!

当然,你也可以随便修改一下 .pro 文件,哪怕是添加或删除一个空格或空行什么的都可以。使得qmake可以自动运行
------解决思路----------------------
首先信号槽一定要加上 Q_OBJECT,其次他是继承自 QObject类的,如果你不是他派生出的,要加上头文件