i downloaded a book about qt programming but its outdated because its version is something like qt4. while i got the latest Qt5.0.2 creator and compiler which i downloaded from qt-project.org/downloads. I've read from chapter 1 and everything seems to work fine but when i reached chapter 2, which includes header, thebody and the main, i received a lot of errors so here is my code...im running windows
in the creator i clicked--File->New Project->Other Project->Empty Qt Project->then i named it finddialog->next2x->Finished
then in the project named finddialog i right clicked it->Add New->C++ Source File->i named it main.cpp->then finished.
then in the project named finddialog again i right clicked it->Add New->C++ Class->class name: finddialog, base class: i chose QWidget->then Finished.
so here is the code in the header file...
#ifndef FINDDIALOG_H
2 #define FINDDIALOG_H
3 #include <QDialog>
8 class FindDialog
: public QDialog 9 {
10 Q_OBJECT
11 public:
12 FindDialog
(QWidget *parent
= 0);
13 signals:
14 void findNext
(const QString &str, Qt
::CaseSensitivity cs
);
15 void findPrevious
(const QString &str, Qt
::CaseSensitivity cs
);
16 private slots:
17 void findClicked();
18 void enableFindButton
(const QString &text
);
19 private:
26 };
27 #endif
#ifndef FINDDIALOG_H
2 #define FINDDIALOG_H
3 #include <QDialog>
4 class QCheckBox;
5 class QLabel;
6 class QLineEdit;
7 class QPushButton;
8 class FindDialog : public QDialog
9 {
10 Q_OBJECT
11 public:
12 FindDialog(QWidget *parent = 0);
13 signals:
14 void findNext(const QString &str, Qt::CaseSensitivity cs);
15 void findPrevious(const QString &str, Qt::CaseSensitivity cs);
16 private slots:
17 void findClicked();
18 void enableFindButton(const QString &text);
19 private:
20 QLabel *label;
21 QLineEdit *lineEdit;
22 QCheckBox *caseCheckBox;
23 QCheckBox *backwardCheckBox;
24 QPushButton *findButton;
25 QPushButton *closeButton;
26 };
27 #endif
To copy to clipboard, switch view to plain text mode
then in the finddialog.cpp here is the code
1 #include <QtGui>
2 #include "finddialog.h"
3 FindDialog
::FindDialog(QWidget *parent
) 5 {
6 label
= new QLabel(tr
("Find &what:"));
8 label->setBuddy(lineEdit);
9 caseCheckBox
= new QCheckBox(tr
("Match &case"));
10 backwardCheckBox
= new QCheckBox(tr
("Search &backward"));
12 findButton->setDefault(true);
13 findButton->setEnabled(false);
15 connect(lineEdit,
SIGNAL(textChanged
(const QString &)),
16 this,
SLOT(enableFindButton
(const QString &)));
17 connect(findButton, SIGNAL(clicked()),
18 this, SLOT(findClicked()));
19 connect(closeButton, SIGNAL(clicked()),
20 this, SLOT(close()));
22 topLeftLayout->addWidget(label);
23 topLeftLayout->addWidget(lineEdit);
25 leftLayout->addLayout(topLeftLayout);
26 leftLayout->addWidget(caseCheckBox);
27 leftLayout->addWidget(backwardCheckBox);
29 rightLayout->addWidget(findButton);
30 rightLayout->addWidget(closeButton);
31 rightLayout->addStretch();
33 mainLayout->addLayout(leftLayout);
34 mainLayout->addLayout(rightLayout);
35 setLayout(mainLayout);
36 setWindowTitle(tr("Find"));
37 setFixedHeight(sizeHint().height());
38 }
39 void FindDialog::findClicked()
40 {
41 QString text
= lineEdit
->text
();
42 Qt::CaseSensitivity cs =
43 caseCheckBox->isChecked() ? Qt::CaseSensitive
44 : Qt::CaseInsensitive;
45 if (backwardCheckBox->isChecked()) {
46 emit findPrevious(text, cs);
47 } else {
48 emit findNext(text, cs);
49 }
50 }
51 void FindDialog
::enableFindButton(const QString &text
) 52 {
53 findButton->setEnabled(!text.isEmpty());
54 }
1 #include <QtGui>
2 #include "finddialog.h"
3 FindDialog::FindDialog(QWidget *parent)
4 : QDialog(parent)
5 {
6 label = new QLabel(tr("Find &what:"));
7 lineEdit = new QLineEdit;
8 label->setBuddy(lineEdit);
9 caseCheckBox = new QCheckBox(tr("Match &case"));
10 backwardCheckBox = new QCheckBox(tr("Search &backward"));
11 findButton = new QPushButton(tr("&Find"));
12 findButton->setDefault(true);
13 findButton->setEnabled(false);
14 closeButton = new QPushButton(tr("Close"));
15 connect(lineEdit, SIGNAL(textChanged(const QString &)),
16 this, SLOT(enableFindButton(const QString &)));
17 connect(findButton, SIGNAL(clicked()),
18 this, SLOT(findClicked()));
19 connect(closeButton, SIGNAL(clicked()),
20 this, SLOT(close()));
21 QHBoxLayout *topLeftLayout = new QHBoxLayout;
22 topLeftLayout->addWidget(label);
23 topLeftLayout->addWidget(lineEdit);
24 QVBoxLayout *leftLayout = new QVBoxLayout;
25 leftLayout->addLayout(topLeftLayout);
26 leftLayout->addWidget(caseCheckBox);
27 leftLayout->addWidget(backwardCheckBox);
28 QVBoxLayout *rightLayout = new QVBoxLayout;
29 rightLayout->addWidget(findButton);
30 rightLayout->addWidget(closeButton);
31 rightLayout->addStretch();
32 QHBoxLayout *mainLayout = new QHBoxLayout;
33 mainLayout->addLayout(leftLayout);
34 mainLayout->addLayout(rightLayout);
35 setLayout(mainLayout);
36 setWindowTitle(tr("Find"));
37 setFixedHeight(sizeHint().height());
38 }
39 void FindDialog::findClicked()
40 {
41 QString text = lineEdit->text();
42 Qt::CaseSensitivity cs =
43 caseCheckBox->isChecked() ? Qt::CaseSensitive
44 : Qt::CaseInsensitive;
45 if (backwardCheckBox->isChecked()) {
46 emit findPrevious(text, cs);
47 } else {
48 emit findNext(text, cs);
49 }
50 }
51 void FindDialog::enableFindButton(const QString &text)
52 {
53 findButton->setEnabled(!text.isEmpty());
54 }
To copy to clipboard, switch view to plain text mode
and the last one in is the main.cpp here it is....
1 #include <QWidgets/QApplication>
2 #include "finddialog.h"
3 int main(int argc, char *argv[])
4 {
6 FindDialog *dialog = new FindDialog;
7 dialog->show();
8 return app.exec();
9 }
1 #include <QWidgets/QApplication>
2 #include "finddialog.h"
3 int main(int argc, char *argv[])
4 {
5 QApplication app(argc, argv);
6 FindDialog *dialog = new FindDialog;
7 dialog->show();
8 return app.exec();
9 }
To copy to clipboard, switch view to plain text mode
When run and compile this i got 46 errors.
In the .pro file i did type this QT += widgets
Did i miss something more important? The codes the i entered above will surely works on qt4 but im currently running the latest Qt5.0.2 packaged which i thought will not give me problems...
Bookmarks