c++ - Changing data in Qt table view -
i went through previous questions on stackoverflow regarding updating table view , qt tutorial still cannot code work. want update table view data setdata method when push button pressed. method insertdata inside custom model tablemodel gets called data not changed (new rows added). problem? i'm not sure if relevant table view on qwidget on tab widget.
mainwindow.cpp
#include "mainwindow.h" #include "ui_mainwindow.h" #include "tablemodel.h" #include <iostream> mainwindow::mainwindow(qwidget *parent) : qmainwindow(parent), ui(new ui::mainwindow) { ui->setupui(this); this->datafetcher = new datafetcher(); tm = new tablemodel(0); ui->tableview->setmodel(tm); ui->tableview->show(); this->setpushbuttonhandlers(); } mainwindow::~mainwindow() { delete ui; delete this->datafetcher; } void mainwindow::setpushbuttonhandlers() { connect(this->ui->refreshdatabutton, signal (clicked()), this, slot (refreshdatahandler())); } void mainwindow::refreshdatahandler() { this->tm->insertdata(); }
tablemodel.cpp
#include "tablemodel.h" #include <iostream> tablemodel::tablemodel(qobject *parent): qabstracttablemodel(parent) { } int tablemodel::rowcount(const qmodelindex &parent) const { return 2; } int tablemodel::columncount(const qmodelindex &parent) const { return 16; } qvariant tablemodel::data(const qmodelindex &index, int role) const { if (role == qt::displayrole) { return qstring("row%1, column%2") .arg(index.row() + 1) .arg(index.column() +1); } return qvariant(); } qvariant tablemodel::headerdata(int section, qt::orientation orientation, int role) const { if (role == qt::displayrole) { if (orientation == qt::horizontal) { switch (section) { case 0: return qstring("symbol"); case 1: return qstring("name"); case 2: return qstring("stock exchange"); case 3: return qstring("trade price"); case 4: return qstring("ask"); case 5: return qstring("bid"); case 6: return qstring("change"); case 7: return qstring("change %"); case 8: return qstring("previous close"); case 9: return qstring("open"); case 10: return qstring("days low"); case 11: return qstring("days high"); case 12: return qstring("year low"); case 13: return qstring("year high"); case 14: return qstring("change % year low"); case 15: return qstring("change % year high"); } } else if (orientation == qt::vertical) { return qvariant(qstring::number(section+1)); } } return qvariant(); } void tablemodel::insertdata() { begininsertrows(qmodelindex(), 3, 100); endinsertrows(); qmodelindex rv = this->index(1, 1); setdata(rv, 9999); emit datachanged(rv, rv); qstring s = data(rv).tostring(); std::cout << s.toutf8().constdata() << std::endl; }
at first should re-implement insertrows
virtual method of qabstractitemmodel
. next, should refactor code following:
void tablemodel::insertdata() { insertrows(rowcount(), 1, qmodelindex()); } bool tablemodel::insertrows(int row, int count, const qmodelindex &index) { begininsertrows(); items.push_back(""); endinsertrows(); return true; } qvariant tablemodel::data(const qmodelindex &index, int role) const { if (index.isvalid() && role == qt::displayrole) { return items.at(index.row()); } return qvariant(); } int tablemodel::rowcount(const qmodelindex &parent) const { return items.size(); }
also, add data actually:
class tablemodel { // ...your code... private: std::vector<qstring> items; };
this code example code - when moving production code absolutely should changes.
Comments
Post a Comment