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. Strange errors with my code
Qt 6.11 is out! See what's new in the release blog

Strange errors with my code

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 5 Posters 4.3k Views 5 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,

    Please look at my code.
    This is Hello.h:

    #ifndef HELLO_H
    #define HELLO_H
    
    #include <QDialog>
    
    class QCheckBox;
    class QLabel;
    class QLineEdit;
    class QPushButton;
    
    class Hello : public QDialog
    {
        Q_OBJECT
    public:
        Hello(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 // HELLO_H
    

    And this one, Hello.cpp

    #include <QtWidgets>
    #include "hello.h"
    
    Hello::Hello(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 Hello::findClicked()
    {
        QString text = lineEdit -> text();
        Qt::CaseSensitivity cs = caseCheckBox -> isChecked() ? Qt::CaseSensitive
                                                             : Qt::CaseInsensitive;
        if(backwardCheckBox -> isChecked()) {
            emit findPrevious(text, cs);
        } else {
            emit findNext(text, cs);
        }
    }
    
    //-------------------------------------------------
    
    void Hello::enableFindButton(const QString& text)
    {
        findButton -> setEnabled(!text.isEmpty());
    }
    

    And this, main.cpp:

    #include <QApplication>
    #include <hello.h>
    
    int main(int argc, char* argv[])
    {
        QApplication app(argc, argv);
        Hello* dialog = new Hello;
        dialog -> show();
    
        return app.exec();
    }
    

    I get some strange errors that I don't know the reason for them, like:
    C:\Users...\Qt\Hello\hello.cpp:5: error: undefined reference to `vtable for Hello'.

    This is for line 5 of Hello.cpp ( : QDialog(parent))

    Would you please help me?

    1 Reply Last reply
    0
    • K Offline
      K Offline
      koahnig
      wrote on last edited by
      #2

      You try to assign a QWidget pointer to QDialog.

      Change to

      class Hello : public QDialog
      {
          Q_OBJECT
      public:
          Hello(QDialog* parent = 0);
      

      and

      Hello::Hello(QDialog *parent)
          : QDialog(parent)
      

      Vote the answer(s) that helped you to solve your issue(s)

      tomyT 1 Reply Last reply
      0
      • K koahnig

        You try to assign a QWidget pointer to QDialog.

        Change to

        class Hello : public QDialog
        {
            Q_OBJECT
        public:
            Hello(QDialog* parent = 0);
        

        and

        Hello::Hello(QDialog *parent)
            : QDialog(parent)
        
        tomyT Offline
        tomyT Offline
        tomy
        wrote on last edited by tomy
        #3

        @koahnig :
        Thanks for the answer. I understand your reason but do you have the book C++-GUI-Programming-with-Qt-4-2ndEdition on your hand to have a look at page 29 of it?
        There it mentions these:

        class Hello : public QDialog
        {
            Q_OBJECT
        public:
            Hello(QWidget* parent = 0);
        

        and

        Hello::Hello(QWidget *parent)
            : QDialog(parent)
        {
        

        I also modified my code as you suggested but get the errors yet! :(

        1 Reply Last reply
        0
        • mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Hi

          • "undefined reference to `vtable "

          From build menu, try
          Clean All
          qmake
          Build All

          tomyT 1 Reply Last reply
          4
          • mrjjM mrjj

            Hi

            • "undefined reference to `vtable "

            From build menu, try
            Clean All
            qmake
            Build All

            tomyT Offline
            tomyT Offline
            tomy
            wrote on last edited by
            #5

            @mrjj said in Strange errors with my code:

            Hi

            • "undefined reference to `vtable "

            From build menu, try
            Clean All
            qmake
            Build All

            Thank you. You solved the issues.

            Could I ask why I faced this kind of problem? What was the reason?

            kshegunovK 1 Reply Last reply
            0
            • K Offline
              K Offline
              koahnig
              wrote on last edited by
              #6

              Sometimes a fresh rebuild simply helps.

              Especially when you have a lot of different source and header not all dependencies are always covered and the system cannot detect.
              This is just a trial to explain, it is very often a mystery to me why you are ending up in such failures.

              The sequence given by @mrjj is basically some sort of Ctrl+Alt+Del for Qt creator

              Vote the answer(s) that helped you to solve your issue(s)

              1 Reply Last reply
              1
              • tomyT tomy

                @mrjj said in Strange errors with my code:

                Hi

                • "undefined reference to `vtable "

                From build menu, try
                Clean All
                qmake
                Build All

                Thank you. You solved the issues.

                Could I ask why I faced this kind of problem? What was the reason?

                kshegunovK Offline
                kshegunovK Offline
                kshegunov
                Moderators
                wrote on last edited by
                #7

                @tomy said in Strange errors with my code:

                What was the reason?

                Stale translation units (object files). Probably Creator missed that you changed something, just as @koahnig explained, and didn't recompile the .cpp, which in turn led to the linker error.

                Read and abide by the Qt Code of Conduct

                1 Reply Last reply
                2
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  Hi,

                  To add to my fellows, the vtable error usually comes when adding/removing the Q_OBJECT macro. This one requires to re-run qmake to refresh the build setup with the add/removed information.

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  2
                  • tomyT Offline
                    tomyT Offline
                    tomy
                    wrote on last edited by
                    #9

                    So whenever I will see such error (vtable) again, I will do the steps mrjj said. :)
                    Thanks so much to all, especially to SGaist for his simple language.

                    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