简体   繁体   English

无法使用Q_OBJECT构建Qt示例(可以避免使用Qmake吗?)

[英]Can't build Qt example with Q_OBJECT (Can I avoid Qmake?)

As in Errors while compiling a test program using Qt I am following the book 'C++ GUI Programming with Qt 4' by Jasmin Blanchette and Mark Summerfield too. 就像在使用Qt编译测试程序时遇到的错误中一样,我也遵循Jasmin Blanchette和Mark Summerfield撰写的《使用Qt 4进行C ++ GUI编程》一书。 I don't have that mistake, and anyway I can't build it. 我没有那个错误,无论如何我都无法建立它。 It had been able to build until I started to write the constructor. 在我开始编写构造函数之前,它一直可以构建。 I use Qt 4.8 and MSVC 2010 via Qt add-in. 我通过Qt加载项使用Qt 4.8和MSVC 2010。

[EDIT] Can I avoid using Qmake in Qt visual studio add-in? [编辑]我可以避免在Qt visual studio加载项中使用Qmake吗? [/EDIT] [/编辑]

My code: 我的代码:

finddialog.h finddialog.h

#ifndef FindDialog_H
#define FindDialog_H
#include <qdialog.h>

class QCheckBox;
class QLabel;
class QLineEdit;
class QPushButton;

class FindDialog : public QDialog
{
    Q_OBJECT

public:
    FindDialog(QWidget *parent=0);

signals:
    void findNext(const QString &str, Qt::CaseSensitivity cs);
    void findPrev(const QString &str, Qt::CaseSensitivity cs);

private slots:
    void findClicked();
    void enableFindButton(const QString &text);

private:
    QLabel *label;
    QLineEdit *lineEdit;
    QCheckBox *caseCheckBox;
    QCheckBox *backwardCheckBox;
    QPushButton *findButton;
    QPushButton *closeButton;
};

#endif

finddialog.cpp finddialog.cpp

#include <QtGui>
#include <finddialog.h>

FindDialog::FindDialog(QWidget *parent) : QDialog(parent)
{
    label = new QLabel(tr("Find &what: "));
    lineEdit = new QLineEdit;
    label->setBuddy(lineEdit);
    caseCheckBox = new QCheckBox(tr("Match &case"));
    backwardCheckBox = new QCheckBox(tr("Serch backward"));
    findButton = new QPushButton(tr("&Find"));
    findButton->setDefault(true);
    findButton->setEnabled(false);
    closeButton = new QPushButton(tr("CLose"));

    connect(lineEdit, SIGNAL(textChanged(const QString &)), this, SLOT(enableFindButton(const QString &)));
    connect(findButton, SIGNAL(findClicked()), this, SLOT(clicked()));
    connect(closeButton, SIGNAL(clicked()), this, SLOT(close()));

    QHBoxLayout *topLeftLayout = new QHBoxLayout;
    topLeftLayout->addWidget(label);
    topLeftLayout->addWidget(lineEdit);

    QVBoxLayout *leftLayout = new QVBoxLayout;
    leftLayout->addLayout(topLeftLayout);
    leftLayout->addWidget(caseCheckBox);
    leftLayout->addWidget(backwardCheckBox);

    QVBoxLayout *rightLayout = new QVBoxLayout;
    rightLayout->addWidget(findButton);
    rightLayout->addWidget(closeButton);

    QHBoxLayout *mainLayout = new QHBoxLayout;
    mainLayout->addLayout(leftLayout);
    mainLayout->addLayout(rightLayout);

    setLayout(mainLayout);

    setWindowTitle(tr("Find"));
    setFixedHeight(sizeHint().height());
}

void FindDialog::findClicked()
{
    QString text = lineEdit->text();
    Qt::CaseSensitivity cs = caseCheckBox->isChecked() ? Qt::CaseSensitive : Qt::CaseInsensitive;

    if(backwardCheckBox->isChecked())
        emit findPrev(text, cs);
    else emit findNext(text, cs);
}

void FindDialog::enableFindButton(const QString &text)
{
    findButton->setEnabled(!text.isEmpty());
}

main.cpp main.cpp中

#include <qapplication.h>
#include <finddialog.h>

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);

    FindDialog *dialog = new FindDialog;
    dialog->show();

    return app.exec();
}

And the output: 并输出:

