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. Qtableview editable cells
Forum Updated to NodeBB v4.3 + New Features

Qtableview editable cells

Scheduled Pinned Locked Moved Solved General and Desktop
21 Posts 4 Posters 4.1k 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.
  • JonBJ JonB

    @Ahsan-Niaz
    I don't know about your dialog (I see @jsulm has just replied).

    But I don't get how your QTableView *view = new QTableView; shows works in practice. You have not set its parent, and you have not added it onto a layout So it's completely "standalone", not even inside a widget/window or with a layout? Plus it will "leak" as you don't retain any handle to it, and each time the button is pressed you'll get a new one.

    jsulmJ Offline
    jsulmJ Offline
    jsulm
    Lifetime Qt Champion
    wrote on last edited by
    #6

    @JonB said in Qtableview editable cells:

    So it's completely "standalone", not even inside a widget/window or with a layout?

    Yes, it's a window as it does not have parent.

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

    JonBJ 1 Reply Last reply
    0
    • jsulmJ jsulm

      @JonB said in Qtableview editable cells:

      So it's completely "standalone", not even inside a widget/window or with a layout?

      Yes, it's a window as it does not have parent.

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #7

      @jsulm
      Indeed. And as I said: no layout. Plus, how does he access/delete it when it doesn't want it any longer, given that view is a local variable? And how does he prevent creating multiple ones?

      I have changed show to "works in practice".

      1 Reply Last reply
      0
      • A Offline
        A Offline
        Ahsan Niaz
        wrote on last edited by
        #8

        @JonB I have a dialog names (case_adjustment.ui). Initially, i added a tableview in it, and a push button to load the table. I loaded the table succesfully, but couldn't make the tableview editable.
        Then someone here on QTFOrum suggested me the following code

        QSqlTableModel *model = new QSqlTableModel;
        
            model->setTable("adjusted_cases");
        
            model->setEditStrategy(QSqlTableModel::OnRowChange);
            model->select();
        
        
            QTableView *view = new QTableView;
            view->setModel(model);
            view->resize(1550,650);
            view->show();
        

        Now the problem is, i am able to edit things, and edits are saved as well, but i am unable to show the hidden dialog as i need it badly.

        JonBJ 1 Reply Last reply
        0
        • A Ahsan Niaz

          @JonB I have a dialog names (case_adjustment.ui). Initially, i added a tableview in it, and a push button to load the table. I loaded the table succesfully, but couldn't make the tableview editable.
          Then someone here on QTFOrum suggested me the following code

          QSqlTableModel *model = new QSqlTableModel;
          
              model->setTable("adjusted_cases");
          
              model->setEditStrategy(QSqlTableModel::OnRowChange);
              model->select();
          
          
              QTableView *view = new QTableView;
              view->setModel(model);
              view->resize(1550,650);
              view->show();
          

          Now the problem is, i am able to edit things, and edits are saved as well, but i am unable to show the hidden dialog as i need it badly.

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #9

          @Ahsan-Niaz
          That was not the part I was talking about. Read @jsulm's earlier post about signals/slots for that part.

          My comments were to do with what you do from then on with the local view, not the editing behaviour. None of the code you show is to do with your separate dialog issue.

          1 Reply Last reply
          0
          • A Offline
            A Offline
            Ahsan Niaz
            wrote on last edited by
            #10

            @jsulm @JonB I am a newbie in qt creator. I have zero knowledge as compared to you guys.
            I am literally unaware of deriving a custom class from QTableViewm, signal and slots. Can you help me a bit to understand and implement the concepts correctly.
            @JonB I also want to ask how i can add TableView to a layout? and make it editable as well. Thanks for your replies i really appreciate.

            jsulmJ 1 Reply Last reply
            0
            • A Ahsan Niaz

              @jsulm @JonB I am a newbie in qt creator. I have zero knowledge as compared to you guys.
              I am literally unaware of deriving a custom class from QTableViewm, signal and slots. Can you help me a bit to understand and implement the concepts correctly.
              @JonB I also want to ask how i can add TableView to a layout? and make it editable as well. Thanks for your replies i really appreciate.

              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on last edited by
              #11

              @Ahsan-Niaz Please read https://doc.qt.io/qt-5/signalsandslots.html - this is one of the core concepts in Qt you need to know.
              Regarding deriving a class from another one: this is pure C++, not Qt specific. Same for overriding a method from base class. If unsure please read about OOP and inheritance.

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

              1 Reply Last reply
              0
              • A Offline
                A Offline
                Ahsan Niaz
                wrote on last edited by
                #12

                @jsulm I have OOP concepts, i have gone through the link you sent in your last reply. But, i am still finding it hard to derive a custom class from QTableview. As the Qtableview is available only in this piece of code

                QTableView *view = new QTableView;
                    view->setModel(model);
                    view->resize(1550,650);
                    view->show();
                

                will this (view) object help me to derive a custom class? How?

                jsulmJ 1 Reply Last reply
                0
                • A Ahsan Niaz

                  @jsulm I have OOP concepts, i have gone through the link you sent in your last reply. But, i am still finding it hard to derive a custom class from QTableview. As the Qtableview is available only in this piece of code

                  QTableView *view = new QTableView;
                      view->setModel(model);
                      view->resize(1550,650);
                      view->show();
                  

                  will this (view) object help me to derive a custom class? How?

                  jsulmJ Offline
                  jsulmJ Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on last edited by jsulm
                  #13

                  @Ahsan-Niaz said in Qtableview editable cells:

                  will this (view) object help me to derive a custom class? How?

                  class MyView : public QTableView
                  {
                  ...
                  signals:
                      void closing();
                  
                  protected:
                      void closeEvent(QCloseEvent *event)override;
                  }
                  
                  // In cpp
                  void MyView::closeEvent(QCloseEvent *event)
                  {
                      emit closing();
                      QTableView::closeEvent(event); 
                  }
                  ...
                  MyView *view = new MyView;
                  connect(view, &MyView::closing, this, &QDialog::show);
                  view->setModel(model);
                  view->resize(1550,650);
                  view->show();
                  

                  Don't forget that currently you have a memory leak: you're not deleting view.

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

                  1 Reply Last reply
                  1
                  • A Offline
                    A Offline
                    Ahsan Niaz
                    wrote on last edited by
                    #14

                    Hi, Thanks for this piece of code

                    class MyView : public QTableView
                    {
                    ...
                    signals:
                        void closing();
                    
                    protected:
                        void closeEvent(QCloseEvent *event)override;
                    }
                    

                    I have added the above code in (.h) file, which looks like
                    ca7b94cb-8f91-4978-aeb2-85ff7bced622-image.png
                    Then i added the remaining codes in cpp file which looks like
                    706864bc-c57a-41a2-becd-10bf81b2e471-image.png

                    When i run the code, i am getting an erro saying (Static Assertion Failed, No Q_Object in the class with the signal)
                    Here is the error
                    9ce62df5-5aba-47a1-ba93-91123371ef19-image.png

                    jsulmJ JonBJ 2 Replies Last reply
                    0
                    • A Ahsan Niaz

                      Hi, Thanks for this piece of code

                      class MyView : public QTableView
                      {
                      ...
                      signals:
                          void closing();
                      
                      protected:
                          void closeEvent(QCloseEvent *event)override;
                      }
                      

                      I have added the above code in (.h) file, which looks like
                      ca7b94cb-8f91-4978-aeb2-85ff7bced622-image.png
                      Then i added the remaining codes in cpp file which looks like
                      706864bc-c57a-41a2-becd-10bf81b2e471-image.png

                      When i run the code, i am getting an erro saying (Static Assertion Failed, No Q_Object in the class with the signal)
                      Here is the error
                      9ce62df5-5aba-47a1-ba93-91123371ef19-image.png

                      jsulmJ Offline
                      jsulmJ Offline
                      jsulm
                      Lifetime Qt Champion
                      wrote on last edited by
                      #15

                      @Ahsan-Niaz Please put each class in its own header/cpp.
                      Also, if deriving from QObject based classes you need to add Q_OBJECT macro to your class (https://doc.qt.io/qt-5/qobject.html#Q_OBJECT):

                      class MyView : public QTableView
                      {
                      Q_OBJECT
                      ...
                      

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

                      1 Reply Last reply
                      2
                      • A Ahsan Niaz

                        Hi, Thanks for this piece of code

                        class MyView : public QTableView
                        {
                        ...
                        signals:
                            void closing();
                        
                        protected:
                            void closeEvent(QCloseEvent *event)override;
                        }
                        

                        I have added the above code in (.h) file, which looks like
                        ca7b94cb-8f91-4978-aeb2-85ff7bced622-image.png
                        Then i added the remaining codes in cpp file which looks like
                        706864bc-c57a-41a2-becd-10bf81b2e471-image.png

                        When i run the code, i am getting an erro saying (Static Assertion Failed, No Q_Object in the class with the signal)
                        Here is the error
                        9ce62df5-5aba-47a1-ba93-91123371ef19-image.png

                        JonBJ Offline
                        JonBJ Offline
                        JonB
                        wrote on last edited by JonB
                        #16

                        @Ahsan-Niaz
                        Additional to @jsulm. Separate problem. You have a member variable QSqlDatabase mydb. Don't do this. I know there is a lot to learn, but as per https://doc.qt.io/qt-5/qsqldatabase.html#details

                        Warning: It is highly recommended that you do not keep a copy of the QSqlDatabase around as a member of a class, as this will prevent the instance from being correctly cleaned up on shutdown. If you need to access an existing QSqlDatabase, it should be accessed with database(). If you chose to have a QSqlDatabase member variable, this needs to be deleted before the QCoreApplication instance is deleted, otherwise it may lead to undefined behavior.

                        You will end up falling foul of this, so follow the advice/example there and get it done now.

                        P.S.
                        When you have followed @jsulm's instruction to add Q_OBJECT macro in header file, you may find compilation/linking goes confusingly wrong. If so, delete all files in the build directory and recompile from scratch. This can be required any time you start with a header file class which does not have Q_OBJECT, you compile, and then you decide to go back and add in Q_OBJECT.

                        1 Reply Last reply
                        2
                        • A Offline
                          A Offline
                          Ahsan Niaz
                          wrote on last edited by Ahsan Niaz
                          #17

                          @jsulm I have added a separate (.h) file named myview.h here is how it looks
                          bef11111-06d0-491f-8762-6577fb8fe344-image.png
                          I included (myview.h) in the cpp file which looks like this
                          e5b99876-70d7-4116-b064-fe1116644555-image.png
                          I ran the code, it ran without error, but when i closed the tableview after editing, it didn't show the dialog. any expected reason?
                          Moreover, canyou explain this

                          connect(view, &MyView::closing, this, &QDialog::show);
                          
                          jsulmJ 1 Reply Last reply
                          0
                          • A Ahsan Niaz

                            @jsulm I have added a separate (.h) file named myview.h here is how it looks
                            bef11111-06d0-491f-8762-6577fb8fe344-image.png
                            I included (myview.h) in the cpp file which looks like this
                            e5b99876-70d7-4116-b064-fe1116644555-image.png
                            I ran the code, it ran without error, but when i closed the tableview after editing, it didn't show the dialog. any expected reason?
                            Moreover, canyou explain this

                            connect(view, &MyView::closing, this, &QDialog::show);
                            
                            jsulmJ Offline
                            jsulmJ Offline
                            jsulm
                            Lifetime Qt Champion
                            wrote on last edited by
                            #18

                            @Ahsan-Niaz Why is MyView::closeEvent in case_adjustment.cpp?!
                            As I said: put each class in its own header AND cpp file.
                            If you're done that and it still does not work, then please use debugger to find out whether MyView::closeEvent was called.

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

                            1 Reply Last reply
                            0
                            • A Offline
                              A Offline
                              Ahsan Niaz
                              wrote on last edited by
                              #19

                              @jsulm I really appreciate for your precious time and effort.
                              I have created a cpp file for myview, It looks like this
                              280a2e81-0f3b-4c03-b0fc-b487e59e43f8-image.png
                              There is already a (myview.h) file.
                              I have run the code, it ran well, without error, but it didn't do the desired thing.
                              Have a look at case_adjustment.cpp as well
                              3d81f286-6d42-4997-be6b-859b10cab6cf-image.png
                              I debugged it as well,
                              But the debugger is not doing anything.
                              I have a request. If you can connect to my pc for a while? i have to submit it tonight, i would really appreciate it. Thanks

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

                                Hi,

                                @Ahsan-Niaz said in Qtableview editable cells:

                                I have run the code, it ran well, without error, but it didn't do the desired thing.

                                What should happen ?
                                What does happen ?

                                @Ahsan-Niaz said in Qtableview editable cells:

                                I have a request. If you can connect to my pc for a while? i have to submit it tonight, i would really appreciate it. Thanks

                                Sounds like a school assignment, doesn't it ?

                                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
                                1
                                • A Ahsan Niaz

                                  @jsulm I really appreciate for your precious time and effort.
                                  I have created a cpp file for myview, It looks like this
                                  280a2e81-0f3b-4c03-b0fc-b487e59e43f8-image.png
                                  There is already a (myview.h) file.
                                  I have run the code, it ran well, without error, but it didn't do the desired thing.
                                  Have a look at case_adjustment.cpp as well
                                  3d81f286-6d42-4997-be6b-859b10cab6cf-image.png
                                  I debugged it as well,
                                  But the debugger is not doing anything.
                                  I have a request. If you can connect to my pc for a while? i have to submit it tonight, i would really appreciate it. Thanks

                                  jsulmJ Offline
                                  jsulmJ Offline
                                  jsulm
                                  Lifetime Qt Champion
                                  wrote on last edited by
                                  #21

                                  @Ahsan-Niaz said in Qtableview editable cells:

                                  But the debugger is not doing anything

                                  Set break point inside MyView::closeEvent and run through debugger: does it stop inside MyView::closeEvent?
                                  Please post your code as text and not images.

                                  And no, I'm not going to connect to your PC and do your work...

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

                                  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