多个定义错误:一个在我的文件中,一个在 moc 文件中。

发布于 2024-12-25 00:25:44 字数 3198 浏览 1 评论 0原文

我有一个名为 FindAndReplaceBar 的类,其实现是这样的:

#include "FindAndReplaceBar.h"
#include <QLabel>
#include <QPushButton>
#include <QGridLayout>
#include <QTextDocument>
#include <QLineEdit>

FindAndReplaceBar::FindAndReplaceBar(QObject *parent) :
    QToolBar(NULL)
{
    lblFind         = new QLabel("Find: ",this);
    lblReplace      = new QLabel("Replace",this);

    ledtFind        = new QLineEdit(this);
    ledtReplace     = new QLineEdit(this);

    QPixmap next(":/res/resources/next.gif");
    QPixmap previous(":/res/resources/previous.gif");
    QPixmap close(":/res/resources/close_icon.gif");

    btnFindNext     = new QPushButton(QIcon(next),"",this);
    btnFindPrevious = new QPushButton(QIcon(previous),"",this);
    btnClose        = new QPushButton(QIcon(close),"",this);

    btnReplace      = new QPushButton("Replace",this);
    btnReplaceAll   = new QPushButton("Replace All",this);

    btnFindNext->setFlat(true);
    btnFindPrevious->setFlat(true);
    btnClose->setFlat(true);
    btnReplace->setFlat(true);
    btnReplaceAll->setFlat(true);

    lytFindAndReplaceBar        = new QGridLayout(this);

    lytFindAndReplaceBar->addWidget(lblFind,0,0,1,1);
    lytFindAndReplaceBar->addWidget(ledtFind,0,1,1,2);
    lytFindAndReplaceBar->addWidget(btnFindPrevious,0,3,1,1);
    lytFindAndReplaceBar->addWidget(btnFindNext,0,4,1,1);

    lytFindAndReplaceBar->addWidget(lblReplace,0,5,1,1);
    lytFindAndReplaceBar->addWidget(ledtReplace,0,6,1,2);
    lytFindAndReplaceBar->addWidget(btnReplace,0,8,1,1);
    lytFindAndReplaceBar->addWidget(btnReplaceAll,0,9,1,1);

    this->setLayout(lytFindAndReplaceBar);

    connect(btnFindNext,SIGNAL(clicked()),this,SIGNAL(findNext()));
    connect(btnFindPrevious,SIGNAL(pressed()),this,SIGNAL(findPrevious()));
    connect(btnClose,SIGNAL(pressed()),this,SLOT(close()));
    connect(btnReplace,SIGNAL(pressed()),this,SIGNAL(replace()));
    connect(btnReplaceAll,SIGNAL(pressed()),this,SIGNAL(replaceAll()));

    this->setStyleSheet("QToolBar{background: qlineargradient(x1:0,x2:0,y1:0,y2:1,stop:0 #fffaf0,stop:0.3 #fdf5e6)} QLineEdit{border-radius:4px;padding:2px;}");
}

void FindAndReplaceBar::findNext()
{
    emit find(0);
}

void FindAndReplaceBar::findPrevious()
{
    emit find(QTextDocument::FindBackward);
}

void FindAndReplaceBar::replace()
{
    emit replace(false);
}

void FindAndReplaceBar::replaceAll()
{
    emit replace(true);
}

QString FindAndReplaceBar::searchTerm()
{
    return this->ledtFind->text();
}

QString FindAndReplaceBar::replaceTerm()
{
    return this->ledtReplace->text();
}

void FindAndReplaceBar::setSearchFieldText(const QString & searchFieldText)
{
    this->ledtFind->setText(searchFieldText);
}

void FindAndReplaceBar::setReplaceFieldText(const QString & replaceFieldText)
{
    this->ledtReplace->setText(replaceFieldText);
}

当我运行程序时,我遇到函数多个定义的问题:

findNext(), findPrevious(), <代码>replace()、replaceAll()

另一个定义是在 moc_FindAndReplaceBar.cpp 文件中进行的。 我不确定问题是什么,所以我不知道如何解决它!我真的很感激任何帮助,谢谢!

I have a class called FindAndReplaceBar, whose implementation is this:

#include "FindAndReplaceBar.h"
#include <QLabel>
#include <QPushButton>
#include <QGridLayout>
#include <QTextDocument>
#include <QLineEdit>

FindAndReplaceBar::FindAndReplaceBar(QObject *parent) :
    QToolBar(NULL)
{
    lblFind         = new QLabel("Find: ",this);
    lblReplace      = new QLabel("Replace",this);

    ledtFind        = new QLineEdit(this);
    ledtReplace     = new QLineEdit(this);

    QPixmap next(":/res/resources/next.gif");
    QPixmap previous(":/res/resources/previous.gif");
    QPixmap close(":/res/resources/close_icon.gif");

    btnFindNext     = new QPushButton(QIcon(next),"",this);
    btnFindPrevious = new QPushButton(QIcon(previous),"",this);
    btnClose        = new QPushButton(QIcon(close),"",this);

    btnReplace      = new QPushButton("Replace",this);
    btnReplaceAll   = new QPushButton("Replace All",this);

    btnFindNext->setFlat(true);
    btnFindPrevious->setFlat(true);
    btnClose->setFlat(true);
    btnReplace->setFlat(true);
    btnReplaceAll->setFlat(true);

    lytFindAndReplaceBar        = new QGridLayout(this);

    lytFindAndReplaceBar->addWidget(lblFind,0,0,1,1);
    lytFindAndReplaceBar->addWidget(ledtFind,0,1,1,2);
    lytFindAndReplaceBar->addWidget(btnFindPrevious,0,3,1,1);
    lytFindAndReplaceBar->addWidget(btnFindNext,0,4,1,1);

    lytFindAndReplaceBar->addWidget(lblReplace,0,5,1,1);
    lytFindAndReplaceBar->addWidget(ledtReplace,0,6,1,2);
    lytFindAndReplaceBar->addWidget(btnReplace,0,8,1,1);
    lytFindAndReplaceBar->addWidget(btnReplaceAll,0,9,1,1);

    this->setLayout(lytFindAndReplaceBar);

    connect(btnFindNext,SIGNAL(clicked()),this,SIGNAL(findNext()));
    connect(btnFindPrevious,SIGNAL(pressed()),this,SIGNAL(findPrevious()));
    connect(btnClose,SIGNAL(pressed()),this,SLOT(close()));
    connect(btnReplace,SIGNAL(pressed()),this,SIGNAL(replace()));
    connect(btnReplaceAll,SIGNAL(pressed()),this,SIGNAL(replaceAll()));

    this->setStyleSheet("QToolBar{background: qlineargradient(x1:0,x2:0,y1:0,y2:1,stop:0 #fffaf0,stop:0.3 #fdf5e6)} QLineEdit{border-radius:4px;padding:2px;}");
}

void FindAndReplaceBar::findNext()
{
    emit find(0);
}

void FindAndReplaceBar::findPrevious()
{
    emit find(QTextDocument::FindBackward);
}

void FindAndReplaceBar::replace()
{
    emit replace(false);
}

void FindAndReplaceBar::replaceAll()
{
    emit replace(true);
}

QString FindAndReplaceBar::searchTerm()
{
    return this->ledtFind->text();
}

QString FindAndReplaceBar::replaceTerm()
{
    return this->ledtReplace->text();
}

void FindAndReplaceBar::setSearchFieldText(const QString & searchFieldText)
{
    this->ledtFind->setText(searchFieldText);
}

void FindAndReplaceBar::setReplaceFieldText(const QString & replaceFieldText)
{
    this->ledtReplace->setText(replaceFieldText);
}

When I run the program I get problems of multiple definitions of the functions:

findNext(), findPrevious(), replace(), replaceAll().

The other definition is made in the moc_FindAndReplaceBar.cpp file.
I'm not sure what the problem so I don't know how to fix it! I would really appreciate any help, thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

萌辣 2025-01-01 00:25:44

根据您的连接调用(信号到信号)判断,我假设如果我们查看您的头文件,您将声明 findNext()、findPrevious()、replace()、replaceAll()信号,但是您不能实现信号 - 它们只需要在标头中声明即可。

来自 Qt 文档 信号

信号由 moc 自动生成,不得
在 .cpp 文件中实现。他们永远不能有返回类型(即
使用 void)。

Judging by your connect calls (signal to signal), I assume if we look at your header file, you will have declared findNext(), findPrevious(), replace(), replaceAll() as signals, but you must not implement signals - they just need to be declared in the header.

From the Qt docs on signals

Signals are automatically generated by the moc and must not be
implemented in the .cpp file. They can never have return types (i.e.
use void).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文