web-dev-qa-db-fra.com

Erreur Qt: utilisation non valide du type incomplet 'classe QLabel'

J'essaie de compiler le programme suivant à l'aide de QtCreater mais je reçois beaucoup d'erreurs dans ce programme. J'ai eu ce programme dans un livre et je ne suis pas capable de comprendre où se trouve l'erreur. Quelqu'un peut aider à déboguer ce programme.

Voici le FindDialog.h

#ifndef FINDDIALOG_H
#define FINDDIALOG_H

#include <QDialog>

class QCheckbox;
class QLabel;
class QLineEdit;
class QPushButton;

class FindDialog : public QDialog
{
    Q_OBJECT

public:
    FindDialog(QWidget *parent = 0);

signals:
    void findNext(const QString &str, Qt::CaseSensitivity cs);
    void findPrevious(const QString &str, Qt::CaseSensitivity cs);

private slots:
    void findClicked();
    void enableFindButton(const QString &text);

private:
    QLabel *label;
    QLineEdit *lineEdit;
    QCheckbox *caseCheckBox;
    QCheckbox *backwardCheckBox;
    QPushButton *findButton;
    QPushButton *closeButton;
};

#endif

FindDialog.cpp

#include <QtGui>

#include "finddialog.h"

FindDialog::FindDialog(QWidget *parent) : QDialog(parent)
{
    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"));

    connect(lineEdit, SIGNAL(textChanged(const QString &)), 
            this, SLOT(enableFindButton(const QString &)));

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

    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());
}

void FindDialog::findClicked()
{
    QString text = lineEdit->text();
    Qt::CaseSensitivity cs = caseCheckBox->isChecked() ? Qt::CaseSensitivity
                                                       : Qt::CaseInsensitive;
    if(backwardCheckBox->isChecked()) {
        emit findPrevious(text, cs);
    } else {
        emit findNext(text, cs);
    }
}

void FindDialog::enableFindButton(const QString &text)
{
    findButton->setEnabled(!text.isEmpty());
}

main.cpp

#include <QApplication>

#include "findDialog.h"

int main (int argc, char *argv[])
{
    QApplication app(argc, argv);
    FindDialog *dialog = new FindDialog;
    dialog->show();
    return app.exec();
}

Les erreurs que je reçois sont les suivantes:

finddialog.cpp:21:32: note: candidates are:
In file included from /usr/include/qt5/QtCore/qabstractanimation.h:45:0,
                 from /usr/include/qt5/QtCore/QtCore:4,
                 from /usr/include/qt5/QtGui/QtGui:4,
                 from finddialog.cpp:1:
