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.
  • R Risver

    Hi, I have a QTableWidget which is loading data from my Sqlite database. I need to make a delete selected row mechanism, but i don't know how to do this. Please help.

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    
    #include <QObject>
    #include <QMainWindow>
    #include <QSqlQueryModel>
    #include <QHeaderView>
    
    #include "signup.h"
    #include "login.h"
    #include "dbmanager.h"
    
    
    QT_BEGIN_NAMESPACE
    namespace Ui { class MainWindow; }
    QT_END_NAMESPACE
    
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    
    public:
        MainWindow(QWidget *parent = nullptr);
        ~MainWindow();
    
    public slots:
        void nextWindow(QString);
    
    private slots:
        void on_login_clicked();
        void on_signup_clicked();
        void on_remove_clicked();
    
        void on_tableWidget_cellClicked(int row, int column);
    
    private:
        //Ui::MainWindow *ui;
        login * m_logWindow{nullptr};
        signup * m_regWindow{nullptr};
        QSqlQueryModel * querymodel;
    
        void readList(QString);
        QString Bufferbase;
    
        QSqlDatabase m_db;
        QSqlQuery qry;
    
    protected:
        Ui::MainWindow *ui;
    };
    #endif // MAINWINDOW_H
    
    
    #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);
    
        ui->tableWidget->setColumnCount(3);
        QStringList labels;
        labels << "Url" << "E-mail" << "Password";
        ui->tableWidget->setHorizontalHeaderLabels(labels);
    
        int rowCount = 0;
        while(qry.next())
        {
            ui->tableWidget->insertRow(rowCount);
            QTableWidgetItem * url = new QTableWidgetItem;
            QTableWidgetItem * email = new QTableWidgetItem;
            QTableWidgetItem * password = new QTableWidgetItem;
    
            url->setText(qry.value(1).toString());
            email->setText(qry.value(1).toString());
            password->setText(qry.value(1).toString());
    
            ui->tableWidget->setItem(rowCount, 0, url);
            ui->tableWidget->setItem(rowCount, 1, email);
            ui->tableWidget->setItem(rowCount, 2, password);
    
            rowCount++;
        }
    }
    
    void MainWindow::on_remove_clicked()
    {
        
    }
    

    Thank you in advance!

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

    @Risver Why don't you use QTableView with a proper model?

    Back to your question: what exactly are you asking? To delete a row from a table in a SQL database you use https://www.w3schools.com/sql/sql_delete.asp . So, what is unclear?

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

    R 1 Reply Last reply
    2
    • jsulmJ jsulm

      @Risver Why don't you use QTableView with a proper model?

      Back to your question: what exactly are you asking? To delete a row from a table in a SQL database you use https://www.w3schools.com/sql/sql_delete.asp . So, what is unclear?

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

      @jsulm
      I read that in QTableView you cannot delete the rows.
      Yes, I know that i can use SQL query but i need to deleting the row after index. For example when i delete the 1 row, the second row will be the first with id = 2.

      jsulmJ 1 Reply Last reply
      0
      • R Risver

        @jsulm
        I read that in QTableView you cannot delete the rows.
        Yes, I know that i can use SQL query but i need to deleting the row after index. For example when i delete the 1 row, the second row will be the first with id = 2.

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

        @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

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

        R 1 Reply Last reply
        2
        • 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 Offline
                Christian EhrlicherC Offline
                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 Offline
                    Christian EhrlicherC Offline
                    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

                                          • Login

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