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 3.7k 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.
  • A Offline
    A Offline
    Ahsan Niaz
    wrote on last edited by Ahsan Niaz
    #1

    Hi, I have a weird situation.
    I have a small dialog
    106ba75e-83b1-4a43-8663-475d6e1d035e-image.png
    When i click this push button, my code shows the following QTableview with the help of QSqlTableModel
    16583d51-300c-4ed5-90bb-41d359796cea-image.png

    I wanted this table view to be editable. To make it editable, my code has

    void Case_Adjustment::on_pushButton_clicked()
    {
    
        setVisible(false);
        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();
    }
    

    But, the tableview is only editable when my previous dialog is either hidden or closed. THats fine, I hide() the (load case adjustment) dialog. But i am having difficulty in bringing it back after i am finished with editing.
    I hope i am very clear with my problem. Can anybody help me in this regard?

    jsulmJ 1 Reply Last reply
    0
    • A Ahsan Niaz

      Hi, I have a weird situation.
      I have a small dialog
      106ba75e-83b1-4a43-8663-475d6e1d035e-image.png
      When i click this push button, my code shows the following QTableview with the help of QSqlTableModel
      16583d51-300c-4ed5-90bb-41d359796cea-image.png

      I wanted this table view to be editable. To make it editable, my code has

      void Case_Adjustment::on_pushButton_clicked()
      {
      
          setVisible(false);
          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();
      }
      

      But, the tableview is only editable when my previous dialog is either hidden or closed. THats fine, I hide() the (load case adjustment) dialog. But i am having difficulty in bringing it back after i am finished with editing.
      I hope i am very clear with my problem. Can anybody help me in this regard?

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

      @Ahsan-Niaz said in Qtableview editable cells:

      Can anybody help me in this regard?

      It is not really clear what exactly you are doing. How do you show this dialog (is it modal?)? How do you show the table (own window?)? How do you try to show the dialog again.

      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 Ahsan Niaz
        #3
        void Case_Adjustment::on_pushButton_clicked()
        {
        
            setVisible(false);
            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();
         
        
        
        
        
        }
        

        @jsulm This code will help you understand everything. setVisible false hides the dialog which had push button. After editing the tableview, i want to show that hidden dialog again. Is it clear?

        jsulmJ JonBJ 2 Replies Last reply
        0
        • A Ahsan Niaz
          void Case_Adjustment::on_pushButton_clicked()
          {
          
              setVisible(false);
              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();
           
          
          
          
          
          }
          

          @jsulm This code will help you understand everything. setVisible false hides the dialog which had push button. After editing the tableview, i want to show that hidden dialog again. Is it clear?

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

          @Ahsan-Niaz Then you need to call show() on your dialog as soon as editing is done. I assume that "editing done" is when the user closes the table view (as you did not mention this). You could derive a custom class from QTableView and overwrite https://doc.qt.io/qt-5/qwidget.html#closeEvent where you can emit a signal. Connect that signal to show() slot of your dialog.

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

          1 Reply Last reply
          1
          • A Ahsan Niaz
            void Case_Adjustment::on_pushButton_clicked()
            {
            
                setVisible(false);
                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();
             
            
            
            
            
            }
            

            @jsulm This code will help you understand everything. setVisible false hides the dialog which had push button. After editing the tableview, i want to show that hidden dialog again. Is it clear?

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

            @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 1 Reply Last reply
            0
            • 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

                                          • Login

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