Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Errors when building a dialog window
Forum Updated to NodeBB v4.3 + New Features

Errors when building a dialog window

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 3 Posters 1.8k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • tomyT Offline
    tomyT Offline
    tomy
    wrote on last edited by tomy
    #1

    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
    {
        Q_OBJECT
    
    public:
        explicit FindDialog(QWidget *parent = 0);
        ~FindDialog();
    signals:
        void FindNext(const QString&, const Qt::CaseSensitivity&);
        void FindPrevious(const QString&, const Qt::CaseSensitivity&);
    private slots:
        void FindCkicked();
        void EnableFindButton(const QString&);
    private:
        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);
        setLayout(mainLayout);
    
        setWindowTitle(tr("Find"));
        setFixedHeight(sizeHint().height());
    
        ui->setupUi(this);
    }
    
    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());
    }
    
    FindDialog::~FindDialog()
    {
        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.

    1 Reply Last reply
    0
    • Ni.SumiN Offline
      Ni.SumiN Offline
      Ni.Sumi
      wrote on last edited by Ni.Sumi
      #2

      Hi @tomy

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

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

      1 Reply Last reply
      0
      • jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on last edited by
        #3

        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>).

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        1
        • Ni.SumiN Offline
          Ni.SumiN Offline
          Ni.Sumi
          wrote on last edited by Ni.Sumi
          #4

          @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.

          1 Reply Last reply
          0
          • tomyT Offline
            tomyT Offline
            tomy
            wrote on last edited by tomy
            #5

            @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!

            @Ni.Sumi

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

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

            1 Reply Last reply
            0
            • Ni.SumiN Offline
              Ni.SumiN Offline
              Ni.Sumi
              wrote on last edited by
              #6

              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

              1 Reply Last reply
              1

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved