c++ - Convert data from QSqlTableModel to human form in QTableView (enum -> String) -


i'm using qsqltablemodel data required table, , i'm rendering in gui using qtableview. problem have want change different fields (convert them) existing value 1 (enum -> string). example existing value of 1 should displaied error in column of qtableview.

my understanding (correct me if i'm wrong) should use delegates.

backuptableview->setitemdelegateforcolumn(4, new statusformatdelegate());

column should changed in case #4.

is there way implement , in both cases (yes/no) example?

p.s. data shouldn't editable.

until statusformatdelegate should this:

class statusformatdelegate : public qstyleditemdelegate { public:     statusformatdelegate (quint64 datafromqtableview, qobject *parent = 0) :   qstyleditemdelegate(parent),   columndata_(datafromqtableview)  {  }   virtual qstring displaytext(const qvariant & value, const qlocale & locale ) const  {   q_unused(locale);   switch(columndata_){   case jobstatus_failed:        return "failed";   case jobstatus_finished:      return "finished";   case jobstatus_inprogress:    return "in progress";   case jobstatus_notstarted:    return "not started";   default:                      return "unknown type";   }  }  private:  quint64 columndata_;  }; 

this method creates model -> view

void transferhistory::fillbackuppagefromdb() {     connecttodb();     qsqltablemodel *model = new qsqltablemodel(this, db);     model->settable("backup_history");     model->seteditstrategy(qsqltablemodel::onmanualsubmit);     model->select();     model->setheaderdata(0, qt::horizontal, tr("id"));     model->setheaderdata(1, qt::horizontal, tr("file"));     model->setheaderdata(2, qt::horizontal, tr("size"));     model->setheaderdata(3, qt::horizontal, tr("back-up time"));     model->setheaderdata(4, qt::horizontal, tr("status"));      ui->backuptableview = new qtableview(this->ui->tabwidget->currentwidget());     ui->backuptableview->setmodel(model);     ui->backuptableview->hidecolumn(0);     ui->backuptableview->setshowgrid(false);     ui->backuptableview->setsortingenabled(true);     int width = ui->tabwidget->currentwidget()->width();     ui->backuptableview->setfixedsize(ui->tabwidget->currentwidget()->size());      **ui->backuptableview->setitemdelegateforcolumn(4, new statusformatdelegate(somedata, this));**     ui->backuptableview->setcolumnwidth(1, static_cast<int>(filecolumnwidth_percent * width));     ui->backuptableview->setcolumnwidth(2, static_cast<int>(sizecolumnwidth_percent * width));     ui->backuptableview->setcolumnwidth(3, static_cast<int>(timecolumn_percent * width));     ui->backuptableview->setcolumnwidth(4, static_cast<int>(statuscolumn_percent * width) - qapp->style()->pixelmetric(qstyle::pm_scrollbarextent) - 2);     ui->backuptableview->show(); } 

to data sql db, following:

connect db:

qsqldatabase db = qsqldatabase::adddatabase("qmysql");     db.sethostname("mozart.konkordia.edu");     db.setdatabasename("musicdb");     db.setusername("gbatstone");     db.setpassword("t17av44");     if (!db.open()) {         qmessagebox::critical(0, qobject::tr("database error"),                               db.lasterror().text()); 

select data db

qsqlquery query("select afield atable");      while (query.next()) {          qstring fielddata = query.value(0).tostring();       } 

use different 'to' methods change type of return value.

see more here and here


Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

python - Django-cities exits with "killed" -

python - How to get a widget position inside it's layout in Kivy? -