Signals won't trigger see code below (I am using Qt on OS X 10.8 amples app are working fine but this one won't...)
-
@#ifndef MAINWINDOW_H
#define MAINWINDOW_H//#include <QDialog>
#include <QtGui>class QCheckBox;
class QLabel;
class QLineEdit;
class QPushButton;class MainWindow : public QDialog
{
Q_OBJECTpublic:
MainWindow(QWidget *parent = 0);
~MainWindow();signals:
void findNext(const QString &str, Qt::CaseSensitivity cs);
void findPrevious(const QString &str, Qt::CaseSensitivity cs);
void textChanged(const QString &str);private slots:
void findClicked();
void enableFindButton(const QString &text);private:
QLabel *debugLabel;
QLabel *label;
QLineEdit *lineEdit;
QCheckBox *caseCheckBox;
QCheckBox *backwardCheckBox;
QPushButton *findButton;
QPushButton *closeButton;
};#endif // MAINWINDOW_H
#include <QtGui>
#include "mainwindow.h"MainWindow::MainWindow(QWidget *parent) : QDialog(parent)
{
debugLabel = new QLabel(tr("no debug info yet..."));
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")); /*bool r=false; r = QObject::connect(pObj1,SIGNAL(signalStuff()),pObj2,SLOT(gotStuff())); Q_ASSERT(r);*/ bool r=false; r = connect(lineEdit, SIGNAL(textChanged(QString &text)), this, SLOT(enableFindButton(QString &text))); Q_ASSERT(r); connect(findButton, SIGNAL(clicked()), this, SLOT(findClicked())); 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); leftLayout->addWidget(debugLabel); 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());
}
MainWindow::~MainWindow()
{}
void MainWindow::findClicked()
{
debugLabel->setText(tr("findClicked()"));
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 MainWindow::enableFindButton(const QString &text) {
debugLabel->setText(tr("enableFindButton()"));
findButton->setEnabled(!text.isEmpty());
}@
Thanks for your help,
Ludovic Vignals -
Ok found my two problems:
-
only custom signals need to be declared in class header file so I removed th below signal declaration
@
//void textChanged(const QString &str);
@ -
in the connect() that takes signal and slot with parameters I did not include the const keywork in front of the arguments the changed line look like this:
@
connect(lineEdit, SIGNAL(textChanged(const QString&)), this, SLOT(enableFindButton(const QString&)));
@
After making those changes the connection worked as expected.
Ludovic Vignals -
-
You need to match the call pattern exactly as it is.
The documentation of the "textChanged signal":http://qt-project.org/doc/qt-4.8/qlineedit.html#textChanged sais (const QString &), but you used (QString &). You should also leave the variable's name away.rewrite to
@
bool r=false;
r = connect(lineEdit, SIGNAL(textChanged(const QString &)), this, SLOT(enableFindButton(const QString &)));
Q_ASSERT(r);
@
The "signal clicked":http://qt-project.org/doc/qt-4.8/qabstractbutton.html#clicked has a bool in the call list. Use "pressed" or "released" signal. Their parameter list is empty.[too late]