Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Is there a way to call the tableview again?
Forum Updated to NodeBB v4.3 + New Features

Is there a way to call the tableview again?

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
2 Posts 2 Posters 197 Views
  • 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.
  • K Offline
    K Offline
    Kim chang hee
    wrote on last edited by
    #1

    I want to output the db value corresponding to that value in the table view when the button for that value is pressed.
    It is possible to receive the db from the cpp file and input it into a vector, but I do not know how to call the table view where this vector value will be entered again when the button is pressed....

    producttable.h

    #ifndef PRODUCTTABLE_H
    #define PRODUCTTABLE_H
    
    #include <QObject>
    #include <QAbstractTableModel>
    #include <QSqlTableModel>
    #include "sqlquerymodel.h"
    
    class ProductTable : public QAbstractTableModel
    {
        Q_OBJECT
        enum TableRoles{
            TableDataRole = Qt::UserRole + 1,
            HeadingRole
        };
    
    public:
        explicit ProductTable(QObject *parent = nullptr);
        int rowCount(const QModelIndex & = QModelIndex()) const override;
    
        int columnCount(const QModelIndex & = QModelIndex()) const override;
    
        QVariant data(const QModelIndex &index, int role) const override;
    
        QHash<int, QByteArray> roleNames() const override;
    
    signals:
        void qmlSignal(QString msg);
    
    public slots:
        void cppSlot(const QString &msg);
    
    private:
        QVector<QVector<QString>> productTable;
        SqlQueryModel *db;
        QString c_name=nullptr;
    
    };
    
    #endif // PRODUCTTABLE_H
    

    producttable.cpp

    #include "producttable.h"
    #include <QQmlApplicationEngine>
    
    ProductTable::ProductTable(QObject *parent)
        : QAbstractTableModel{parent}
    {
        productTable.clear();
    
        QSqlQuery query;
        QString c_code;
    
        if(c_name==nullptr){
    
            query.prepare("select product_name, product_sale from product");
            query.exec();
    
            QVector<QString> e;
    
            int i=0;
            while(query.next()){
                e.append(query.value(0).toString() + "\n" + query.value(1).toString() + "원");
                i++;
                if(i==4){
                    productTable.append(e);
                    e.clear();
                    i=0;
                }
            }
        }
        else{
            query.prepare("select cartegory_code from cartegory where cartegory_name = '"+c_name+"'");
            query.exec();
            query.next();
            c_code=query.value(0).toString();
    
            query.prepare("select product_name, product_sale from product where cartegory_code = "+c_code+"");
            query.exec();
    
            QVector<QString> e;
    
            int i=0;
            while(query.next()){
                e.append(query.value(0).toString() + "\n" + query.value(1).toString() + "원");
                i++;
                if(i==4){
                    productTable.append(e);
                    e.clear();
                    i=0;
                }
            }
        }
    }
    
    int ProductTable::rowCount(const QModelIndex &) const{
        return productTable.size();
    }
    
    int ProductTable::columnCount(const QModelIndex &) const{
        return productTable.at(0).size();
    }
    
    QVariant ProductTable::data(const QModelIndex &index, int role) const{
        switch(role){
            case TableDataRole:
                return productTable.at(index.row()).at(index.column());
            case HeadingRole:
                if(index.row()==0){
                    return true;
                } else{
                    return false;
                }
            default:
                break;
        }
    
        return QVariant();
    }
    
    QHash<int, QByteArray> ProductTable::roleNames() const{
        QHash<int, QByteArray> roles;
        roles[TableDataRole] = "producttabledata";
        roles[HeadingRole] = "heading";
        return roles;
    }
    
    void ProductTable::cppSlot(const QString &msg){
        c_name=msg;
    
        productTable.clear();
        QString c_code;
        QSqlQuery query;
    
        query.prepare("select cartegory_code from cartegory where cartegory_name = '"+c_name+"'");
        query.exec();
        query.next();
        c_code=query.value(0).toString();
    
        query.prepare("select product_name, product_sale from product where cartegory_code = "+c_code+"");
        query.exec();
    
        QVector<QString> e;
    
        int i=0;
        while(query.next()){
            e.append(query.value(0).toString() + "\n" + query.value(1).toString() + "원");
            i++;
            if(i==4){
                productTable.append(e);
                e.clear();
                i=0;
            }
        }
        //qDebug() << productTable;
    
        //QQmlApplicationEngine engine;
        //engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
        //productTable.refresh();
        qDebug()<< c_name;
    
        //beginResetModel();
        //endResetModel();
    }
    
    

    main.qml

    TableView {
                            id: producttableview
                            x: 5
                            y: 5
                            width: 380
                            height: 435
                            columnSpacing: 10
                            rowSpacing: 80
                            clip: true
    
                            property var columnWidths: [87.5, 87.5, 87.5, 87.5]
                            columnWidthProvider: function (column) { return columnWidths[column] }
    
    
                            property alias tableVerticalBar: productVerticalBar
    
                            ScrollBar.vertical: ScrollBar {
                                id: productVerticalBar
                                policy:ScrollBar.AlwaysOn
                            }
    
                            model: ProductTable {}
    
                            delegate: Rectangle{
                                height: 100
    
                                Button {
                                    width: 88
                                    height: 115
    
                                    Text {
                                        text: producttabledata
                                        font.pointSize: 9
                                        anchors.bottom: parent.bottom
                                    }
                                    onClicked: {
                                        //saletext2.text = categorytabledata
                                    }
                                }
                            }
    
    
                        }
    
    

    When clicking a button in the table view below, I want the table view above to change to the vector value changed in cpp.

     TableView {
                            x: 5
                            y: 5
                            width: 380
                            height: 100
                            columnSpacing: 10
                            rowSpacing: 10
                            clip: true
    
                            property var columnWidths: [65, 65, 65, 65, 65]
                            columnWidthProvider: function (column) { return columnWidths[column] }
    
                            property alias tableVerticalBar: categoryVerticalBar
    
                            ScrollBar.vertical: ScrollBar {
                                id: categoryVerticalBar
                                policy:ScrollBar.AlwaysOn
                            }
    
                            model: CategoryTable {}
    
                            delegate: Rectangle{
                                border.color: "gray"
                                border.width: 0.5
    
                                Button {
                                    id:category_btn
                                    width: 70
                                    height: 50
                                    text: categorytabledata
                                    font.pointSize: 9
    
    
                                    onClicked: {
                                        saletext2.text = categorytabledata
                                        //wwwindow.close()
                                        qmlSignal(categorytabledata)
    
                                        //salelistwindow.show()
    
                                    }
                                }
                            }
                        }
    

    For example, I want to display 10 digits in the table view when button 1 is clicked and 20 digits when button 2 is clicked.
    3cebdf58-caf0-4101-adf2-6e416e5875eb-image.png

    In my code, when a button corresponding to the category of convenience store is clicked, for example, beverage, I want to receive only product information corresponding to the beverage category in the product table and print it on the table.

    It works fine until I get it from the db, but I don't know how to call the table again...```
    code_text

    jsulmJ 1 Reply Last reply
    0
    • K Kim chang hee

      I want to output the db value corresponding to that value in the table view when the button for that value is pressed.
      It is possible to receive the db from the cpp file and input it into a vector, but I do not know how to call the table view where this vector value will be entered again when the button is pressed....

      producttable.h

      #ifndef PRODUCTTABLE_H
      #define PRODUCTTABLE_H
      
      #include <QObject>
      #include <QAbstractTableModel>
      #include <QSqlTableModel>
      #include "sqlquerymodel.h"
      
      class ProductTable : public QAbstractTableModel
      {
          Q_OBJECT
          enum TableRoles{
              TableDataRole = Qt::UserRole + 1,
              HeadingRole
          };
      
      public:
          explicit ProductTable(QObject *parent = nullptr);
          int rowCount(const QModelIndex & = QModelIndex()) const override;
      
          int columnCount(const QModelIndex & = QModelIndex()) const override;
      
          QVariant data(const QModelIndex &index, int role) const override;
      
          QHash<int, QByteArray> roleNames() const override;
      
      signals:
          void qmlSignal(QString msg);
      
      public slots:
          void cppSlot(const QString &msg);
      
      private:
          QVector<QVector<QString>> productTable;
          SqlQueryModel *db;
          QString c_name=nullptr;
      
      };
      
      #endif // PRODUCTTABLE_H
      

      producttable.cpp

      #include "producttable.h"
      #include <QQmlApplicationEngine>
      
      ProductTable::ProductTable(QObject *parent)
          : QAbstractTableModel{parent}
      {
          productTable.clear();
      
          QSqlQuery query;
          QString c_code;
      
          if(c_name==nullptr){
      
              query.prepare("select product_name, product_sale from product");
              query.exec();
      
              QVector<QString> e;
      
              int i=0;
              while(query.next()){
                  e.append(query.value(0).toString() + "\n" + query.value(1).toString() + "원");
                  i++;
                  if(i==4){
                      productTable.append(e);
                      e.clear();
                      i=0;
                  }
              }
          }
          else{
              query.prepare("select cartegory_code from cartegory where cartegory_name = '"+c_name+"'");
              query.exec();
              query.next();
              c_code=query.value(0).toString();
      
              query.prepare("select product_name, product_sale from product where cartegory_code = "+c_code+"");
              query.exec();
      
              QVector<QString> e;
      
              int i=0;
              while(query.next()){
                  e.append(query.value(0).toString() + "\n" + query.value(1).toString() + "원");
                  i++;
                  if(i==4){
                      productTable.append(e);
                      e.clear();
                      i=0;
                  }
              }
          }
      }
      
      int ProductTable::rowCount(const QModelIndex &) const{
          return productTable.size();
      }
      
      int ProductTable::columnCount(const QModelIndex &) const{
          return productTable.at(0).size();
      }
      
      QVariant ProductTable::data(const QModelIndex &index, int role) const{
          switch(role){
              case TableDataRole:
                  return productTable.at(index.row()).at(index.column());
              case HeadingRole:
                  if(index.row()==0){
                      return true;
                  } else{
                      return false;
                  }
              default:
                  break;
          }
      
          return QVariant();
      }
      
      QHash<int, QByteArray> ProductTable::roleNames() const{
          QHash<int, QByteArray> roles;
          roles[TableDataRole] = "producttabledata";
          roles[HeadingRole] = "heading";
          return roles;
      }
      
      void ProductTable::cppSlot(const QString &msg){
          c_name=msg;
      
          productTable.clear();
          QString c_code;
          QSqlQuery query;
      
          query.prepare("select cartegory_code from cartegory where cartegory_name = '"+c_name+"'");
          query.exec();
          query.next();
          c_code=query.value(0).toString();
      
          query.prepare("select product_name, product_sale from product where cartegory_code = "+c_code+"");
          query.exec();
      
          QVector<QString> e;
      
          int i=0;
          while(query.next()){
              e.append(query.value(0).toString() + "\n" + query.value(1).toString() + "원");
              i++;
              if(i==4){
                  productTable.append(e);
                  e.clear();
                  i=0;
              }
          }
          //qDebug() << productTable;
      
          //QQmlApplicationEngine engine;
          //engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
          //productTable.refresh();
          qDebug()<< c_name;
      
          //beginResetModel();
          //endResetModel();
      }
      
      

      main.qml

      TableView {
                              id: producttableview
                              x: 5
                              y: 5
                              width: 380
                              height: 435
                              columnSpacing: 10
                              rowSpacing: 80
                              clip: true
      
                              property var columnWidths: [87.5, 87.5, 87.5, 87.5]
                              columnWidthProvider: function (column) { return columnWidths[column] }
      
      
                              property alias tableVerticalBar: productVerticalBar
      
                              ScrollBar.vertical: ScrollBar {
                                  id: productVerticalBar
                                  policy:ScrollBar.AlwaysOn
                              }
      
                              model: ProductTable {}
      
                              delegate: Rectangle{
                                  height: 100
      
                                  Button {
                                      width: 88
                                      height: 115
      
                                      Text {
                                          text: producttabledata
                                          font.pointSize: 9
                                          anchors.bottom: parent.bottom
                                      }
                                      onClicked: {
                                          //saletext2.text = categorytabledata
                                      }
                                  }
                              }
      
      
                          }
      
      

      When clicking a button in the table view below, I want the table view above to change to the vector value changed in cpp.

       TableView {
                              x: 5
                              y: 5
                              width: 380
                              height: 100
                              columnSpacing: 10
                              rowSpacing: 10
                              clip: true
      
                              property var columnWidths: [65, 65, 65, 65, 65]
                              columnWidthProvider: function (column) { return columnWidths[column] }
      
                              property alias tableVerticalBar: categoryVerticalBar
      
                              ScrollBar.vertical: ScrollBar {
                                  id: categoryVerticalBar
                                  policy:ScrollBar.AlwaysOn
                              }
      
                              model: CategoryTable {}
      
                              delegate: Rectangle{
                                  border.color: "gray"
                                  border.width: 0.5
      
                                  Button {
                                      id:category_btn
                                      width: 70
                                      height: 50
                                      text: categorytabledata
                                      font.pointSize: 9
      
      
                                      onClicked: {
                                          saletext2.text = categorytabledata
                                          //wwwindow.close()
                                          qmlSignal(categorytabledata)
      
                                          //salelistwindow.show()
      
                                      }
                                  }
                              }
                          }
      

      For example, I want to display 10 digits in the table view when button 1 is clicked and 20 digits when button 2 is clicked.
      3cebdf58-caf0-4101-adf2-6e416e5875eb-image.png

      In my code, when a button corresponding to the category of convenience store is clicked, for example, beverage, I want to receive only product information corresponding to the beverage category in the product table and print it on the table.

      It works fine until I get it from the db, but I don't know how to call the table again...```
      code_text

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

      @Kim-chang-hee Please don't double post!
      This is same question as in: https://forum.qt.io/topic/136727/how-to-call-tableview-again

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

      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