[SOLVED] Qt beginner question: findNext and findPrevious "identifier not found"
-
Dear all,
This is my first post here, apologies for formatting mistakes or similar things. I'm trying to compile and run one of the first examples from the book "C++ GUI Programming with Qt 4". I'm using an up-to-date Qt Creator 2.7.0, based on Qt 5.0.2 (32 bit), all running on a Windows 8 (shame on me) machine. Everything is out of a fresh install; my understanding is that it should work fine without further configuration, although I have some doubts. Simpler Qt applications (previous examples in the book) run fine in this configuration.
I tried Build->Clean All, Build->Run qmake, Build->Build Project. I initially got many errors as apparently "#include<QtGui>" didn't work as intended; but I could solve most errors by including all the relevant classes. I have two errors left:
@error: C3861: 'findPrevious': identifier not found
error: C3861: 'findNext': identifier not found@If I comment out the lines producing the error, the program compiles, but crashes:
@Starting C:\Qt\Qt502\Tools\QtCreator\bin\build-QtTest-Desktop_Qt_5_0_2_MSVC2012_64bit-Debug\debug\QtTest...
The program has unexpectedly finished.
C:\Qt\Qt502\Tools\QtCreator\bin\build-QtTest-Desktop_Qt_5_0_2_MSVC2012_64bit-Debug\debug\QtTest exited with code -1073741819@The complete code should be identical to that in the book (except I used "testdialog" instead of "finddialog" and had to add a bunch of includes), and is as follows:
main.cpp:
@
#include <QApplication>
#include "testdialog.h"int main(int argc, char *argv[])
{
QApplication a(argc, argv);
TestDialog *dialog = new TestDialog(0);
dialog->show();
return a.exec();
}
@textdialog.h:
@
#ifndef TESTDIALOG_H
#define TESTDIALOG_H#include <QDialog>
class QCheckBox;
class QLabel;
class QLineEdit;
class QPushButton;class TestDialog : public QDialog
{
//Provides ~events/delegates (SIGNALS/SLOTS) with qmake.
Q_OBJECTpublic:
explicit TestDialog(QWidget *parent = 0);signals:
public slots:
private slots:
void testClicked();
void enableTestButton(const QString &text);private:
QLabel *label;
QLineEdit *lineEdit;
QCheckBox *caseCheckBox;
QCheckBox *backwardCheckBox;
QPushButton *testButton;
QPushButton *closeButton;
};#endif // TESTDIALOG_H
@testdialog.cpp:
@
#include <QtGui> //Doesn't seem to work. I get errors for basically every class. Had to "manually" include each class.
#include <QCheckBox>
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QString>
#include "testdialog.h"TestDialog::TestDialog(QWidget *parent) :
QDialog(parent)
{
label = new QLabel(tr("Find &what:"));
label->setBuddy(lineEdit);
caseCheckBox = new QCheckBox(tr("Match &case"));
backwardCheckBox = new QCheckBox(tr("Search &backward"));testButton = new QPushButton(tr("&Find")); testButton->setDefault(true); testButton->setEnabled(false); closeButton = new QPushButton(tr("Close")); connect(lineEdit, SIGNAL(textChanged(const QString &)), this, SLOT(enableFindButton(const Qstring &))); connect(testButton, SIGNAL(clicked(const QString &)), 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); QVBoxLayout *rightLayout = new QVBoxLayout(); rightLayout->addWidget(testButton); rightLayout->addWidget(closeButton); rightLayout->addStretch(); QHBoxLayout *mainLayout = new QHBoxLayout(); mainLayout->addLayout(leftLayout); mainLayout->addLayout(rightLayout); setLayout(mainLayout); setWindowTitle(tr("Find")); setFixedHeight(sizeHint().height());
}
void TestDialog::testClicked()
{
QString text = lineEdit->text();
Qt::CaseSensitivity cs = caseCheckBox->isChecked() ? Qt::CaseSensitive : Qt::CaseInsensitive;
if(backwardCheckBox->isChecked())
{
emit findPrevious(text, cs); //error 1
}
else
{
emit findNext(text, cs); //error 2
}
}void TestDialog::enableTestButton(const QString &text)
{
testButton->setEnabled(!text.isEmpty());
}
@Thanks in advance.
-
I feel stupid for missing the signals declaration; I just assumed that somehow these specific signals were included in Qt. The program now compiles fine, although it still crashes once compiled. I'll look into why it crashes before I call this "solved", might be something as simple as what you just mentioned. Thanks!
-
I would say: lineEdit not initialized before you use it
-
Your welcome !
Don't forget the debugger, he's (almost) always your friend :)