KDChart 甘特图在Qt中的加载使用案例,代码来自官方
mainwindow.h
/**************************************************************************** ** Copyright (C) 2001-2018 Klaralvdalens Datakonsult AB. All rights reserved. ** ** This file is part of the KD Chart library. ** ** Licensees holding valid commercial KD Chart licenses may use this file in ** accordance with the KD Chart Commercial License Agreement provided with ** the Software. ** ** ** This file may be distributed and/or modified under the terms of the ** GNU General Public License version 2 and version 3 as published by the ** Free Software Foundation and appearing in the file LICENSE.GPL.txt included. ** ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. ** ** Contact info@kdab.com if any conditions of this licensing are not ** clear to you. ** **********************************************************************/ #ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QItemSelection> #include <QMainWindow> QT_BEGIN_NAMESPACE class QStandardItemModel; class QCloseEvent; namespace Ui { class MainWindow; } QT_END_NAMESPACE namespace KDGantt { class ConstraintModel; class DateTimeGrid; class Legend; } class MainWindow : public QMainWindow { Q_OBJECT public: explicit MainWindow( QWidget * parent = 0, Qt::WindowFlags flags = 0 ); virtual ~MainWindow(); virtual void closeEvent(QCloseEvent *event); private slots: void addNewEntry(); void removeEntry(); void showContextMenu( const QPoint& ); void enableActions( const QItemSelection& selected ); void zoomIn(); void zoomOut(); void zoomFit(); void scaleAuto(); void scaleHour(); void scaleDay(); void scaleWeek(); void scaleMonth(); private: void initModel(); void initActions(); void initItemDelegate(); void initGrid(); void setReadOnly( const QModelIndex& index, bool readOnly ); void addConstraint( const QModelIndex& index1, const QModelIndex& index2 ); QStandardItemModel* model; KDGantt::ConstraintModel* constraintModel; KDGantt::DateTimeGrid* grid; KDGantt::Legend* smallLegend; KDGantt::Legend* detailedLegend; QAction* newEntryAction; QAction* removeEntryAction; QAction* zoomInAction; QAction* zoomOutAction; QAction* zoomFitAction; Ui::MainWindow* ui; }; #endif /* MAINWINDOW_H */
entrydialog.h
/**************************************************************************** ** Copyright (C) 2001-2018 Klaralvdalens Datakonsult AB. All rights reserved. ** ** This file is part of the KD Chart library. ** ** Licensees holding valid commercial KD Chart licenses may use this file in ** accordance with the KD Chart Commercial License Agreement provided with ** the Software. ** ** ** This file may be distributed and/or modified under the terms of the ** GNU General Public License version 2 and version 3 as published by the ** Free Software Foundation and appearing in the file LICENSE.GPL.txt included. ** ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. ** ** Contact info@kdab.com if any conditions of this licensing are not ** clear to you. ** **********************************************************************/ #ifndef ENTRYDIALOG_H #define ENTRYDIALOG_H #include <QDateTime> #include <QDialog> #include <QModelIndex> QT_BEGIN_NAMESPACE class QAbstractItemModel; namespace Ui { class EntryDialog; } QT_END_NAMESPACE namespace KDGantt { class ConstraintModel; } class EntryDialog : public QDialog { Q_OBJECT public: explicit EntryDialog( const QAbstractItemModel* model, QWidget* parent = 0, Qt::WindowFlags f = 0 ); void initFrom( const QModelIndex& index, const KDGantt::ConstraintModel* constraintModel ); QString name() const; int type() const; QDateTime startDate() const; QDateTime endDate() const; int completion() const; bool readOnly() const; QModelIndex depends() const; QString legend() const; private slots: void updateEndDate( const QDateTime& startDate ); void disableEditing( bool disable ); void typeChanged( int index ); private: void init(); void addDependItem( const QAbstractItemModel* model, const QModelIndex& index, int indent = 0 ); QList<QPersistentModelIndex> indexList; const QAbstractItemModel* model; Ui::EntryDialog* ui; }; #endif /* ENTRYDIALOG_H */
entrydelegate.h
/**************************************************************************** ** Copyright (C) 2001-2018 Klaralvdalens Datakonsult AB. All rights reserved. ** ** This file is part of the KD Chart library. ** ** Licensees holding valid commercial KD Chart licenses may use this file in ** accordance with the KD Chart Commercial License Agreement provided with ** the Software. ** ** ** This file may be distributed and/or modified under the terms of the ** GNU General Public License version 2 and version 3 as published by the ** Free Software Foundation and appearing in the file LICENSE.GPL.txt included. ** ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. ** ** Contact info@kdab.com if any conditions of this licensing are not ** clear to you. ** **********************************************************************/ #ifndef ENTRYDELEGATE_H #define ENTRYDELEGATE_H #include <QItemDelegate> namespace KDGantt { class ConstraintModel; } class EntryDelegate : public QItemDelegate { public: explicit EntryDelegate( KDGantt::ConstraintModel* constraintModel, QObject* parent = 0 ); bool editorEvent( QEvent* event, QAbstractItemModel* model, const QStyleOptionViewItem& option, const QModelIndex& index ); private: void addConstraint(const QModelIndex & index1, const QModelIndex & index2); void setReadOnly(const QModelIndex & index, bool readOnly); KDGantt::ConstraintModel* constraintModel; }; #endif /* ENTRYDELEGATE_H */
entrydelegate.cpp
/**************************************************************************** ** Copyright (C) 2001-2018 Klaralvdalens Datakonsult AB. All rights reserved. ** ** This file is part of the KD Chart library. ** ** Licensees holding valid commercial KD Chart licenses may use this file in ** accordance with the KD Chart Commercial License Agreement provided with ** the Software. ** ** ** This file may be distributed and/or modified under the terms of the ** GNU General Public License version 2 and version 3 as published by the ** Free Software Foundation and appearing in the file LICENSE.GPL.txt included. ** ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. ** ** Contact info@kdab.com if any conditions of this licensing are not ** clear to you. ** **********************************************************************/ #include "entrydelegate.h" #include "entrydialog.h" #include <KDGanttConstraintModel> #include <KDGanttGlobal> #include <QEvent> #include <QModelIndex> #include <QStandardItemModel> #include <QPointer> EntryDelegate::EntryDelegate( KDGantt::ConstraintModel* constraintModel, QObject* parent ) : QItemDelegate( parent ) { this->constraintModel = constraintModel; } bool EntryDelegate::editorEvent( QEvent* event, QAbstractItemModel *model, const QStyleOptionViewItem& option, const QModelIndex& index ) { if ( event->type() != QEvent::MouseButtonDblClick ) return false; if ( !index.isValid() ) return QItemDelegate::editorEvent( event, model, option, index ); QPointer<EntryDialog> dialog = new EntryDialog( model ); dialog->initFrom( index, constraintModel ); dialog->setWindowTitle( tr( "Edit Entry" ) ); dialog->exec(); if ( !dialog ) return false; int row = index.row(); const QModelIndex parent = index.parent(); model->setData( model->index( row, 0, parent ), dialog->name() ); model->setData( model->index( row, 1, parent ), dialog->type() ); if ( dialog->type() != KDGantt::TypeSummary ) { model->setData( model->index( row, 2, parent ), dialog->startDate(), KDGantt::StartTimeRole ); model->setData( model->index( row, 3, parent ), dialog->endDate(), KDGantt::EndTimeRole ); } model->setData( model->index( row, 4, parent ), dialog->completion() ); model->setData( model->index( row, 5, parent ), dialog->legend() ); addConstraint( dialog->depends(), model->index( row, 0, parent ) ); setReadOnly( model->index( row, 0, parent ), dialog->readOnly() ); delete dialog; return true; } void EntryDelegate::setReadOnly(const QModelIndex & index, bool readOnly) { int row = index.row(); QModelIndex parent = index.parent(); QStandardItem* item; const QStandardItemModel* model = qobject_cast<const QStandardItemModel*>( index.model() ); if ( !model ) return; item = model->itemFromIndex( model->index( row, 0, parent ) ); item->setFlags( readOnly ? item->flags() & ~Qt::ItemIsEditable : item->flags() | Qt::ItemIsEditable ); item = model->itemFromIndex( model->index( row, 1, parent ) ); item->setFlags( readOnly ? item->flags() & ~Qt::ItemIsEditable : item->flags() | Qt::ItemIsEditable ); item = model->itemFromIndex( model->index( row, 2, parent ) ); item->setFlags( readOnly ? item->flags() & ~Qt::ItemIsEditable : item->flags() | Qt::ItemIsEditable ); item = model->itemFromIndex( model->index( row, 3, parent ) ); item->setFlags( readOnly ? item->flags() & ~Qt::ItemIsEditable : item->flags() | Qt::ItemIsEditable ); item = model->itemFromIndex( model->index( row, 4, parent ) ); item->setFlags( readOnly ? item->flags() & ~Qt::ItemIsEditable : item->flags() | Qt::ItemIsEditable ); } void EntryDelegate::addConstraint(const QModelIndex & index1, const QModelIndex & index2) { if ( !index1.isValid() || !index2.isValid() ) return; KDGantt::Constraint c( index1, index2 ); constraintModel->addConstraint( c ); }
entrydialog.cpp
/**************************************************************************** ** Copyright (C) 2001-2018 Klaralvdalens Datakonsult AB. All rights reserved. ** ** This file is part of the KD Chart library. ** ** Licensees holding valid commercial KD Chart licenses may use this file in ** accordance with the KD Chart Commercial License Agreement provided with ** the Software. ** ** ** This file may be distributed and/or modified under the terms of the ** GNU General Public License version 2 and version 3 as published by the ** Free Software Foundation and appearing in the file LICENSE.GPL.txt included. ** ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. ** ** Contact info@kdab.com if any conditions of this licensing are not ** clear to you. ** **********************************************************************/ #include "entrydialog.h" #include "ui_entrydialog.h" #include <KDGanttConstraintModel> #include <KDGanttGlobal> #include <QAbstractItemModel> #include <QDateTime> #include <QPersistentModelIndex> Q_DECLARE_METATYPE( QPersistentModelIndex ); EntryDialog::EntryDialog( const QAbstractIte