1>------ Build started: Project: ex1, Configuration: Release Win32 ------
1>Build started 26.03.2013 23:36:17.
1>InitializeBuildStatus:
1>  Touching "Release\ex1.unsuccessfulbuild".
1>ClCompile:
1>  finddialog.cpp
1>  main.cpp
1>  Generating Code...
1>finddialog.obj : error LNK2019: unresolved external symbol "public: static struct QMetaObject const FindDialog::staticMetaObject" (?staticMetaObject@FindDialog@@2UQMetaObject@@B) referenced in function "public: static class QString __cdecl FindDialog::tr(char const *,char const *)" (?tr@FindDialog@@SA?AVQString@@PBD0@Z)
1>finddialog.obj : error LNK2019: unresolved external symbol "protected: void __thiscall FindDialog::findNext(class QString const &,enum Qt::CaseSensitivity)" (?findNext@FindDialog@@IAEXABVQString@@W4CaseSensitivity@Qt@@@Z) referenced in function "private: void __thiscall FindDialog::findClicked(void)" (?findClicked@FindDialog@@AAEXXZ)
1>finddialog.obj : error LNK2019: unresolved external symbol "protected: void __thiscall FindDialog::findPrev(class QString const &,enum Qt::CaseSensitivity)" (?findPrev@FindDialog@@IAEXABVQString@@W4CaseSensitivity@Qt@@@Z) referenced in function "private: void __thiscall FindDialog::findClicked(void)" (?findClicked@FindDialog@@AAEXXZ)
1>finddialog.obj : error LNK2001: unresolved external symbol "public: virtual struct QMetaObject const * __thiscall FindDialog::metaObject(void)const " (?metaObject@FindDialog@@UBEPBUQMetaObject@@XZ)
1>finddialog.obj : error LNK2001: unresolved external symbol "public: virtual void * __thiscall FindDialog::qt_metacast(char const *)" (?qt_metacast@FindDialog@@UAEPAXPBD@Z)
1>finddialog.obj : error LNK2001: unresolved external symbol "public: virtual int __thiscall FindDialog::qt_metacall(enum QMetaObject::Call,int,void * *)" (?qt_metacall@FindDialog@@UAEHW4Call@QMetaObject@@HPAPAX@Z)
1>C:\Users\Family\Documents\Visual Studio 2010\Projects\ex1\Win32\Release\\ex1.exe : fatal error LNK1120: 6 unresolved externals
1>
1>Build FAILED.
1>
1>Time Elapsed 00:00:02.21
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Thank you. 谢谢。

Code's been run before. 代码之前已经运行过。 It worked without any make-file. 它没有任何make-file即可工作。 Just simple compilation in MSVC2010: MSVC2010中的简单编译:

#include <QApplication>
#include <QHBoxLayout>
#include <QSlider>
#include <QSpinBox>

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);

    QWidget *window = new QWidget;
    window->setWindowTitle("Enter Your Age");

    QSpinBox *spinBox = new QSpinBox;
    QSlider *slider = new QSlider(Qt::Horizontal);
    spinBox->setRange(0, 130);
    slider->setRange(0, 130);

    QObject::connect(spinBox, SIGNAL(valueChanged(int)),
                     slider, SLOT(setValue(int)));
    QObject::connect(slider, SIGNAL(valueChanged(int)),
                     spinBox, SLOT(setValue(int)));
    spinBox->setValue(35);

    QHBoxLayout *layout = new QHBoxLayout;
    layout->addWidget(spinBox);
    layout->addWidget(slider);
    window->setLayout(layout);

    window->show();

    return app.exec();
}

Maybe it is quite a beginner's question. 也许这是一个初学者的问题。 But if I had such one, maybe it would be helpful for someone else. 但是,如果我有这样的一个,也许对其他人会有帮助。

Errors were there because of my using Q_OBJECT, so my app cannot work without moc. 由于使用Q_OBJECT导致出现错误,因此如果没有moc,我的应用程序将无法运行。 Second example worked coorectly cause there was no the macros and moc wasn't needed necessarily. 第二个示例协同工作,因为没有宏,并且不一定需要moc。

And Can I avoid writing Qmake and moc? 而且我可以避免编写Qmake和moc吗? Yes I can. 我可以。 Qt Visual Studio add-in makes all required files automatically. Qt Visual Studio加载项会自动生成所有必需的文件。 If there are some problems like 如果有类似的问题

1>finddialog.obj : error LNK2019: unresolved external symbol "public: static struct QMetaObject const FindDialog::staticMetaObject" (?staticMetaObject@FindDialog@@2UQMetaObject@@B) referenced in function "public: static class QString __cdecl FindDialog::tr(char const *,char const *)" (?tr@FindDialog@@SA?AVQString@@PBD0@Z)
1>finddialog.obj : error LNK2019: unresolved external symbol "protected: void __thiscall FindDialog::findNext(class QString const &,enum Qt::CaseSensitivity)" (?findNext@FindDialog@@IAEXABVQString@@W4CaseSensitivity@Qt@@@Z) referenced in function "private: void __thiscall FindDialog::findClicked(void)" (?findClicked@FindDialog@@AAEXXZ)
1>finddialog.obj : error LNK2019: unresolved external symbol "protected: void __thiscall FindDialog::findPrev(class QString const &,enum Qt::CaseSensitivity)" (?findPrev@FindDialog@@IAEXABVQString@@W4CaseSensitivity@Qt@@@Z) referenced in function "private: void __thiscall FindDialog::findClicked(void)" (?findClicked@FindDialog@@AAEXXZ)
1>finddialog.obj : error LNK2001: unresolved external symbol "public: virtual struct QMetaObject const * __thiscall FindDialog::metaObject(void)const " (?metaObject@FindDialog@@UBEPBUQMetaObject@@XZ)
1>finddialog.obj : error LNK2001: unresolved external symbol "public: virtual void * __thiscall FindDialog::qt_metacast(char const *)" (?qt_metacast@FindDialog@@UAEPAXPBD@Z)
1>finddialog.obj : error LNK2001: unresolved external symbol "public: virtual int __thiscall FindDialog::qt_metacall(enum QMetaObject::Call,int,void * *)" (?qt_metacall@FindDialog@@UAEHW4Call@QMetaObject@@HPAPAX@Z)

Then reload the header file, where Q_OBJECT class was defined (just exclude the file, and add it again). 然后重新加载定义了Q_OBJECT类的头文件(只需排除该文件,然后再次添加它)。 New moc.cpp files will be created. 将创建新的moc.cpp文件。 Then rebuild the project. 然后重建项目。

I couldn't find any help for the Qt add-in. 我找不到有关Qt加载项的任何帮助。 If someone had it, could you give the link, please. 如果有人知道,请给我链接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM