读书人

qt 关于 Object:connect: No such slo

发布时间: 2013-04-20 19:43:01 作者: rapoo

qt 关于 Object::connect: No such slot QDialog::findClicked()
---------------------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();
}

-----------------------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 findPrevious(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------------------------
#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("Search &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(clicked()),
this, SLOT(findClicked()));
connect(closeButton, SIGNAL(clicked()),
this, SLOT(findClicked()));

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);
rightLayout->addStretch();

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

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

}

void FindDialog::findClicked()
{
QMessageBox::question(this,"quit","quit",QMessageBox::Ok | QMessageBox::Cancel ,QMessageBox::Ok);
QString text = lineEdit->text();
Qt::CaseSensitivity cs =
caseCheckBox->isChecked() ? Qt::CaseSensitive
: Qt::CaseInsensitive;
if (backwardCheckBox->isChecked()) {
emit findPrevious(text, cs);
} else {
emit findNext(text, cs);
}
}

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

void FindDialog::findNext(const QString &str, Qt::CaseSensitivity cs)
{
QMessageBox::question(this,"quit","quit",QMessageBox::Ok | QMessageBox::Cancel ,QMessageBox::Ok);
}

void FindDialog::findPrevious(const QString &str, Qt::CaseSensitivity cs)
{
QMessageBox::question(this,"quit","quit",QMessageBox::Ok | QMessageBox::Cancel ,QMessageBox::Ok);
}

如上代码是C++GUI Qt4编程第二版的。 随书源代码的第二章的一个find列子。
我用arm-hismall-linux-g++编译好了。在hi3515板子上跑的时候怎么总是提示Object::connect: No such slot QDialog::findClicked() 这个呢。 是哪个信号-槽的问题,能编译能运行,但是按钮没有相应,应该是信号-槽的设置没好。 各位高手达人们,求解答。 qt4? connect:?Nosuch?slot
[解决办法]
// Q_OBJECT
去掉注释看看
[解决办法]
// Q_OBJECT
你犯错误了,这个宏必须有!!!
在有你自定义signal和slots的Qt派生类里必须有这个宏。这可是Qt最基本原则
[解决办法]
// Q_OBJECT
去掉注释,使用信号槽机制必须要有Q_OBJECT
并使用moc生成文件
[解决办法]

引用:
引用:// Q_OBJECT
去掉注释看看
去掉后,用moc编译后,生成了moc_源文件, 在用arm-hismall-linux-g++编译的时候,moc_源文件报错。 还提示moc编译器 版本低了。 这个宏应该不会影响吧。 我自己实现了信号的代码。 我也不清楚,哪个Q_OBJECT 有没有影响的。

这样的话只好升级QtSDK版本喽。
[解决办法]
Q_OBJECT不能省的。。要不就没有meta信息了。

读书人网 >QT开发

热点推荐