Use QCombox to display data in QTableView
-
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 useQComboBox
to display the ordered data into aQTableView
.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? -
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.
-
@makopo
For keeping view up to date with changing model you should be using "insert rows" orsetData()
, 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. -
I'am using
beginInsertRows()
,endInsertRows()
anddataChanged()
-method to hold the model up-to-date. Also displaying data without combo box works fine. If I useQComboBox
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? -
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.
-
thanks for recommendation of
QSortFilterProxyModel
! I got it running.Kind regards.