`

QItemDelegate

    博客分类:
  • Qt
 
阅读更多
.h
#ifndef DIALOG_H
#define DIALOG_H

#include <QDialog>
#include <QItemDelegate>
class QTableWidget;
class QItemDelegate;
class QStyleOptionViewItem;
class QModelIndex;
class QAbstractItemModel;

class Track{
public:
    explicit Track(const QString& title="",int duration=0){

    }

    QString title;
    int duration;
};

class TrackEditor:public QDialog{
    Q_OBJECT
public:
    explicit TrackEditor(QList<Track> *tracks,QWidget* parent=NULL);
private:
    QList<Track> *tracks;
    QTableWidget* tableWidget;
};

class TrackDelegate:public QItemDelegate{
    Q_OBJECT
public:
    TrackDelegate(int durationColumn,QObject *parent=0);
    void paint(QPainter *painter,const QStyleOptionViewItem &option,
               const QModelIndex& index)const;
    QWidget *createEditor(QWidget* parent,
                          const QStyleOptionViewItem &option,
                          const QModelIndex &index)const;
    void setEditorData(QWidget* editor,const QModelIndex &index)const;
    void setModelData(QWidget* editor,QAbstractItemModel *model,
                      const QModelIndex &index)const;
private slots:
    void commitAndCloseEditor();
private:
    int duration;
};

#endif // DIALOG_H



.cpp
#include "dialog.h"
#include <QTableWidget>
#include <QItemDelegate>
#include <QStyleOptionViewItem>
#include <QModelIndex>
#include <QTimeEdit>
#include <QVBoxLayout>

TrackEditor::TrackEditor(QList<Track> *tracks, QWidget *parent)
    :QDialog(parent)
{
    this->tracks = tracks;
    tableWidget = new QTableWidget(tracks->count(),2);
    tableWidget->setItemDelegate(new TrackDelegate(1));
    tableWidget->setHorizontalHeaderLabels(
                QStringList()<<tr("Track")<<tr("Duration"));

    for(int row=0;row<tracks->count();++row){
        Track track = tracks->at(row);

        QTableWidgetItem *item0 = new QTableWidgetItem(track.title);
        tableWidget->setItem(row,0,item0);
        QTableWidgetItem *item1 = new QTableWidgetItem(
                    QString::number(track.duration));
        item1->setTextAlignment(Qt::AlignRight);
        tableWidget->setItem(row,1,item1);
    }
    QVBoxLayout *layout = new QVBoxLayout;
    layout->addWidget(tableWidget);
    setLayout(layout);
}

TrackDelegate::TrackDelegate(int durationColumn, QObject *parent)
    :QItemDelegate(parent)
{
    this->duration = durationColumn;
}

void TrackDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
                          const QModelIndex &index) const
{
    if(index.column()==duration){
        int secs = index.model()->data(index,Qt::DisplayRole).toInt();
        QString text = QString("%1:%2").arg(secs/60,2,10,QChar('0'))
                                       .arg(secs%60,20,10,QChar('0'));
        QStyleOptionViewItem myOption = option;
        myOption.displayAlignment = Qt::AlignRight | Qt::AlignVCenter;
        drawDisplay(painter,myOption,myOption.rect,text);
        drawFocus(painter,myOption,myOption.rect);
    }else{
        QItemDelegate::paint(painter,option,index);
    }
}

QWidget *TrackDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option,
                                     const QModelIndex &index) const
{
    if(index.column()==duration){
        QTimeEdit *timeEdit = new QTimeEdit(parent);
        timeEdit->setDisplayFormat("mm:ss");
        connect(timeEdit,SIGNAL(editingFinished()),this,SLOT(commitAndCloseEditor()));
        return timeEdit;
    }else{
        return QItemDelegate::createEditor(parent,option,index);
    }
}

void TrackDelegate::commitAndCloseEditor()
{
    QTimeEdit *edit = qobject_cast<QTimeEdit *>(sender());
    emit commitData(edit);
    emit closeEditor(edit);
}

void TrackDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
    if(index.column()==duration){
        int secs = index.model()->data(index,Qt::DisplayRole).toInt();
        QTimeEdit *timeEdit = qobject_cast<QTimeEdit *>(editor);
        timeEdit->setTime(QTime(0,secs/60,secs%60));
    }else{
        QItemDelegate::setEditorData(editor,index);
    }
}


void TrackDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
                                 const QModelIndex &index) const
{
    if(index.column()==duration){
        QTimeEdit *timeEdit = qobject_cast<QTimeEdit *>(editor);
        QTime time = timeEdit->time();
        int secs = (time.minute()*60)+time.second();
        model->setData(index,secs);
    }else{
        QItemDelegate::setModelData(editor,model,index);
    }
}



main.cpp
#include <QtGui/QApplication>
#include "dialog.h"

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QList<Track> list;
    for(int i=0;i<10;i++){
        list.push_back(Track(QString("title%1").arg(i),10));
    }
    TrackEditor w(&list);
    w.show();
    return a.exec();
}
分享到:
评论

相关推荐

    基于QItemDelegate的例子2 trackeEditorDelegate

    NULL 博文链接:https://qimo601.iteye.com/blog/1536464

    tableView.zip

    然而,简单基础部件的委托可以继承QItemDelegate而不是QAbstractItemDelegate,并使用这些函数的默认实现。 委托编辑器可以通过使用小工具来管理编辑过程或直接处理事件来实现。 使用现有委托 Qt提供的标准视图中...

    CustomDelegate.rar

    简单基础部件的委托可以继承QItemDelegate,并使用这些函数的默认实现,委托编辑器可以通过使用小工具来管理编辑过程或直接处理事件来实现。 使用Delegate的原因:Qt中当用到QTreeView和QTableView等用于显示item的...

    Qt开发的通讯录小程序。

    Qt开发的通讯录小程序。 学习QT时,参照网上的例子完成的小程序,主要是练习之用,不当之处在所难免...2.QSqlTableModel / QDataWidgetMapper / QItemDelegate等类的使用 3.Windows环境下摄像头的使用 4.图像的表示方法

    TableViewDelegate.zip

    《C++ Qt开发:QItemDelegate自定义代理组件》文章课件

    自定义委托

    自定义委托类,重写QItemDelegate中的5个函数并用于模型实例.

    Delegates.7z

    QT中代理Delegates使用实例代码,可运行,多种编辑方式,有下列框,日期、QSpinBox,界面显示一看就懂!代码无buge,如果对你有帮助请给好评,有问题可以私信联系我

    QTableView 中单元格添加控件的实例代码

    QTableView 中单元格添加控件的实例代码

Global site tag (gtag.js) - Google Analytics