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. Deleting row from QTableWidget and from Sqlite database
Forum Updated to NodeBB v4.3 + New Features

Deleting row from QTableWidget and from Sqlite database

Scheduled Pinned Locked Moved Solved General and Desktop
71 Posts 6 Posters 16.3k 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.
  • jsulmJ jsulm

    @Risver said in Deleting row from QTableWidget and from Sqlite database:

    I read that in QTableView you cannot delete the rows

    Because that is the job of the model, not view.
    See https://doc.qt.io/qt-5/qsqltablemodel.html#removeRows

    R Offline
    R Offline
    Risver
    wrote on last edited by
    #5

    @jsulm
    Okey i swapped the QTableWidget to QTableView. Now it looks like:

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    
    #include <QMessageBox>
    
    MainWindow::MainWindow(QWidget *parent)
        : QMainWindow(parent)
        , ui(new Ui::MainWindow)
    {
        ui->setupUi(this); 
        setWindowFlags(Qt::MSWindowsFixedSizeDialogHint);
    
        m_logWindow = new login(this);
        m_regWindow = new signup(this);
    
        connect(m_logWindow,&login::LoginSuccess, this, &MainWindow::nextWindow);
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    
    void MainWindow::on_login_clicked()
    {
        m_logWindow->show();
    }
    
    void MainWindow::on_signup_clicked()
    {
        m_regWindow->show();
    }
    
    void MainWindow::nextWindow(QString base)
    {
        ui->stackedWidget->setCurrentIndex(1);
        readList(base);
        Bufferbase = base;
    }
    
    void MainWindow::readList(QString base)
    {
        m_db.setDatabaseName("DiffPass.sqlite");
    
        QSqlQuery qry(m_db);
        QString sBuffer = "SELECT * FROM "+base;
        qry.exec(sBuffer);
    
        querymodel = new QSqlQueryModel();
        querymodel -> setQuery(sBuffer);
        ui -> tableView -> setModel(querymodel);
    }
    
    void MainWindow::on_remove_clicked()
    {
        
    }
    
    

    And now how can i detect the selected row signal ?

    jsulmJ 1 Reply Last reply
    0
    • R Risver

      @jsulm
      Okey i swapped the QTableWidget to QTableView. Now it looks like:

      #include "mainwindow.h"
      #include "ui_mainwindow.h"
      
      #include <QMessageBox>
      
      MainWindow::MainWindow(QWidget *parent)
          : QMainWindow(parent)
          , ui(new Ui::MainWindow)
      {
          ui->setupUi(this); 
          setWindowFlags(Qt::MSWindowsFixedSizeDialogHint);
      
          m_logWindow = new login(this);
          m_regWindow = new signup(this);
      
          connect(m_logWindow,&login::LoginSuccess, this, &MainWindow::nextWindow);
      }
      
      MainWindow::~MainWindow()
      {
          delete ui;
      }
      
      void MainWindow::on_login_clicked()
      {
          m_logWindow->show();
      }
      
      void MainWindow::on_signup_clicked()
      {
          m_regWindow->show();
      }
      
      void MainWindow::nextWindow(QString base)
      {
          ui->stackedWidget->setCurrentIndex(1);
          readList(base);
          Bufferbase = base;
      }
      
      void MainWindow::readList(QString base)
      {
          m_db.setDatabaseName("DiffPass.sqlite");
      
          QSqlQuery qry(m_db);
          QString sBuffer = "SELECT * FROM "+base;
          qry.exec(sBuffer);
      
          querymodel = new QSqlQueryModel();
          querymodel -> setQuery(sBuffer);
          ui -> tableView -> setModel(querymodel);
      }
      
      void MainWindow::on_remove_clicked()
      {
          
      }
      
      

      And now how can i detect the selected row signal ?

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

      @Risver said in Deleting row from QTableWidget and from Sqlite database:

      And now how can i detect the selected row signal ?

      What about https://doc.qt.io/qt-5/qtableview.html#selectionChanged ?

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

      R 1 Reply Last reply
      1
      • jsulmJ jsulm

        @Risver said in Deleting row from QTableWidget and from Sqlite database:

        And now how can i detect the selected row signal ?

        What about https://doc.qt.io/qt-5/qtableview.html#selectionChanged ?

        R Offline
        R Offline
        Risver
        wrote on last edited by
        #7

        @jsulm
        Okay and how can i connect it with the button_clicked signal?

        Christian EhrlicherC 1 Reply Last reply
        0
        • R Risver

          @jsulm
          Okay and how can i connect it with the button_clicked signal?

          Christian EhrlicherC Online
          Christian EhrlicherC Online
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #8

          @Risver said in Deleting row from QTableWidget and from Sqlite database:

          Okay and how can i connect it with the button_clicked signal?

          You won't
          When you react on the button clicked signal, get the current selection from the view and delete the selected rows in the model.

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          R 1 Reply Last reply
          3
          • Christian EhrlicherC Christian Ehrlicher

            @Risver said in Deleting row from QTableWidget and from Sqlite database:

            Okay and how can i connect it with the button_clicked signal?

            You won't
            When you react on the button clicked signal, get the current selection from the view and delete the selected rows in the model.

            R Offline
            R Offline
            Risver
            wrote on last edited by
            #9

            @Christian-Ehrlicher said in Deleting row from QTableWidget and from Sqlite database:

            @Risver said in Deleting row from QTableWidget and from Sqlite database:

            Okay and how can i connect it with the button_clicked signal?

            You won't
            When you react on the button clicked signal, get the current selection from the view and delete the selected rows in the model.

            Something like this ?

            void MainWindow::on_remove_clicked()
            {
                int selectedRow = ui->tableView->selectionModel()->currentIndex().row();
                
            }
            
            Christian EhrlicherC 1 Reply Last reply
            0
            • R Risver

              @Christian-Ehrlicher said in Deleting row from QTableWidget and from Sqlite database:

              @Risver said in Deleting row from QTableWidget and from Sqlite database:

              Okay and how can i connect it with the button_clicked signal?

              You won't
              When you react on the button clicked signal, get the current selection from the view and delete the selected rows in the model.

              Something like this ?

              void MainWindow::on_remove_clicked()
              {
                  int selectedRow = ui->tableView->selectionModel()->currentIndex().row();
                  
              }
              
              Christian EhrlicherC Online
              Christian EhrlicherC Online
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by
              #10

              @Risver Yes.

              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
              Visit the Qt Academy at https://academy.qt.io/catalog

              R 1 Reply Last reply
              0
              • Christian EhrlicherC Christian Ehrlicher

                @Risver Yes.

                R Offline
                R Offline
                Risver
                wrote on last edited by
                #11

                @Christian-Ehrlicher
                Now when i'm trying to implement QSqlTableModel::deleteRowFromTable the debugger says the deleteRowFromTable is a protected member of QSqlTableModel

                jsulmJ 1 Reply Last reply
                0
                • R Risver

                  @Christian-Ehrlicher
                  Now when i'm trying to implement QSqlTableModel::deleteRowFromTable the debugger says the deleteRowFromTable is a protected member of QSqlTableModel

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

                  @Risver https://doc.qt.io/qt-5/qsqltablemodel.html#removeRows

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

                  R 1 Reply Last reply
                  3
                  • jsulmJ jsulm

                    @Risver https://doc.qt.io/qt-5/qsqltablemodel.html#removeRows

                    R Offline
                    R Offline
                    Risver
                    wrote on last edited by
                    #13

                    @jsulm
                    Could you give some example ?

                    jsulmJ 1 Reply Last reply
                    0
                    • R Risver

                      @jsulm
                      Could you give some example ?

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

                      @Risver Example?

                      model->removeRows(row, 1);
                      

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

                      R 1 Reply Last reply
                      1
                      • jsulmJ jsulm

                        @Risver Example?

                        model->removeRows(row, 1);
                        
                        R Offline
                        R Offline
                        Risver
                        wrote on last edited by
                        #15

                        @jsulm
                        Now i have error - reference to type 'const QModelIndex' could not bind to an rvalue of type 'int'.
                        I think i have to refer to QModelIndex but i don't know how. I have a QSqlQueryModel. Should i change it to QAbstractTableModel ?

                        jsulmJ 1 Reply Last reply
                        0
                        • R Risver

                          @jsulm
                          Now i have error - reference to type 'const QModelIndex' could not bind to an rvalue of type 'int'.
                          I think i have to refer to QModelIndex but i don't know how. I have a QSqlQueryModel. Should i change it to QAbstractTableModel ?

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

                          @Risver Please show how you're calling removeRows
                          And what Qt version do you use?

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

                          R 1 Reply Last reply
                          1
                          • jsulmJ jsulm

                            @Risver Please show how you're calling removeRows
                            And what Qt version do you use?

                            R Offline
                            R Offline
                            Risver
                            wrote on last edited by
                            #17

                            @jsulm

                            void MainWindow::on_remove_clicked()
                            {
                            
                                if (ui->tableView->selectionModel()->hasSelection())
                                {
                                    int addressId = ui->tableView->selectionModel()->currentIndex().row();
                                    querymodel->removeRow(addressId, 1);
                                    qDebug() << addressId;
                                }
                            }
                            
                            

                            The QT version is 5.0.2

                            jsulmJ M 2 Replies Last reply
                            0
                            • R Risver

                              @jsulm

                              void MainWindow::on_remove_clicked()
                              {
                              
                                  if (ui->tableView->selectionModel()->hasSelection())
                                  {
                                      int addressId = ui->tableView->selectionModel()->currentIndex().row();
                                      querymodel->removeRow(addressId, 1);
                                      qDebug() << addressId;
                                  }
                              }
                              
                              

                              The QT version is 5.0.2

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

                              @Risver said in Deleting row from QTableWidget and from Sqlite database:

                              QT version is 5.0.2

                              Check the documentation for removeRows() in that Qt version.
                              Probably it has a different signature.

                              Is there a reason why you're using such an ancient version?

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

                              R 1 Reply Last reply
                              1
                              • jsulmJ jsulm

                                @Risver said in Deleting row from QTableWidget and from Sqlite database:

                                QT version is 5.0.2

                                Check the documentation for removeRows() in that Qt version.
                                Probably it has a different signature.

                                Is there a reason why you're using such an ancient version?

                                R Offline
                                R Offline
                                Risver
                                wrote on last edited by
                                #19

                                @jsulm said in Deleting row from QTableWidget and from Sqlite database:

                                Is there a reason why you're using such an ancient version?

                                I just installed it a while time ago.

                                JonBJ 1 Reply Last reply
                                0
                                • R Risver

                                  @jsulm said in Deleting row from QTableWidget and from Sqlite database:

                                  Is there a reason why you're using such an ancient version?

                                  I just installed it a while time ago.

                                  JonBJ Online
                                  JonBJ Online
                                  JonB
                                  wrote on last edited by
                                  #20

                                  @Risver said in Deleting row from QTableWidget and from Sqlite database:

                                  I just installed it a while time ago.

                                  Before you spend any more time on this. Qt5 is already up to 5.15, unless you have some deep reason for wanting 5.0 I would uninstall and get such an up-to-date version.

                                  R 1 Reply Last reply
                                  1
                                  • JonBJ JonB

                                    @Risver said in Deleting row from QTableWidget and from Sqlite database:

                                    I just installed it a while time ago.

                                    Before you spend any more time on this. Qt5 is already up to 5.15, unless you have some deep reason for wanting 5.0 I would uninstall and get such an up-to-date version.

                                    R Offline
                                    R Offline
                                    Risver
                                    wrote on last edited by
                                    #21

                                    @JonB
                                    I can just upgrade Qt to v6 and see if it works

                                    JonBJ 1 Reply Last reply
                                    0
                                    • R Risver

                                      @JonB
                                      I can just upgrade Qt to v6 and see if it works

                                      JonBJ Online
                                      JonBJ Online
                                      JonB
                                      wrote on last edited by
                                      #22

                                      @Risver
                                      You can indeed, though my personal preference would be to take the latest Qt5.x release. I still see too many issues with Qt6, not to mention that I think they still have not ported all modules, depends what bits you will be using.

                                      However, my learned colleagues may disagree and advise you to go for Qt6, i don't know....

                                      R 1 Reply Last reply
                                      1
                                      • JonBJ JonB

                                        @Risver
                                        You can indeed, though my personal preference would be to take the latest Qt5.x release. I still see too many issues with Qt6, not to mention that I think they still have not ported all modules, depends what bits you will be using.

                                        However, my learned colleagues may disagree and advise you to go for Qt6, i don't know....

                                        R Offline
                                        R Offline
                                        Risver
                                        wrote on last edited by Risver
                                        #23

                                        @JonB
                                        I'm installing Qt 6 for now, i will check if everything works. If it won't i will install Qt 5.15

                                        Okay, I configured the project in the new Qt and i still have the same error.

                                        jsulmJ 1 Reply Last reply
                                        0
                                        • R Risver

                                          @JonB
                                          I'm installing Qt 6 for now, i will check if everything works. If it won't i will install Qt 5.15

                                          Okay, I configured the project in the new Qt and i still have the same error.

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

                                          @Risver said in Deleting row from QTableWidget and from Sqlite database:

                                          I configured the project in the new Qt and i still have the same error.

                                          And you are sure you're building with Qt6 now?
                                          And did you do a complete rebuild?

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

                                          R 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