QT model/view (update data in real time)

Source: Internet
Author: User
Tags rowcount
The last two sections briefly describe the programming of the model/view in Qt, and most of the assistants are very clear about it, so let's start with the actual combat section. In real-world applications, the data displayed by the view is often not static, so how to update it in real time becomes a very important issue. Features:(1) Add delegate (progress bar) (2) display file name, size, progress, speed, remaining time, status, etc. (3) can be added, updated, deleted, emptied and other operations. (4) Update the data in real time first look at an effect diagram:

Delegate (progress bar): Progressbardelegate::P rogressbardelegate (Qobject *parent): qitemdelegate (parent) {
}
void Progressbardelegate::p aint (qpainter* painter, const qstyleoptionviewitem& option, const qmodelindex& Index) const {    IF (index.column () = = 2)     {int progress = Index.model ()-&G T;data (Index, Qt::D isplayrole). ToInt ();         qstyleoptionprogressbarv2 progressbaroption;         progressbaroption.state = Qstyle:: State_Enabled;         progressbaroption.direction = QApplication:: LayoutDirection ();         progressbaroption.rect = Option.rect;         progressbaroption.fontmetrics = QApplication:: FontMetrics ();         progressbaroption.minimum = 0;         progressbaroption.maximum = 100;         progressbaroptIon.textalignment = Qt:: aligncenter;         progressbaroption.textvisible = true;         progressbaroption.progress = progress;         progressbaroption.text = QString ("%1%"). Arg ( progressbaroption.progress);         qapplication:: Style ()->drawcontrol (QStyle::CE_ProgressBar, &progressbaroption, painter);     } else {return qitemdelegate::p aint (painter, option, index);}}
Model: Tablemodel::tablemodel (qobject *parent): Qabstracttablemodel (parent), Arr_row_list (NULL) {
}
Tablemodel::~tablemodel (void) {arr_row_list = NULL;}
void Tablemodel::sethorizontalheaderlist (Qstringlist horizontalheaderlist) {horizontal_header_list = Horizontalheaderlist; }
void Tablemodel::setverticalheaderlist (Qstringlist verticalheaderlist) {vertical_header_list = verticalHeaderList;}
int Tablemodel::rowcount (const qmodelindex &parent) Const {if (vertical_header_list.size () > 0) return Vertical_header_list.size ();
if (NULL = = arr_row_list) return 0; else return arr_row_list->size (); }
int Tablemodel::columncount (const qmodelindex &parent) Const {if (horizontal_header_list.size () > 0) re Turn horizontal_header_list.size ();
if (NULL = = arr_row_list) return 0; else if (Arr_row_list->size () < 1) return 0; else return Arr_row_list->at (0). Size (); }
Qvariant TableModel::d ata (const qmodelindex &index, int role) const {if (!index.isvalid ()) return Qvarian T ();
if (NULL = = arr_row_list) return qvariant ();
if (Arr_row_list->size () < 1) return qvariant ();
if (role = = Qt::textalignmentrole) {return int (Qt::alignleft |     Qt::alignvcenter); } else if (role = = Qt::D isplayrole) {if (Index.row () >= arr_row_list->size ()) return Q         Variant ();         if (Index.column () >= arr_row_list->at (0). Size ()) return qvariant ();     Return Arr_row_list->at (Index.row ()). at (Index.column ()); } return Qvariant (); }
qvariant tablemodel::headerdata (int section, qt::orientation Orientation, int role) const {&NBSP;&NBSP;&NBSP;&NBSP;IF ( ROLE==QT::D isplayrole)       {          if ( Orientation==qt::horizontal)//Horizontal header           {               if (horizontal_header_list.size () > section)                  return Horizontal_ Header_list[section];             else                  return qvariant ();           }           else          {        &nbsP;   if (vertical_header_list.size () > section)                  return Vertical_header_list[section]; Vertical header               else                  return QVariant ();         }       
return Qvariant (); }
Qt::itemflags tablemodel::flags (const qmodelindex &index) Const {if (!index.isvalid ()) return QT::NOITEMF Lags
Qt::itemflags flag = qabstractitemmodel::flags (index);
Flag|=qt::itemiseditable//Set cell editable, note here, cell cannot be edited return flag; }
void Tablemodel::setmodaldatas (qlist< qstringlist > *rowlist) {arr_row_list = rowlist;}
void Tablemodel::refrushmodel () {Beginresetmodel (); Endresetmodel ();
Emit Updatecount (This->rowcount (Qmodelindex ())); }
View: Tableview::tableview (Qwidget *parent): Qtableview (parent) {this->setalternatingrowcolors (true); this->    Setstylesheet ("Qtableview{background-color:rgb (250, 250, 115);" "Alternate-background-color:rgb (141, 163, 215);}"  ); This->setselectionbehavior (qabstractitemview::selectrows); This->horizontalheader ()->setstretchlastsection (true);  This->horizontalheader ()->sethighlightsections (false); This->verticalheader ()->setvisible (false); This->setshowgrid (FALSE); This->setedittriggers (qabstractitemview::noedittriggers); This->setselectionmode (qabstractitemview::extendedselection);
Model = new TableModel ();     This->setmodel (model);     This->initheader ();     Model->setmodaldatas (&grid_data_list);     Progressbar_delegate = new Progressbardelegate (); This->setitemdelegate (progressbar_delegate);
Connect (model, &tablemodel::updatecount, this, &tableview::updatecount);
This->initheader (); }
Tableview::~tableview (void) {if (progressbar_delegate) {delete progressbar_delegate;     Progressbar_delegate = NULL; }
if (model) {delete model;     model = NULL; } grid_data_list.clear (); }
void Tableview::addrow (Qstringlist rowlist) {grid_data_list.append (rowlist); Model->refrushmodel (); }
void Tableview::remove () {qmodelindexlist model_index_list = this->selectedindexes (); int model_count = Model_index_ List.count (); if (model_count <= 0) return;
Qlist List_row; for (int i=model_count-1; i>=0; i--) {Qmodelindex Model_index = model_index_list.at (i); int row = Model_index.row (); if (!list_row.contains (Row)) List_row.append (row); }
if (List_row.isempty ()) return;
QSort (List_row);
for (int i=list_row.count ()-1; i>=0; i--) {grid_data_list.removeat (list_row.at (i));}
Model->refrushmodel (); }
void Tableview::clear () {grid_data_list.clear (); Model->refrushmodel ();}
int Tableview::rowcount () {return Model->rowcount (Qmodelindex ());}
void Tableview::initheader () {qstringlist header; Header << TR ("name") << tr ("size") << TR ("Progress") << tr ("Speed") << tr ("Left Time") <& Lt     TR ("state"); Model->sethorizontalheaderlist (header); }
void Tableview::changevalue () {//Here is updated with 10 line endings int row_count = This->rowcount (); if (Row_count <) return;
for (int i=0; i<10; i++) {qstringlist file_list = grid_data_list.at (i); int progress = rand () 0; Qstringlist row_list; Row_list << file_list.at (0) << file_list.at (1) << Qstring::number (Progress) << file_list.at (3) << file_list.at (4) << file_list.at (5); Grid_data_list.replace (i, row_list); }
Model->refrushmodel (); }
Complete project (source code) Download address: http://download.csdn.net/detail/u011012932/6829783.
Note:Technology lies in communication, communication, reproduced please specify the source and maintain the integrity of the work. Author: ╰☆ Struggle ing❤ Children ' original: http://blog.sina.com.cn/s/blog_a6fb6cc90101hhse.html.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.