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. Cannot open include file " but I can create objects "
Qt 6.11 is out! See what's new in the release blog

Cannot open include file " but I can create objects "

Scheduled Pinned Locked Moved General and Desktop
17 Posts 3 Posters 3.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.
  • SGaistS Offline
    SGaistS Offline
    SGaist
    Lifetime Qt Champion
    wrote on last edited by
    #4

    Hi,

    You are missing the base class initialization:

    @
    calculator::calculator(QWidget parent):
    QWidget(parent)
    {
    //rest of the code
    }
    @

    Also you currently have a memory leak in your application since you don't delete dialog once the application is finished.

    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
    0
    • P Offline
      P Offline
      Project try
      wrote on last edited by
      #5

      Hey , thanks for your help in advance.

      I have a problem now when I compile it doesn't show the window then says

      The program has unexpectedly finished.

      --

      and I didn't understand your code what is :

      @
      QWidget{
      //code ?
      }

      you mean I should put what I put in the constructor inside that QWIDGET braces ?

      about the leak , lets just hope it runs first :D

      1 Reply Last reply
      0
      • P Offline
        P Offline
        Project try
        wrote on last edited by
        #6

        oh yeah forgot about qwidget in the implement :D

        1 Reply Last reply
        0
        • P Offline
          P Offline
          Project try
          wrote on last edited by
          #7

          Still same problem :(

          1 Reply Last reply
          0
          • P Offline
            P Offline
            Project try
            wrote on last edited by
            #8

            the code " I changed the project to solve the first problem so I named the class to another name "

            imp:
            @
            #include "checkclass.h"

            checkclass::checkclass(QWidget parent):
            QWidget(){
            edit = new QLineEdit;
            edit1=new QLineEdit;

                 left= new QHBoxLayout;
                 Main=new QHBoxLayout;
                    edit->setMaximumWidth(20);
                    edit->setFixedWidth(20);
                    edit->setValidator(Valid);
                    edit1->setMaximumWidth(20);
                    edit1->setFixedWidth(20);
                    edit1->setValidator(Valid);
                    QObject:: connect(edit,SIGNAL(editingFinished()),this,SLOT(convert));
                           QObject:: connect(edit1,SIGNAL(editingFinished()),this,SLOT(convert1));
                    left->addWidget(edit);
                    left->addWidget(edit1);
                    Main->addLayout(left);
            
                    setLayout(Main);
            

            }

            void checkclass::convert(){
            QString numbertext=edit->text();
            int x=numbertext.toInt();

            }
            void checkclass::convert1(){
            QString numbertext=edit1->text();
            int y=numbertext.toInt();

            }

            @

            header:
            @
            #ifndef CHECKCLASS_H
            #define CHECKCLASS_H
            #include<QLabel>
            #include<QHBoxLayout>
            #include<QLineEdit>
            #include<QIntValidator>
            #include<QWidget>
            class checkclass : public QWidget
            {
            public:
            checkclass(QWidget parent);

            void convert();
                void convert1();
            private:
                QLineEdit *edit;
                QLineEdit *edit1;
            QIntValidator *Valid;
                QHBoxLayout *left;
            
                QHBoxLayout *Main;
            

            };

            #endif // CHECKCLASS_H
            @

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

              Valid is not initialized

              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
              0
              • P Offline
                P Offline
                Project try
                wrote on last edited by
                #10

                same problem :(

                1 Reply Last reply
                0
                • P Offline
                  P Offline
                  Project try
                  wrote on last edited by
                  #11

                  @SGaist , Hopefully you didn't go to sleep yet , I really need your help , because tomorrow I want to focus on studying for my mid term exam :D

                  1 Reply Last reply
                  0
                  • P Offline
                    P Offline
                    Project try
                    wrote on last edited by
                    #12

                    The problem happens only when I use show function like for example:

                    myclass *A;
                    A->show();

                    it crashes , when I don't use show() my program runs just fine in the backround.

                    1 Reply Last reply
                    0
                    • JKSHJ Offline
                      JKSHJ Offline
                      JKSH
                      Moderators
                      wrote on last edited by
                      #13

                      [quote author="Project try" date="1394235877"]The problem happens only when I use show function like for example:

                      myclass *A;
                      A->show();

                      it crashes , when I don't use show() my program runs just fine in the backround.[/quote]Like SGaist said, you didn't initialize your pointer. A does not point to a valid object yet, so it will cause problems if you try to use it. It's not just show(), ANY function can cause a crash -- it might not crash immediately, but will crash some time later.

                      Call new to initialize your pointer. This is a very fundamental (and very important) concept of C++ by the way; if you are not familiar with it, I highly recommend reading a tutorial about pointers.

                      Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                      1 Reply Last reply
                      0
                      • P Offline
                        P Offline
                        Project try
                        wrote on last edited by
                        #14

                        Thanks man ^^ , it worked

                        but can you please tell me why did we put : QWidget *parent in the arguments of the constructor ? even though we didn't send any argument but it was a default so what's the point ?

                        and since it didn't take any argument there's no need to delete that parent pointer ? , because I saw a lot of examples and most of them use it like I said but they didn't really explain why.

                        1 Reply Last reply
                        0
                        • P Offline
                          P Offline
                          Project try
                          wrote on last edited by
                          #15

                          Good night , I need to sleep , thanks guys for helping me , and sorry for asking lots of question :(

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

                            So that you can reuse your widget the same way you would a standard Qt widget.

                            Even if you don't reuse it somewhere else, it's good practice to keep a consistent code base. You never know who will use your code later

                            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
                            0
                            • JKSHJ Offline
                              JKSHJ Offline
                              JKSH
                              Moderators
                              wrote on last edited by
                              #17

                              Parent-child relationships have multiple uses in Qt. One of them is memory management. When a parent object is deleted, it will automatically delete its child objects. Thus, you should never store or delete the parent pointer.

                              [quote]
                              @
                              checkclass::checkclass(QWidget parent):
                              QWidget(){
                              ...
                              @
                              [/quote]You have not made use of the parent pointer. Pass it to the QWidget constructor, and let Qt take care of things in the background:

                              @
                              checkclass::checkclass(QWidget parent):
                              QWidget(parent){
                              ...
                              @

                              Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                              1 Reply Last reply
                              0

                              • Login

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