• 4918阅读
  • 7回复

关于 Q_OBJECT 宏的一些疑问 [复制链接]

上一主题 下一主题
离线jiangll
 
只看楼主 正序阅读 楼主  发表于: 2009-09-15
— 本帖被 XChinux 执行加亮操作(2009-09-16) —
上午刚刚做了一个练习只是简单的操作,但是Build不过去,在头文件中将Q_OBJECT删除后可以正常build。不知道具体问题出在哪里了,希望各位能帮忙解答,谢谢了。

头文件:

#ifndef MYWIDGET_H
#define MYWIDGET_H

#include <QDialog>

class QLabel;
class QLineEdit;
class QPushButton;

class MyWidget:public QDialog
{
    Q_OBJECT

    public:
        MyWidget(QWidget *parent=0);
    public slots:
        void okButtonClicked();
        void noButtonClicked();
    private:
        QLabel *nameLabel;
        QLabel *pwdLabel;
        QLineEdit *nameEdit;
        QLineEdit *pwdEdit;
        QPushButton *okButton;
        QPushButton *noButton;


};

#endif // MYWIDGET_H

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

程序:

#include <QtGui>

#include "mywidget.h"

MyWidget::MyWidget(QWidget *parent)
        :QDialog(parent)
{
    nameLabel = new QLabel;
    nameLabel->setText("Name:");
    nameEdit = new QLineEdit;
    nameLabel->setBuddy(nameEdit);

    pwdLabel = new QLabel;
    pwdLabel->setText("Password:");
    pwdEdit = new QLineEdit;
    pwdEdit->setEchoMode(QLineEdit::Password);
    pwdLabel->setBuddy(pwdEdit);

    okButton = new QPushButton;
    okButton->setText(tr("Ok"));
    noButton = new QPushButton;
    noButton->setText(tr("No"));

    QHBoxLayout *topLayout = new QHBoxLayout;
    topLayout->addWidget(nameLabel);
    topLayout->addWidget(nameEdit);

    QHBoxLayout *centerLayout = new QHBoxLayout;
    centerLayout->addWidget(pwdLabel);
    centerLayout->addWidget(pwdEdit);

    QHBoxLayout *bottomLayout = new QHBoxLayout;
    bottomLayout->addWidget(okButton);
    bottomLayout->addWidget(noButton);

    QVBoxLayout *mainLayout = new QVBoxLayout;
    mainLayout->addLayout(topLayout);
    mainLayout->addLayout(centerLayout);
    mainLayout->addLayout(bottomLayout);

    connect(this->okButton,SIGNAL(clicked()),this,SLOT(okButtonClicked()));
    connect(this->noButton,SIGNAL(clicked()),this,SLOT(noButtonClicked()));

    setLayout(mainLayout);
}

void MyWidget::okButtonClicked(){
    if(nameEdit->text().isEmpty()&&pwdEdit->text().isEmpty()){
        QMessageBox::warning(this,"warming","",QMessageBox::Ok);

    }else{
        QMessageBox::information(this,"welcome",
          tr("Hello {0}").arg(nameEdit->text()),QMessageBox::Ok,QMessageBox::Ok);
    }
}

void MyWidget::noButtonClicked(){
    int rel = QMessageBox::information(this,"do you want to close the app.",
                                       "do you want to close the app.",
                                       QMessageBox::Ok,QMessageBox::Ok);

    if(rel==QMessageBox::Ok){
        this->close();
    }
}
离线foxyz

只看该作者 7楼 发表于: 2009-09-17
不好意思,说错了,Q_OBJECT和moc_XXX.cpp有关
离线jiangll
只看该作者 6楼 发表于: 2009-09-16
foxyz,我的画面部分全部是通过在构造函数中进行的创建和布局,不知道我添加 #include "ui_XXX.h"有什么用。而且,从编译上来看本身就是引用Q_OBJECT宏出现了问题,和你说的没什么关系。不过还是谢谢你。
离线foxyz

只看该作者 5楼 发表于: 2009-09-16
说简单点, 你的cpp文件缺少#include "ui_XXX.h"
离线jiangll
只看该作者 4楼 发表于: 2009-09-15
恩,重新运行了下qmake就好了,dbzhang800,谢谢你的回复阿。
离线dbzhang800

只看该作者 3楼 发表于: 2009-09-15
重新运行下qmake 试试
离线jiangll
只看该作者 2楼 发表于: 2009-09-15
Running build steps for project MyWidget...
Configuration unchanged, skipping QMake step.
Starting: C:/MinGW/bin/mingw32-make.exe -w
mingw32-make: Entering directory `C:/Documents and Settings/jiangll/My Documents/MyWidget'
C:/MinGW/bin/mingw32-make -f Makefile.Debug
mingw32-make[1]: Entering directory `C:/Documents and Settings/jiangll/My Documents/MyWidget'
g++ -enable-stdcall-fixup -Wl,-enable-auto-import -Wl,-enable-runtime-pseudo-reloc -mthreads -Wl -Wl,-subsystem,windows -o debug\MyWidget.exe debug/mywidget.o debug/Main.o -L"d:\Qt\4.5.2\lib" -lmingw32 -lqtmaind -lQtGuid4 -lQtCored4
mingw32-make[1]: Leaving directory `C:/Documents and Settings/jiangll/My Documents/MyWidget'
mingw32-make: Leaving directory `C:/Documents and Settings/jiangll/My Documents/MyWidget'
debug/mywidget.o: In function `ZN8MyWidgetC2EP7QWidget':
C:/Documents and Settings/jiangll/My Documents/MyWidget/mywidget.cpp:7: undefined reference to `vtable for MyWidget'
C:/Documents and Settings/jiangll/My Documents/MyWidget/mywidget.cpp:7: undefined reference to `vtable for MyWidget'
debug/mywidget.o: In function `ZN8MyWidgetC1EP7QWidget':
C:/Documents and Settings/jiangll/My Documents/MyWidget/mywidget.cpp:7: undefined reference to `vtable for MyWidget'
C:/Documents and Settings/jiangll/My Documents/MyWidget/mywidget.cpp:7: undefined reference to `vtable for MyWidget'
debug/mywidget.o: In function `ZNK7QString7isEmptyEv':
C:/Documents and Settings/jiangll/My Documents/MyWidget/mywidget.cpp:(.text$_ZN8MyWidget2trEPKcS1_[MyWidget::tr(char const*, char const*)]+0x1c): undefined reference to `MyWidget::staticMetaObject'
debug/Main.o: In function `ZSt17__verify_groupingPKcjRKSs':
C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/c++/3.4.5/bits/locale_facets.tcc:2498: undefined reference to `vtable for MyWidget'
C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/c++/3.4.5/bits/locale_facets.tcc:2499: undefined reference to `vtable for MyWidget'
collect2: ld returned 1 exit status
mingw32-make[1]: *** [debug\MyWidget.exe] Error 1
mingw32-make: *** [debug] Error 2
Exited with code 2.
Error while building project MyWidget
When executing build step 'Make'
离线dbzhang800

只看该作者 1楼 发表于: 2009-09-15
把编译器或链接器的报错贴出来看看
快速回复
限100 字节
 
上一个 下一个