i
ironexmaidenAug. 24, 2017, 5:05 p.m.

QTableView connected with QSqlTableModel flickers (scrolls to the bottom and returns)

qt, qsql, QSqlDataBase, QSqlTableModel, QTableView

Hello guys I have a problem using table view. I have a tableview connected with a QSqlTableModel. On its 2nd column I want to have check state to control the row's Status (active/inactive) but when I try to push the check box QTableView scrolls to the bottom and returns to it's previous position. Is there any solution?

    bool ProjectsModel::setData(const QModelIndex &index, const QVariant &value, int role)
    {
        if(index.column() == STATUS_INDEX) {
            QSqlRecord rec = this->record(index.row());
            QString qrstr;
     
            qrstr = (rec.field(index.column()).value().toInt() == 1) ? "update " + tableName() + " set Status=0 where BRACE_ID=" : "update " + tableName() + " set Status=1 where BRACE_ID=";
            qrstr += QString().setNum(index.row()+1);
     
            braceDatabase->exec(qrstr.toUtf8().constData());
            return true;
        }
     
        return false;
    }
     
    void DataBase::updateTablesSlot()
    {
        if(projectsModel)
            projectsModel->select();
        if(attributesModel)
            attributesModel->select();
        if(targetsModel)
            targetsModel->select();
    }
     
    void DataBase::exec(const std::string &querystr)
    {
    		QSqlQuery qr(db);
            qr.exec(querystr.c_str());
            //qDebug() <<"Command :" << querystr.c_str() << "-> "<< query->lastError().text();
            updateTables(); 
    }
We recommend hosting TIMEWEB
We recommend hosting TIMEWEB
Stable hosting, on which the social network EVILEG is located. For projects on Django we recommend VDS hosting.

Do you like it? Share on social networks!

9
Evgenii Legotckoi
  • Aug. 25, 2017, 2:49 a.m.

