Errors when building a dialog window

  • Hi guys,

    As I'm reading the book C++ GUI Programming with Qt 4 (2nd Edition), I tried to write the codes for a little useful project named FindDialog of that book. When I completed the codes and ran it I got many errors!

    I reviewed the codes of the files finddialog.h, finddialog.cpp and also main.cpp but didn't find any real issue.
    The only point I assumed to be odd is the // forward declarations of Qt classes in the file finddialog.h. Until this point of studying C++, I haven't seen that kind of declarations for classes (classes without curly brackets)!

    This is the code for finddialog.h:

    #ifndef FINDDIALOG_H
    #define FINDDIALOG_H
    #include <QDialog>
    //forward declarations of Qt classes
    class QPushButton;
    class QCheckBox;
    class QLineEdit;
    class QLabel;
    namespace Ui {
    class FindDialog;
    class FindDialog : public QDialog
        explicit FindDialog(QWidget *parent = 0);
        void FindNext(const QString&, const Qt::CaseSensitivity&);
        void FindPrevious(const QString&, const Qt::CaseSensitivity&);
    private slots:
        void FindCkicked();
        void EnableFindButton(const QString&);
        QLabel* label;
        QLineEdit* lineEdit;
        QCheckBox* caseCheckBox;
        QCheckBox* backwardCheckBox;
        QPushButton* findButton;
        QPushButton* closeButton;
        Ui::FindDialog *ui;
    #endif // FINDDIALOG_H

    This one is the code for finddialog.cpp:

    #include "finddialog.h"
    #include "ui_finddialog.h"
    #include <QtGui>
    FindDialog::FindDialog(QWidget *parent) :
        QDialog(parent), ui(new Ui::FindDialog)
        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 -> setEnable(false);
        closeButton = new QPushButton(tr("close"));
        connect(lineEdit, SIGNAL(textChanged(const Qstring&)), this, SLOT(EnableFindButton(const QString&)));
        connect(findButton, SIGNAL(clicked()), this, SLOT(FindCkicked()));
        connect(closeButton, SIGNAL(clicked()), SLOT(close()));
        QHBoxLayout* topLeftLayout = new QHBoxLayout;
        topLeftLayout -> addWidget(label);
        topLeftLayout -> addWidget(lineEdit);
        QVBoxLayout* leftLayout = new QVBoxLayout;
        leftLayout -> addWidget(topLeftLayout);
        leftLayout -> addWidget(caseCheckBox);
        leftLayout -> addWidget(backwardCheckBox);
        QVBoxLayout* rightLayout = new QVBoxLayout;
        rightLayout -> addWidget(findButton);
        rightLayout -> addWidget(closeButton);
        rightLayout -> addStreach();
        QHBoxLayout* mainLayout = new QHBoxLayout;
        mainLayout -> addWidget(leftLayout);
        mainLayout -> addWidget(rightLayout);
    void FindDialog::FindCkicked()
        QString text = lineEdit -> text();
        Qt::CaseSensitivity cs =
             caseCheckBox -> isChecked() ? Qt::CaseSensitive : Qt::CaseInsensitive;
        if(backwardCheckBox -> isChecked())
            emit findPrevious(text, cs);
        else FindNext(text, cs);
    void FindDialog::EnableFindButton(const QString& text)
       findButton -> setEnabled(!text.isEmpty());
        delete ui;

    And this one is for main.cpp:

    #include "finddialog.h"
    #include <QApplication>
    int main(int argc, char *argv[])
        QApplication app(argc, argv);
        FindDialog* dialog = new FindDialog;
        dialog -> show();
        return app.exec();

    I get 51 errors when running! (Goodness!)
    I wrote the errors #1 and #2 here. If you guide me on these two errors (please) I try to find the issues of the rest by myself :-)

    #1: C:\Users\tomy\Documents\Qt\FindDialog\finddialog.cpp:8: error: invalid use of incomplete type 'class QLabel'
    label = new QLabel(tr("Find &what"));

    #2: C:\Users\tomy\Documents\Qt\FindDialog\finddialog.h:10: error: forward declaration of 'class QLabel'
    class QLabel;

    Thanks so much in advance.

    PS: I use a Windows machine and my IDE is Qt Creator 3.6.0.

  • Hi @tomy

    Instead of class..use include. Forward declaration in .h do not work.

    #include < QLabel> //class QLabel;
    #include <QPushButton>
    #include< QCheckBox>
    #include <QLineEdit>

  • Moderators

    It is perfectly fine to use forward declarations in the *.h file. But you then have to include all the needed header files in the *.cpp file (for example #include <QLabel>).

  • @tomy ,

    Just information, I guess this is example from the book which is completely with Qt4. But if your are working with Qt5, then they are few things to change when you compiling the same code from book. Like "Add Qt += widgets in the .pro file ". I found a link1 and link2 that can tell you the differences.

  • @jsulm said:

    It is perfectly fine to use forward declarations in the *.h file. But you then have to include all the needed header files in the *.cpp file (for example #include <QLabel>).

    Thanks for it. I added those header files. Now all the header files of my .cpp file are as follows:

    #include "ui_finddialog.h"
    #include <QtWidgets>
    #include <QLabel>
    #include <QPushButton>
    #include <QLineEdit>
    #include <QCheckBox>
    #include <QHBoxLayout>

    And also I found some syntax errors. Now I have only 3 errors on the project all pointing to the same matter:
    This is one of them related to the statement leftLayout -> addWidget(topLeftLayout); of finddialog.cpp

    C:\Users\tomy\Documents\Qt\FindDialog\finddialog.cpp:34: error: no matching function for call to 'QVBoxLayout::addWidget(QHBoxLayout&)'
    leftLayout -> addWidget(topLeftLayout);
    ^ *

    I think this error is reasonable, because the argument list of the addWidget() function is as follows:
    void addWidget(QWidget*, int stretch = 0, Qt::Alignment alignment = 0)

    That is we must at least supply a QWidget* in the addWidget of
    leftLayout -> addWidget(topLeftLayout);
    But topLeftLayout is not a pointer to QWidget! So the error is reasonable.

    The variable topLeftLayout is a pointer to QHBoxLayout which in turn inherits from QBoxLayout that inherits from QLayout which inherits from QObject and QLayoutiten.
    There is no QWidget inheritance!


    "Add Qt += widgets in the .pro file "

    The .pro file contains the Qt += widgets already.

  • Hi @tomy

    yes you are rght. leftLayout -> addWidget(widget_name); //this is only for widgets

    We have to use leftLayout -> addLayout(topLeftLayout) // for adding layout

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.