Advertisement
ZirconiumX

Untitled

Jun 13th, 2019
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.97 KB | None | 0 0
  1. QVariant GameListModel::data(
  2.     const QModelIndex& index, int role
  3. ) const
  4. {
  5.     // might be cool in the future
  6.     // but for now let's just focus
  7.     // on display role
  8.     if (role != Qt::DisplayRole)
  9.         return QVariant();
  10.  
  11.     auto file = games.at(index.row());
  12.     QFileInfo file_info(file);
  13.     auto file_size = double(file_info.size());
  14.  
  15.     switch (index.column())
  16.     {
  17.         case COLUMN_NAME:
  18.             return file_info.fileName();
  19.         case COLUMN_SIZE:
  20.             if (file_size >= 1024*1024*1024) {
  21.                 return QString().sprintf("%.1f GiB", file_size / 1024*1024*1024);
  22.             } else if (file_size >= 1024*1024) {
  23.                 return QString().sprintf("%.1f MiB", file_size / 1024*1024);
  24.             } else if (file_size >= 1024) {
  25.                 return QString().sprintf("%.1f KiB", file_size / 1024);
  26.             }
  27.             return QString().sprintf("%.0lf bytes", file_size);
  28.     }
  29.  
  30.     return QVariant();
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement