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. Use QCombox to display data in QTableView
QtWS25 Last Chance

Use QCombox to display data in QTableView

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 328 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.
  • M Offline
    M Offline
    makopo
    wrote on last edited by
    #1

    Hello,

    I have a large quantity of data in my program which is ordered in categories. This categories I added successfully to QComboBox. I want to use QComboBox to display the ordered data into a QTableView.

    The special thing in my program is, that the data is not loaded from a file or database, its live data that is directly grabbed by the program and stored in memory. Then the data is send to a slot where the data is consign to the table model.

    I use QComboBox::activated(int index) to select an item of combo box. The corresponding slot get the grabbed data as class member and send it to the slot.
    A minimal code example:

    QObject::connect(m_type1Selection, &QComboBox::activated, m_in, &Input::dataRelation, Qt::QueuedConnection);
    
    void Input::dataRelation(int index)
    {
    	if (index == 0 && m_did == "A0") {
    		sendAncillaryData(data.dataName, m_did);
    	}
    }
    

    I think this is an silly construct, also because of that the data is not keep updated, but as beginner I did get no better idea.
    What is the best way to handle this interaction?

    JonBJ 1 Reply Last reply
    0
    • Christian EhrlicherC Online
      Christian EhrlicherC Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #4

      Put all data in your model and filter with QSortFilterProxyModel.

      emit dataChanged(createIndex(0, 1), createIndex(0, 1), { Qt::DisplayRole });

      Not needed and very problematic within a begin/EndWhatEverRows/Column block.

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

      M 1 Reply Last reply
      2
      • M makopo

        Hello,

        I have a large quantity of data in my program which is ordered in categories. This categories I added successfully to QComboBox. I want to use QComboBox to display the ordered data into a QTableView.

        The special thing in my program is, that the data is not loaded from a file or database, its live data that is directly grabbed by the program and stored in memory. Then the data is send to a slot where the data is consign to the table model.

        I use QComboBox::activated(int index) to select an item of combo box. The corresponding slot get the grabbed data as class member and send it to the slot.
        A minimal code example:

        QObject::connect(m_type1Selection, &QComboBox::activated, m_in, &Input::dataRelation, Qt::QueuedConnection);
        
        void Input::dataRelation(int index)
        {
        	if (index == 0 && m_did == "A0") {
        		sendAncillaryData(data.dataName, m_did);
        	}
        }
        

        I think this is an silly construct, also because of that the data is not keep updated, but as beginner I did get no better idea.
        What is the best way to handle this interaction?

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

        @makopo
        For keeping view up to date with changing model you should be using "insert rows" or setData(), which raise the necessary signals so that the view knows rows have changed.

        Whether you need "changed data in memory" to be "send to a slot where the data is consign to the table model." --- i.e. you have one set of the genuine data in memory and some copy in the model, as opposed to using the data in memory for a custom model (derived from QAbstractItemModel) directly --- is a design/architectural decision.

        I don't know what your sendAncillaryData() method does, fired in response to a combo box activation.

        M 1 Reply Last reply
        1
        • JonBJ JonB

          @makopo
          For keeping view up to date with changing model you should be using "insert rows" or setData(), which raise the necessary signals so that the view knows rows have changed.

          Whether you need "changed data in memory" to be "send to a slot where the data is consign to the table model." --- i.e. you have one set of the genuine data in memory and some copy in the model, as opposed to using the data in memory for a custom model (derived from QAbstractItemModel) directly --- is a design/architectural decision.

          I don't know what your sendAncillaryData() method does, fired in response to a combo box activation.

          M Offline
          M Offline
          makopo
          wrote on last edited by makopo
          #3

          @JonB

          I'am using beginInsertRows(), endInsertRows() and dataChanged()-method to hold the model up-to-date. Also displaying data without combo box works fine. If I use QComboBox only the last data item that was grabbed is displayed.
          The following code shows the whole data in the table view:

          void DataTableModel::addData(QString name, QString did, QString sdidOrDbn, QString location)
          {
              beginInsertRows(QModelIndex(), m_tableItems.count(), m_tableItems.count());
              DataTableItems items;
              items.m_dataName = name;
              items.m_did = did;
              items.m_sdid = sdidOrDbn;
              items.m_location = location;
              emit dataChanged(createIndex(0, 1), createIndex(0, 1), { Qt::DisplayRole });
              m_tableItems.push_back(items);
              endInsertRows();
          }
          
          //function  that sends the data
          void Input::dataRelation(QString did, QString sdidOrDbn, QString line)
          {
          	ANCDataNames ancName;
          	if (did == "A0") {
          		sendData(ancName.audioPacketHDGroup2, did, sdidOrDbn, line);
          	}
          }
          //function that receives the data and put it to the model
          void QMainClass::receiveData(QString name, QString did, QString sdidOrDbn, QString line)
          {
              m_tableModel->addData(name, did, sdidOrDbn, line);
          }
          

          The sendAncillaryData() method sends the data directly to the slot, where the data is consign to the table model. My idea is that the signal with data of "category A" is send if the "category A" item in the combo box is activated. But this only shows the last data item. I'am not sure how to construct this. The combo box and slot that receives the data runs in the main-class of Qt, the data is grabbed in a worker class and the model has its own class of course.
          Is it a better idea to store the data into a csv file and read the data from there with combo box action?

          1 Reply Last reply
          0
          • Christian EhrlicherC Online
            Christian EhrlicherC Online
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by
            #4

            Put all data in your model and filter with QSortFilterProxyModel.

            emit dataChanged(createIndex(0, 1), createIndex(0, 1), { Qt::DisplayRole });

            Not needed and very problematic within a begin/EndWhatEverRows/Column block.

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

            M 1 Reply Last reply
            2
            • Christian EhrlicherC Christian Ehrlicher

              Put all data in your model and filter with QSortFilterProxyModel.

              emit dataChanged(createIndex(0, 1), createIndex(0, 1), { Qt::DisplayRole });

              Not needed and very problematic within a begin/EndWhatEverRows/Column block.

              M Offline
              M Offline
              makopo
              wrote on last edited by
              #5

              @Christian-Ehrlicher
              Hi,

              thanks for recommendation of QSortFilterProxyModel! I got it running.

              Kind regards.

              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