Hello!
I think you need to use QItemDelegate for this (or same claas which was inherited from this class). Or You can use QTableWidget.
You can research article with checkboxes in QTableWidget .
May be it will be usefull for You.

    Evgenii Legotckoi
    • Aug. 25, 2017, 3:41 a.m.

    And may be you need use for scroll method scrollTo()

    For this You should to save QModelIndex with which you work now, and after getting information from SQL DataBase, You should invoke method scrollTo(storedIndex) for scroll to needed position.
      i
      • Aug. 25, 2017, 3:45 a.m.
      • (edited)

      Hello my friend and thank you for the help unfortunately I can't use QTableWidget for performance reasons, I must load big databases from csv files.  If I try the second solution with the scroll It will have a disturbing effect the table will hop up and down every time the user presses the check state.

      Christos,
      Greeting from Thessaloniki, Greece

        Evgenii Legotckoi
        • Aug. 25, 2017, 4:14 a.m.

        Ok. May be You need to use custom ItemDelegate for this.


        class CheckBoxDelegate: public QItemDelegate
        {
            Q_OBJECT
        public:
            CheckBoxDelegate(QObject *parent = 0);
        
            void paint( QPainter *painter,
                                const QStyleOptionViewItem &option,
                                const QModelIndex &index ) const;
        
        
            QWidget *createEditor( QWidget *parent,
                                const QStyleOptionViewItem &option,
                                const QModelIndex &index ) const;
        
            void setEditorData( QWidget *editor,
                                const QModelIndex &index ) const;
        
            void setModelData( QWidget *editor,
                                QAbstractItemModel *model,
                                const QModelIndex &index ) const;
        
            void updateEditorGeometry( QWidget *editor,
                                const QStyleOptionViewItem &option,
                                const QModelIndex &index ) const;
        
            mutable QCheckBox * theCheckBox;
        
        private slots:
        
            void setData(bool val);
        
        
        };
        And set delegate
        ui->myTable->setItemDelegateForColumn(5,new CheckBoxDelegate(ui->myTable));

        Best Regards from Prague.
          i
          • Aug. 25, 2017, 4:20 a.m.
           QString qrstr;
               
                      qrstr = (rec.field(index.column()).value().toInt() == 1) ? "update " + tableName() + " set Status=0 where BRACE_ID=" : "update " + tableName() + " set Status=1 where BRACE_ID=";
                      qrstr += QString().setNum(index.row()+1);
               
                      braceDatabase->exec(qrstr.toUtf8().constData());
          Thank you my friend I will check this solution and I will report keep on mind that another guy informed me that the query exec clears the model and reset the data, and this reset is the reason of the flickering he proposed me the solution of QSqlTableModel::setData
          I changed my code to this one but the check state on click didn't change :(
              bool AttributesModel::setData(const QModelIndex &index, const QVariant &value, int role)
              {
                  if(index.column() == STATUS_INDEX) {
                      bool ret = QSqlTableModel::setData(index, value == Qt::Checked ? Qt::Unchecked : Qt::Checked, role);
                      database().commit();
                      this->submitAll();
                      return ret;
                  }
                  return QSqlTableModel::setData(index, value, role);
              }
            Evgenii Legotckoi
            • Aug. 25, 2017, 4:25 a.m.
            • (edited)

            I am not sure, but problem of this solution may be in Qt::Checked and Qt::Unchecked. 

            Because this enum has 3 states

            enum CheckState {
                    Unchecked,
                    PartiallyChecked,
                    Checked
                };
            • Unchecked = 0
            • PartiallyChecked = 1
            • Checked = 2
            But You use two states 0 and 1 in your csv files, if I see correctly.
              i
              • Aug. 25, 2017, 4:30 a.m.

              That's the point I want the status of the row to be Active/Inactive PartiallyChecked option is useless for me. Do you refer that I had an error on this line ?

              bool ret = QSqlTableModel::setData(index, value == Qt::Checked ? Qt::Unchecked : Qt::Checked, role);
                Evgenii Legotckoi
                • Aug. 25, 2017, 4:32 a.m.

                I am not sure, but may be you can try to check the following code in this row:

                bool ret = QSqlTableModel::setData(index, value == Qt::Checked ? 0 : 1, role);
                  i
                  • Aug. 25, 2017, 4:34 a.m.

                  Hmm you may be right I will check it EVILEG and I will report later thank you

                    Comments

                    Only authorized users can post comments.
                    Please, Log in or Sign up
                    AD

                    C ++ - Test 004. Pointers, Arrays and Loops

                    • Result:50points,
                    • Rating points-4
                    m

                    C ++ - Test 004. Pointers, Arrays and Loops

                    • Result:80points,
                    • Rating points4
                    m

                    C ++ - Test 004. Pointers, Arrays and Loops

                    • Result:20points,
                    • Rating points-10
                    Last comments
                    i
                    innorwallNov. 14, 2024, 12:07 p.m.
                    Circuit switching and packet data transmission networks Angioedema 1 priligy dapoxetine
                    i
                    innorwallNov. 14, 2024, 11:42 a.m.
                    How to Copy Files in Linux If only females relatives with DZ offspring were considered these percentages were 23 order priligy online uk
                    i
                    innorwallNov. 14, 2024, 9:09 a.m.
                    Qt/C++ - Tutorial 068. Hello World using the CMAKE build system in CLion ditropan pristiq dosing With the Yankees leading, 4 3, Rivera jogged in from the bullpen to a standing ovation as he prepared for his final appearance in Chicago buy priligy pakistan
                    i
                    innorwallNov. 14, 2024, 4:05 a.m.
                    EVILEG-CORE. Using Google reCAPTCHA 2001; 98 29 34 priligy buy
                    i
                    innorwallNov. 14, 2024, 4 a.m.
                    PyQt5 - Lesson 007. Works with QML QtQuick (Signals and slots) priligy 30mg Am J Obstet Gynecol 171 1488 505
                    Now discuss on the forum
                    i
                    innorwallNov. 14, 2024, 3:39 a.m.
                    добавить qlineseries в функции priligy amazon canada 93 GREB1 protein GREB1 AB011147 6
                    i
                    innorwallNov. 11, 2024, 10:55 a.m.
                    Всё ещё разбираюсь с кешем. priligy walgreens levitra dulcolax carbs The third ring was found to be made up of ultra relativistic electrons, which are also present in both the outer and inner rings
                    9
                    9AnonimOct. 25, 2024, 9:10 a.m.
                    Машина тьюринга // Начальное состояние 0 0, ,<,1 // Переход в состояние 1 при пустом символе 0,0,>,0 // Остаемся в состоянии 0, двигаясь вправо при встрече 0 0,1,>…

                    Follow us in social networks