• 5947阅读
  • 2回复

求助:谁用过的QxtScheduleView [复制链接]

上一主题 下一主题
离线ppdayz
 

只看楼主 倒序阅读 楼主  发表于: 2011-08-16
求助:谁用过的QxtScheduleView
最近要做计划任务,发现qxt的QxtScheduleView很不错,就是没有例子,哪位有例子的麻烦分享下,谢谢了
离线逍遥心

只看该作者 1楼 发表于: 2011-08-16
我也支持啊,哪位大大,提供下代码来看看。
离线ppdayz

只看该作者 2楼 发表于: 2011-08-17
  1. #ifndef _GC_QxtScheduleViewProxy_h
  2. #define _GC_QxtScheduleViewProxy_h 1
  3. #include "GoldenCheetah.h"
  4. #include <QtGui>
  5. #include "qxtscheduleview.h"
  6. #include "MainWindow.h"
  7. #include "RideMetadata.h"
  8. #include "Colors.h"
  9. #include "Settings.h"
  10. // Proxy model for doing groupBy
  11. class QxtScheduleViewProxy : public QAbstractProxyModel
  12. {
  13.     Q_OBJECT
  14.     G_OBJECT
  15. private:
  16.     QxtScheduleView *rideNavigator;
  17.     QList<FieldDefinition> *fieldDefinitions;
  18.     QList<QString> columns; // what columns in the sql model
  19.     MainWindow *mainWindow;
  20.     int filenameIndex, durationIndex, dateIndex, summaryIndex;
  21. public:
  22.     QxtScheduleViewProxy(QWidget *parent, QList<FieldDefinition> *fields, MainWindow *main) : QAbstractProxyModel(parent), fieldDefinitions(fields), mainWindow(main) {
  23.         setParent(parent);
  24.     }
  25.     ~QxtScheduleViewProxy() {}
  26.     void setSourceModel(QAbstractItemModel *model) {
  27.         QAbstractProxyModel::setSourceModel(model);
  28.         summaryIndex = filenameIndex = dateIndex = durationIndex = -1;
  29.         columns.clear();
  30.         for (int i=0; i<sourceModel()->columnCount(); i++) {
  31.             QString column = sourceModel()->headerData (i, Qt::Horizontal, Qt::DisplayRole).toString();
  32.             columns << column;
  33.             if (column == tr("Duration")) durationIndex = i;
  34.             if (column == tr("Date")) dateIndex = i;
  35.             if (column == tr("Filename")) filenameIndex = i;
  36.             if (column == tr("Calendar Text")) summaryIndex = i;
  37.         }
  38.     }
  39.     QModelIndex index(int row, int column, const QModelIndex &/*parent*/ = QModelIndex()) const {
  40.         return createIndex(row,column,(void *)NULL);
  41.     }
  42.     QModelIndex parent(const QModelIndex &index) const {
  43.         // parent should be encoded in the index if we supplied it, if
  44.         // we didn't then return a duffer
  45.         if (index == QModelIndex() || index.internalPointer() == NULL) {
  46.             return QModelIndex();
  47.         } else if (index.column()) {
  48.             return QModelIndex();
  49.         }  else {
  50.             return *static_cast<QModelIndex*>(index.internalPointer());
  51.         }
  52.     }
  53.     QModelIndex mapToSource(const QModelIndex &proxyIndex) const {
  54.         return sourceModel()->index(proxyIndex.row(), proxyIndex.column(), QModelIndex());
  55.     }
  56.     QModelIndex mapFromSource(const QModelIndex &sourceIndex) const {
  57.         return createIndex(sourceIndex.row(), sourceIndex.column(), (void *)NULL); // accomodate virtual column
  58.     }
  59.     // we override the standard version to make our virtual column zero
  60.     // selectable. If we don't do that then the arrow keys don't work
  61.     // since there are no valid rows to cursor up or down to.
  62.     Qt::ItemFlags flags (const QModelIndex &/*index*/) const {
  63.         return 0; //Qt::ItemIsSelectable | Qt::ItemIsEnabled;
  64.     }
  65.     // we'll add some roles to access specific bits of info to!
  66.     enum UserRoles {
  67.         FilenameRole = Qxt::UserRole + 1
  68.     };
  69.     QVariant data(const QModelIndex &proxyIndex, int role = Qt::DisplayRole) const {
  70.         if (!proxyIndex.isValid()) return QVariant();
  71.         QVariant returning;
  72.         switch (role) {
  73.         // do something interesting!
  74.         case Qxt::ItemStartTimeRole:
  75.             if (dateIndex >= 0)
  76.                 return sourceModel()->data(sourceModel()->index(proxyIndex.row(), dateIndex), Qt::DisplayRole).toDateTime().toTime_t();
  77.             else
  78.                 return 0;
  79.             break;
  80.         case Qxt::ItemDurationRole:
  81.             if (durationIndex >= 0) {
  82.                 int duration = sourceModel()->data(sourceModel()->index(proxyIndex.row(), durationIndex), Qt::DisplayRole).toInt();
  83.                 if (duration) return duration;
  84.                 else return 1; // zeros cause QxtSchedule to crash!
  85.             } else
  86.                 return 3600; // Just default to an hour
  87.             break;
  88.         case Qt::BackgroundRole:
  89.             if (mainWindow->rideItem() && dateIndex >= 0 &&
  90.                 sourceModel()->data(sourceModel()->index(proxyIndex.row(), dateIndex), Qt::DisplayRole).toDateTime() == mainWindow->rideItem()->dateTime)
  91.                 return GColor(CCALCURRENT);
  92.             else
  93.                 return GColor(CCALACTUAL);
  94.             break;
  95.         case Qt::ForegroundRole:
  96.             return Qt::black;
  97.             break;
  98.         case Qt::FontRole:
  99.             {
  100.                 QFont font;
  101.                 font.fromString(appsettings->value(this, GC_FONT_CALENDAR, QFont().toString()).toString());
  102.                 font.setPointSize(appsettings->value(this, GC_FONT_CALENDAR_SIZE, 12).toInt());
  103.                 return font;
  104.             }
  105.             break;
  106.         case Qxt::OutlineRole:
  107.             if (mainWindow->rideItem() && dateIndex >= 0 &&
  108.                 sourceModel()->data(sourceModel()->index(proxyIndex.row(), dateIndex), Qt::DisplayRole).toDateTime() == mainWindow->rideItem()->dateTime)
  109.                 return 3;
  110.             else
  111.                 return 1;
  112.             break;
  113.         case FilenameRole:
  114.             if (filenameIndex >= 0)
  115.                 return sourceModel()->data(sourceModel()->index(proxyIndex.row(), filenameIndex), Qt::DisplayRole).toString();
  116.             else
  117.                 return "";
  118.             break;
  119.         case Qt::EditRole:
  120.         case Qt::DisplayRole:
  121.             return sourceModel()->data(sourceModel()->index(proxyIndex.row(), summaryIndex), Qt::DisplayRole).toString();
  122.             break;
  123.         }
  124.         return QVariant();
  125.     }
  126.     QVariant headerData (int section, Qt::Orientation orientation, int role = Qt::DisplayRole ) const {
  127.         return sourceModel()->headerData(section, orientation, role);
  128.     }
  129.     bool setHeaderData (int section, Qt::Orientation orientation, const QVariant & value, int role = Qt::EditRole) {
  130.         return sourceModel()->setHeaderData(section, orientation, value, role);
  131.     }
  132.     int columnCount(const QModelIndex &parent = QModelIndex()) const {
  133.         return sourceModel()->columnCount(parent);
  134.     }
  135.     int rowCount(const QModelIndex &parent = QModelIndex()) const {
  136.         return sourceModel()->rowCount(parent);
  137.     }
  138.     // does this index have children?
  139.     bool hasChildren(const QModelIndex &index) const {
  140.         return sourceModel()->hasChildren(index);
  141.     }
  142. };
  143. #endif
找了一天,找到个引用的,是在一个叫srhea-GoldenCheetah的项目里,可惜项目有点问题,可以去关注下,貌似这个只能自己试了

快速回复
限100 字节
 
上一个 下一个