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. Some errors in a simple program
Forum Updated to NodeBB v4.3 + New Features

Some errors in a simple program

Scheduled Pinned Locked Moved Solved General and Desktop
12 Posts 2 Posters 3.1k Views 2 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
    #1

    Hi all,
    Please consider these .h, .cpp and main,cpp files:

    test.h:

    #ifndef TEST1_H
    #define TEST1_H
    #include <QMainWindow>
    
    class QlineEdit;
    class QPushButton;
    
    class test1 : public QMainWindow
    {
        Q_OBJECT
    
    public:
        test1(QWidget* parent = 0);
    
    private slots:
        void show_clicked();
    
    private:
        int i;
        QlineEdit* line;
        QPushButton* show;
        QPushButton* quit;
    
    };
    
    #endif // TEST1_H
    
    

    test1.cpp:

    #include <QtWidgets>
    #include "test1.h"
    
    test1::test1(QWidget* parent)
       : QMainWindow(parent)
    {
     i = 5;
     show = new QPushButton(tr("show"));
     quit = new QPushButton(tr("Quit"));
     line = new QlineEdit;
    
     connect(show, SIGNAL(clicked(bool)), line, SLOT(show_clicked()));
     connect(quit, SIGNAL(clicked(bool)), this, SLOT(close));
    
     QVBoxLayout* layout = new QVBoxLayout;
     layout->addWidget(line);
     layout->addWidget(show);
     layout->addWidget(quit);
     setLayout(layout);
    }
    
    //**********************************
    
    void test1::show_clicked()
    {
     line->text(i);
    }
    
    

    and main.cpp:

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

    I get these errors in the test1.cpp:

    test1.cpp:10: error: invalid use of incomplete type 'class QlineEdit'
    line = new QlineEdit;

    test1.cpp:12: error: no matching function for call to 'test1::connect(QPushButton&, const char*, QlineEdit*&, const char*)'
    connect(show, SIGNAL(clicked(bool)), line, SLOT(show_clicked()));*
    ^
    error: no type named 'Object' in 'struct QtPrivate::FunctionPointer<const char>'*

    no matching function for call to 'QVBoxLayout::addWidget(QlineEdit&)'
    layout->addWidget(line);*

    test1.cpp:26: error: invalid use of incomplete type 'class QlineEdit'
    line->text(i);

    5 errors!

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

      @tomy said in Some errors in a simple program:

      invalid use of incomplete type 'class QLineEdit'

      Means that you need its include file.
      It simply only knows its name, not full info.

      so you need
      #include <QlineEdit>

      Also, you have a misstype
      line = new QlineEdit;

      its with big L

      line = new QLineEdit;

      so its most likely that.

      tomyT 1 Reply Last reply
      2
      • mrjjM mrjj

        @tomy said in Some errors in a simple program:

        invalid use of incomplete type 'class QLineEdit'

        Means that you need its include file.
        It simply only knows its name, not full info.

        so you need
        #include <QlineEdit>

        Also, you have a misstype
        line = new QlineEdit;

        its with big L

        line = new QLineEdit;

        so its most likely that.

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

        @mrjj
        Because of forward declarations, it didn't need more includes. I corrected the typos and now it's fine except a way to show an int number in a lineEdit.
        I tried to find a function (of lineEdit) for that but couldn't find one!

        Thank you for your help.

        mrjjM 1 Reply Last reply
        0
        • tomyT tomy

          @mrjj
          Because of forward declarations, it didn't need more includes. I corrected the typos and now it's fine except a way to show an int number in a lineEdit.
          I tried to find a function (of lineEdit) for that but couldn't find one!

          Thank you for your help.

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by mrjj
          #4

          @tomy
          To show an int as a string, you can use QString::number

          Like
          lineEdit->setText( QString::number( int_variable ) );

          Note that it wont do right if other locales etc. But I will assume
          for your tests, we can ignore that for now.

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

            Thank you.

            I din't understand your comment on not using int values for a lineEdit. Would you please explain it?
            If a lineEdit is only good to show texts, what is the standard way to show values on programs, please?

            I changed the QPushbutton show to show_number. Then ran the program. It ran successfully but shows only a window!!
            Isn't the setLayout() function sufficient to map the widgets on the window when running/showing?

            mrjjM 1 Reply Last reply
            0
            • tomyT tomy

              Thank you.

              I din't understand your comment on not using int values for a lineEdit. Would you please explain it?
              If a lineEdit is only good to show texts, what is the standard way to show values on programs, please?

              I changed the QPushbutton show to show_number. Then ran the program. It ran successfully but shows only a window!!
              Isn't the setLayout() function sufficient to map the widgets on the window when running/showing?

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @tomy

              • I din't understand your comment on not using int values for a lineEdit. Would you please explain it?

              Its not about the lineEdit. using ::number converts directly to
              a text version of the number.
              like 7 -> "7".
              However, for some other uses, you must take into account how its written. Some countries uses . for thousand like 1.000, other might use , (comma) and so on. 1,000
              Qt has other function to convert such numbers besides ::number.
              That was what i meant.

              tomyT 1 Reply Last reply
              1
              • mrjjM mrjj

                @tomy

                • I din't understand your comment on not using int values for a lineEdit. Would you please explain it?

                Its not about the lineEdit. using ::number converts directly to
                a text version of the number.
                like 7 -> "7".
                However, for some other uses, you must take into account how its written. Some countries uses . for thousand like 1.000, other might use , (comma) and so on. 1,000
                Qt has other function to convert such numbers besides ::number.
                That was what i meant.

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

                @mrjj

                Qt has other function to convert such numbers besides ::number.
                That was what i meant.

                OK. What function would you use to show a number in a widget, e.g. a lineEdit, please?

                And did you read my last question in the prior post please?

                mrjjM 1 Reply Last reply
                0
                • tomyT tomy

                  @mrjj

                  Qt has other function to convert such numbers besides ::number.
                  That was what i meant.

                  OK. What function would you use to show a number in a widget, e.g. a lineEdit, please?

                  And did you read my last question in the prior post please?

                  mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @tomy
                  This class
                  http://doc.qt.io/qt-5/qlocale.html

                  setLayout(layout); should do it but else try to give widget as parent in ctor
                  QVBoxLayout* layout = new QVBoxLayout(this); << give parent
                  and dont do setLayout

                  tomyT 1 Reply Last reply
                  0
                  • mrjjM mrjj

                    @tomy
                    This class
                    http://doc.qt.io/qt-5/qlocale.html

                    setLayout(layout); should do it but else try to give widget as parent in ctor
                    QVBoxLayout* layout = new QVBoxLayout(this); << give parent
                    and dont do setLayout

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

                    @mrjj

                    This class
                    http://doc.qt.io/qt-5/qlocale.html

                    For showing a number?!

                    setLayout(layout); should do it but else try to give widget as parent in ctor
                    QVBoxLayout* layout = new QVBoxLayout(this); << give parent
                    and dont do setLayout

                    I declared it like this: QVBoxLayout* layout = new QVBoxLayout(this); and didn't use the setLayout(layout);.
                    But no changes.
                    I think the problem is very simple. Please take a look at the codes once gain:

                    #ifndef TEST1_H
                    #define TEST1_H
                    #include <QMainWindow>
                    
                    class QLineEdit;
                    class QPushButton;
                    
                    class test1 : public QMainWindow
                    {
                        Q_OBJECT
                    
                    public:
                        test1(QWidget* parent = 0);
                    
                    private slots:
                        void show_clicked();
                    
                    private:
                        int i;
                        QLineEdit* line;
                        QPushButton* show_number;
                        QPushButton* quit;
                    
                    };
                    
                    #endif // TEST1_H
                    
                    #include <QtWidgets>
                    #include "test1.h"
                    
                    test1::test1(QWidget* parent)
                        : QMainWindow(parent)
                    {
                      i = 5;
                      show_number = new QPushButton(tr("show"));
                      quit = new QPushButton(tr("Quit"));
                      line = new QLineEdit;
                    
                      connect(show_number, SIGNAL(clicked(bool)),
                              line, SLOT(show_clicked()));
                      connect(quit, SIGNAL(clicked(bool)), this, SLOT(close));
                    
                      QVBoxLayout* layout = new QVBoxLayout(this);
                      layout->addWidget(line);
                      layout->addWidget(show_number);
                      layout->addWidget(quit);
                      setLayout(layout);
                    }
                    
                    //**********************************
                    
                    void test1::show_clicked()
                    {
                      line->setText(QString::number(i));
                    }
                    
                    #include<QApplication>
                    #include "test1.h"
                    
                    int main(int argc, char* argv[])
                    {
                        QApplication app(argc, argv);
                        test1* t1 = new test1;
                        t1->show();
                        return app.exec();
                    }
                    
                    
                    1 Reply Last reply
                    0
                    • mrjjM Offline
                      mrjjM Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on last edited by mrjj
                      #10

                      @tomy said in Some errors in a simple program:

                      For showing a number?!

                      For correctly formatting numbers as string while considering country norms.

                      • think the problem is very simple. Please take a look at the codes once gain:

                      Ah its because its a MainWindow
                      it has special center widget called
                      http://doc.qt.io/qt-5/qmainwindow.html#centralWidget
                      which you should set the layout on.
                      and not the mainwindow itself!!

                      QVBoxLayout* layout = new QVBoxLayout(this->centralWidget());
                      layout->addWidget(line);
                      layout->addWidget(show_number);
                      layout->addWidget(quit);
                      setLayout(layout); if used MUST be centralWidget()->setLayout(xx)

                      tomyT 1 Reply Last reply
                      0
                      • mrjjM mrjj

                        @tomy said in Some errors in a simple program:

                        For showing a number?!

                        For correctly formatting numbers as string while considering country norms.

                        • think the problem is very simple. Please take a look at the codes once gain:

                        Ah its because its a MainWindow
                        it has special center widget called
                        http://doc.qt.io/qt-5/qmainwindow.html#centralWidget
                        which you should set the layout on.
                        and not the mainwindow itself!!

                        QVBoxLayout* layout = new QVBoxLayout(this->centralWidget());
                        layout->addWidget(line);
                        layout->addWidget(show_number);
                        layout->addWidget(quit);
                        setLayout(layout); if used MUST be centralWidget()->setLayout(xx)

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

                        @mrjj

                        QVBoxLayout* layout = new QVBoxLayout(this->centralWidget());
                        layout->addWidget(line);
                        layout->addWidget(show_number);
                        layout->addWidget(quit);
                        setLayout(layout); if used MUST be centralWidget()->setLayout(xx)

                        Hi,

                        I used both cases but neither worked for QMainWindow. I had to change it to QDialog. then it worked and showed the widgets.

                        Incidentally, one of my connects in the code was:

                        connect(show_number, SIGNAL(clicked(bool)), line, SLOT(show_clicked()));
                        

                        When I clicked on the show button, nothing was shown in the lineEdit. But when I changed the receiver from line to this, it showed 5. Why please?

                        mrjjM 1 Reply Last reply
                        0
                        • tomyT tomy

                          @mrjj

                          QVBoxLayout* layout = new QVBoxLayout(this->centralWidget());
                          layout->addWidget(line);
                          layout->addWidget(show_number);
                          layout->addWidget(quit);
                          setLayout(layout); if used MUST be centralWidget()->setLayout(xx)

                          Hi,

                          I used both cases but neither worked for QMainWindow. I had to change it to QDialog. then it worked and showed the widgets.

                          Incidentally, one of my connects in the code was:

                          connect(show_number, SIGNAL(clicked(bool)), line, SLOT(show_clicked()));
                          

                          When I clicked on the show button, nothing was shown in the lineEdit. But when I changed the receiver from line to this, it showed 5. Why please?

                          mrjjM Offline
                          mrjjM Offline
                          mrjj
                          Lifetime Qt Champion
                          wrote on last edited by
                          #12

                          @tomy said in Some errors in a simple program:

                          But when I changed the receiver from line to this, it showed 5. Why please?

                          Well, maybe before the connect was not right.

                          so if line didnt have show_clicked()

                          then connect returns false and no connection is made.

                          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