/usr/include/qt5/QtCore/qobject.h:199:36: note: static QMetaObject::Connection QObject::connect(const QObject*, const char*, const QObject*, const char*, Qt::ConnectionType)
     static QMetaObject::Connection connect(const QObject *sender, const char *signal,
                                    ^
/usr/include/qt5/QtCore/qobject.h:199:36: note:   no known conversion for argument 1 from ‘QPushButton*’ to ‘const QObject*’
/usr/include/qt5/QtCore/qobject.h:202:36: note: static QMetaObject::Connection QObject::connect(const QObject*, const QMetaMethod&, const QObject*, const QMetaMethod&, Qt::ConnectionType)
     static QMetaObject::Connection connect(const QObject *sender, const QMetaMethod &signal,
                                    ^
/usr/include/qt5/QtCore/qobject.h:202:36: note:   no known conversion for argument 1 from ‘QPushButton*’ to ‘const QObject*’
/usr/include/qt5/QtCore/qobject.h:418:32: note: QMetaObject::Connection QObject::connect(const QObject*, const char*, const char*, Qt::ConnectionType) const
 inline QMetaObject::Connection QObject::connect(const QObject *asender, const char *asignal,
                                ^
/usr/include/qt5/QtCore/qobject.h:418:32: note:   no known conversion for argument 1 from ‘QPushButton*’ to ‘const QObject*’
/usr/include/qt5/QtCore/qobject.h:215:43: note: template<class Func1, class Func2> static QMetaObject::Connection QObject::connect(const typename QtPrivate::FunctionPointer<Func>::Object*, Func1, const typename QtPrivate::FunctionPointer<Func2>::Object*, Func2, Qt::ConnectionType)
     static inline QMetaObject::Connection connect(const typename QtPrivate::FunctionPointer<Func1>::Object *sender, Func1 signal,
                                           ^
/usr/include/qt5/QtCore/qobject.h:215:43: note:   template argument deduction/substitution failed:
/usr/include/qt5/QtCore/qobject.h: In substitution of ‘template<class Func1, class Func2> static QMetaObject::Connection QObject::connect(const typename QtPrivate::FunctionPointer<Func>::Object*, Func1, const typename QtPrivate::FunctionPointer<Func2>::Object*, Func2, Qt::ConnectionType) [with Func1 = const char*; Func2 = const char*]’:
finddialog.cpp:21:32:   required from here
/usr/include/qt5/QtCore/qobject.h:215:43: error: no type named ‘Object’ in ‘struct QtPrivate::FunctionPointer<const char*>’
/usr/include/qt5/QtCore/qobject.h:245:13: note: template<class Func1, class Func2> static typename QtPrivate::QEnableIf<((int)(QtPrivate::FunctionPointer<Func2>::ArgumentCount) >= 0), QMetaObject::Connection>::Type QObject::connect(const typename QtPrivate::FunctionPointer<Func>::Object*, Func1, Func2)
             connect(const typename QtPrivate::FunctionPointer<Func1>::Object *sender, Func1 signal, Func2 slot)
             ^
/usr/include/qt5/QtCore/qobject.h:245:13: note:   template argument deduction/substitution failed:
finddialog.cpp:21:32: note:   candidate expects 3 arguments, 4 provided
       this, SLOT(findClicked()));
                                ^
In file included from /usr/include/qt5/QtCore/qabstractanimation.h:45:0,
                 from /usr/include/qt5/QtCore/QtCore:4,
                 from /usr/include/qt5/QtGui/QtGui:4,
                 from finddialog.cpp:1:
/usr/include/qt5/QtCore/qobject.h:268:13: note: template<class Func1, class Func2> static typename QtPrivate::QEnableIf<(QtPrivate::FunctionPointer<Func2>::ArgumentCount == (-1)), QMetaObject::Connection>::Type QObject::connect(const typename QtPrivate::FunctionPointer<Func>::Object*, Func1, Func2)
             connect(const typename QtPrivate::FunctionPointer<Func1>::Object *sender, Func1 signal, Func2 slot)
             ^
/usr/include/qt5/QtCore/qobject.h:268:13: note:   template argument deduction/substitution failed:
finddialog.cpp:21:32: note:   candidate expects 3 arguments, 4 provided
       this, SLOT(findClicked()));
                                ^
finddialog.cpp:23:61: error: no matching function for call to ‘FindDialog::connect(QPushButton*&, const char [11], FindDialog* const, const char [9])’
  connect(closeButton, SIGNAL(clicked()), this, SLOT(close()));
                                                             ^
finddialog.cpp:23:61: note: candidates are:
In file included from /usr/include/qt5/QtCore/qabstractanimation.h:45:0,
                 from /usr/include/qt5/QtCore/QtCore:4,
                 from /usr/include/qt5/QtGui/QtGui:4,
                 from finddialog.cpp:1:
/usr/include/qt5/QtCore/qobject.h:199:36: note: static QMetaObject::Connection QObject::connect(const QObject*, const char*, const QObject*, const char*, Qt::ConnectionType)
     static QMetaObject::Connection connect(const QObject *sender, const char *signal,
                                    ^
/usr/include/qt5/QtCore/qobject.h:199:36: note:   no known conversion for argument 1 from ‘QPushButton*’ to ‘const QObject*’
/usr/include/qt5/QtCore/qobject.h:202:36: note: static QMetaObject::Connection QObject::connect(const QObject*, const QMetaMethod&, const QObject*, const QMetaMethod&, Qt::ConnectionType)
     static QMetaObject::Connection connect(const QObject *sender, const QMetaMethod &signal,
                                    ^
/usr/include/qt5/QtCore/qobject.h:202:36: note:   no known conversion for argument 1 from ‘QPushButton*’ to ‘const QObject*’
/usr/include/qt5/QtCore/qobject.h:418:32: note: QMetaObject::Connection QObject::connect(const QObject*, const char*, const char*, Qt::ConnectionType) const
 inline QMetaObject::Connection QObject::connect(const QObject *asender, const char *asignal,
                                ^
/usr/include/qt5/QtCore/qobject.h:418:32: note:   no known conversion for argument 1 from ‘QPushButton*’ to ‘const QObject*’
/usr/include/qt5/QtCore/qobject.h:215:43: note: template<class Func1, class Func2> static QMetaObject::Connection QObject::connect(const typename QtPrivate::FunctionPointer<Func>::Object*, Func1, const typename QtPrivate::FunctionPointer<Func2>::Object*, Func2, Qt::ConnectionType)
     static inline QMetaObject::Connection connect(const typename QtPrivate::FunctionPointer<Func1>::Object *sender, Func1 signal,
                                           ^
/usr/include/qt5/QtCore/qobject.h:215:43: note:   template argument deduction/substitution failed:
/usr/include/qt5/QtCore/qobject.h: In substitution of ‘template<class Func1, class Func2> static QMetaObject::Connection QObject::connect(const typename QtPrivate::FunctionPointer<Func>::Object*, Func1, const typename QtPrivate::FunctionPointer<Func2>::Object*, Func2, Qt::ConnectionType) [with Func1 = const char*; Func2 = const char*]’:
finddialog.cpp:23:61:   required from here
/usr/include/qt5/QtCore/qobject.h:215:43: error: no type named ‘Object’ in ‘struct QtPrivate::FunctionPointer<const char*>’
/usr/include/qt5/QtCore/qobject.h:245:13: note: template<class Func1, class Func2> static typename QtPrivate::QEnableIf<((int)(QtPrivate::FunctionPointer<Func2>::ArgumentCount) >= 0), QMetaObject::Connection>::Type QObject::connect(const typename QtPrivate::FunctionPointer<Func>::Object*, Func1, Func2)
             connect(const typename QtPrivate::FunctionPointer<Func1>::Object *sender, Func1 signal, Func2 slot)
             ^
/usr/include/qt5/QtCore/qobject.h:245:13: note:   template argument deduction/substitution failed:
finddialog.cpp:23:61: note:   candidate expects 3 arguments, 4 provided
  connect(closeButton, SIGNAL(clicked()), this, SLOT(close()));
                                                             ^
In file included from /usr/include/qt5/QtCore/qabstractanimation.h:45:0,
                 from /usr/include/qt5/QtCore/QtCore:4,
                 from /usr/include/qt5/QtGui/QtGui:4,
                 from finddialog.cpp:1:
/usr/include/qt5/QtCore/qobject.h:268:13: note: template<class Func1, class Func2> static typename QtPrivate::QEnableIf<(QtPrivate::FunctionPointer<Func2>::ArgumentCount == (-1)), QMetaObject::Connection>::Type QObject::connect(const typename QtPrivate::FunctionPointer<Func>::Object*, Func1, Func2)
             connect(const typename QtPrivate::FunctionPointer<Func1>::Object *sender, Func1 signal, Func2 slot)
             ^
/usr/include/qt5/QtCore/qobject.h:268:13: note:   template argument deduction/substitution failed:
finddialog.cpp:23:61: note:   candidate expects 3 arguments, 4 provided
  connect(closeButton, SIGNAL(clicked()), this, SLOT(close()));
                                                             ^
finddialog.cpp:25:2: error: ‘QHBoxLayout’ was not declared in this scope
  QHBoxLayout *topLeftLayout = new QHBoxLayout;
  ^
finddialog.cpp:25:15: error: ‘topLeftLayout’ was not declared in this scope
  QHBoxLayout *topLeftLayout = new QHBoxLayout;
               ^
finddialog.cpp:25:35: error: expected type-specifier before ‘QHBoxLayout’
  QHBoxLayout *topLeftLayout = new QHBoxLayout;
                                   ^
finddialog.cpp:25:35: error: expected ‘;’ before ‘QHBoxLayout’
finddialog.cpp:29:2: error: ‘QVBoxLayout’ was not declared in this scope
  QVBoxLayout *leftLayout = new QVBoxLayout;
  ^
finddialog.cpp:29:15: error: ‘leftLayout’ was not declared in this scope
  QVBoxLayout *leftLayout = new QVBoxLayout;
               ^
finddialog.cpp:29:32: error: expected type-specifier before ‘QVBoxLayout’
  QVBoxLayout *leftLayout = new QVBoxLayout;
                                ^
finddialog.cpp:29:32: error: expected ‘;’ before ‘QVBoxLayout’
finddialog.cpp:34:15: error: ‘rightLayout’ was not declared in this scope
  QVBoxLayout *rightLayout = new QVBoxLayout;
               ^
finddialog.cpp:34:33: error: expected type-specifier before ‘QVBoxLayout’
  QVBoxLayout *rightLayout = new QVBoxLayout;
                                 ^
finddialog.cpp:34:33: error: expected ‘;’ before ‘QVBoxLayout’
finddialog.cpp:39:15: error: ‘mainLayout’ was not declared in this scope
  QHBoxLayout *mainLayout = new QHBoxLayout;
               ^
finddialog.cpp:39:32: error: expected type-specifier before ‘QHBoxLayout’
  QHBoxLayout *mainLayout = new QHBoxLayout;
                                ^
finddialog.cpp:39:32: error: expected ‘;’ before ‘QHBoxLayout’
finddialog.cpp: In member function ‘void FindDialog::findClicked()’:
finddialog.cpp:50:25: error: invalid use of incomplete type ‘class QLineEdit’
  QString text = lineEdit->text();
                         ^
In file included from finddialog.cpp:3:0:
finddialog.h:8:7: error: forward declaration of ‘class QLineEdit’
 class QLineEdit;
       ^
finddialog.cpp:51:39: error: invalid use of incomplete type ‘class QCheckbox’
  Qt::CaseSensitivity cs = caseCheckBox->isChecked() ? Qt::CaseSensitivity
                                       ^
In file included from finddialog.cpp:3:0:
finddialog.h:6:7: error: forward declaration of ‘class QCheckbox’
 class QCheckbox;
       ^
finddialog.cpp:52:17: error: expected primary-expression before ‘:’ token
                 : Qt::CaseInsensitive;
                 ^
finddialog.cpp:53:21: error: invalid use of incomplete type ‘class QCheckbox’
  if(backwardCheckBox->isChecked()) {
                     ^
In file included from finddialog.cpp:3:0:
finddialog.h:6:7: error: forward declaration of ‘class QCheckbox’
 class QCheckbox;
       ^
finddialog.cpp: In member function ‘void FindDialog::enableFindButton(const QString&)’:
finddialog.cpp:62:12: error: invalid use of incomplete type ‘class QPushButton’
  findButton->setEnabled(!text.isEmpty());
            ^
In file included from /usr/include/qt5/QtWidgets/QDialog:1:0,
                 from finddialog.h:4,
                 from finddialog.cpp:3:
/usr/include/qt5/QtWidgets/qdialog.h:52:7: error: forward declaration of ‘class QPushButton’
 class QPushButton;
       ^
make: *** [finddialog.o] Error 1
9
Hell Man

L’erreur devrait provenir de votre fichier .pro puisque vous utilisez Qt5, vous devriez inclure:

QT += widgets

Changer toutes les instances de

#include <QtGui>

à

#include <QtWidgets>

Mais il est bien préférable d’inclure le fichier dont vous avez besoin à la place de l’ensemble de QtGui ou QtWidgets.

10
Kikohs

Je lis le même livre. Il y a deux choses à corriger pour que cet exemple fonctionne. 

  1. Les déclarations en aval de la classe dans le fichier d'en-tête findDialog.h ne fonctionnent pas. Par conséquent, aucun constructeur pour QLabel, etc. ne peut être trouvé. Remplacez chaque classe par le #include correspondant comme suit. (QHBoxLayout et QVBoxLayout ne sont pas nécessaires dans le fichier finddialog.h, mais par souci de commodité, je les ai déplacés de sorte que tous les éléments inclus se trouvent au même endroit).

    #include <QDialog>
    #include <QCheckBox>    //class QCheckBox;  
    #include <QLabel>       //class QLabel;     
    #include <QLineEdit>    //class QLineEdit;  
    #include <QPushButton>  //class QPushButton; 
    #include <QHBoxLayout>  //in the finddialog.cpp
    #include <QVBoxLayout>  //in the finddialog.cpp
    
  2. Editez le fichier .pro dans ceci:

    greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
    
    TARGET   = find 
    
    TEMPLATE = app
    
    HEADERS  = finddialog.h
    
    SOURCES  = finddialog.cpp \
               main.cpp
    
  3. Supprimez tous les anciens makefiles et find.pro.user et reconstruisez le projet.

6
robopoulos

Vous devez modifier de nombreux éléments lorsque vous passez de Qt4. Vous trouverez ci-dessous une liste basée sur mon expérience (from http://www.gpupowered.org/node/23 ). Ce n'est pas une liste complète par tous les moyens.

  1. Erreurs dans la définition QtGui for QWidget. Ajoutez Qt + = widgets dans le fichier .pro comme déjà suggéré.

  2. Certains, comme #include "QGraphicsItem", ont besoin de QtWidgets/QGraphicsItem, QtWidgets/QVBoxLayout 

  3. QString :: toAscii () == QString :: toLatin1 () dans xgxperfserver.cpp

  4. Qt + = widgets nécessaires pour éviter les erreurs de liaison pour tous les widgets

  5. Projet ERREUR: module (s) inconnu (s) dans QT: svg == svg n'est pas défini par défaut dans Qt, nécessite qtsvg

  6. QApplication == QtWidgets/QApplication 

  7. "QtWidgets/QGraphicsProxyWidget" doit être déclaré explicitement

  8. inclure "QtWidgets/QGraphicsDropShadowEffect" à déclarer explicitement

  9. inclure "QDebug" à déclarer explicitement

  10. Si le projet inclut des fichiers .ui et a besoin de ui_mainwindow.h, vous devez également ajouter QT + = widgets pour que uic soit appelé afin de générer des fichiers ui.

  11. Ajouter QtWidgets, #include "QtWidgets/QGraphicsEffect"

  12. Ajouter QtWidgets, #include "QtWidgets/QGraphicsView"

  13. inclure "QtWidgets/QPushButton"

  14. QGraphicsItem :: Scale devient QGraphicsItem :: setScale

  15. QApplication :: sendEvent devient QCoreApplication :: sendEvent

  16. QGraphicsDropShadowEffect devient QtWidgets/QGraphicsDropShadowEffect

3
prabindh

Je lis aussi le même livre. Le problème vient des versions de Qt. Le livre est écrit pour Qt4, pendant que vous essayez d'utiliser Qt5. Le moyen le plus simple de le résoudre consiste à modifier les lignes suivantes.

Dans FindDialog.pro add:

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

et dans FindDialog.cpp change #include <QtGui> pour la ligne:

#include <QtWidgets>

Vous n'avez pas besoin d'inclure plus et il devrait compiler sans erreurs maintenant. Pour plus d'informations à ce sujet, consultez la documentation de Qt sur la transition de Qt4 à Qt5 .

1
Mario García

Pour moi, avec qmake -v rapportant QMake version 3.0 et Qt version 5.3.0, cela fonctionnait comme suit:

1) Ajoutez ces deux lignes à find.pro:

QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

2) Conservez les déclarations anticipées dans find.h et utilisez l'inclus ci-dessous dans find.cpp:

#include <QCheckBox>
#include <QLineEdit>
#include <QLabel>
#include <QPushButtons>

#include <QHBoxLayout>
#include <QVBoxLayout>
0
rturrado