- 浏览: 719177 次
- 性别:
- 来自: 深圳
文章分类
- 全部博客 (1042)
- 数据结构 (36)
- UML与设计模式 (42)
- c++ (87)
- rust (36)
- Qt (41)
- boost模板元编程 (43)
- Linux (77)
- 汇编 (4)
- 其它 (2)
- 烹饪 (3)
- unix c / socket (73)
- 软件工程 (4)
- shell (52)
- Python (37)
- c++ primer 5th(c++11) (22)
- 数据库/MySQL (27)
- 数据存储 (4)
- lisp (7)
- git (4)
- Utility (3)
- CDN与DNS (54)
- Http (53)
- php (7)
- nginx/lua/openresty (41)
- redis (11)
- TCP/IP (16)
- 互联网 (6)
- kernel (2)
- go (33)
- 区块链 (43)
- 比特股 (13)
- 以太坊 (23)
- 比特币 (23)
- 密码学 (10)
- EOS (53)
- DAG (1)
- docker (1)
- filecoin (7)
- solidity (64)
- ipfs (8)
- 零知识证明 (1)
- openzeppelin (3)
- java (1)
- defi (7)
最新评论
.h
.cpp
main.cpp
#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(); }
发表评论
-
mac无法debug
2019-09-23 17:36 230升级新的lldb,底层脚本用的py3,而qtcreator暂不 ... -
现有项目生成pro文件
2018-07-21 15:32 1226qmake -project -
QtCreator使用arm编译器
2014-04-18 16:19 1186构建工具链各种不和谐,心一狠,直接改最原始的配置文件 1.首 ... -
qtcreator c++11
2013-07-15 22:10 847在.pro里面:QMAKE_CXXFLAGS += -std= ... -
qt webkit
2013-03-28 17:27 769arora是一个很不错的开源的浏览器,我说它不错并不见得它的性 ... -
qt profile
2012-05-29 17:04 5347这篇文章是我从360doc上转的,本来是要把转的url列出来的 ... -
QtConcurrent
2012-05-26 14:48 1970当我们想在一个或多个辅助线程执行的同时做一些后台处理且无须使用 ... -
QThreadPool
2012-05-23 22:24 2161#include<QThreadPool> ... -
用qtcreator但不用qt库
2012-04-17 10:48 1247pro file: QT -= core gui -
qtcreator缺少编码
2012-04-14 13:46 843将plugins/codecs复制到yourcreator/b ... -
QTableView显示tooltip与设置日志
2012-01-11 22:15 3607原文http://www.cppblog.com/biao/a ... -
Qt mysql相关
2011-11-28 23:44 1760sudo apt-get install mysql-serv ... -
Qt自定义事件
2011-08-31 14:48 5264先看下官方的文档:(google translation) Q ... -
QComboBox下拉item变灰
2011-07-21 10:10 1860qobject_cast<QStandardItemMo ... -
Qt国际化
2011-07-21 09:37 9161.先在.pro中加入一行:TRANSLATIONS = xx ... -
Qt读写xml
2011-07-20 10:49 6391test.xml <?xml version=&qu ... -
Qt多线程
2011-07-18 21:08 1417Qt中线程安全的类有QMutex,QMutexLocker,Q ... -
生产者与消费者
2011-07-16 09:42 2#include<iostream> #in ... -
Qt读写二进制文件
2011-07-11 22:11 5630DataStream.h #ifndef DATASTRE ... -
Qt的文本文件读写
2011-07-02 17:00 3203mainwindow.h #ifndef MAINWIND ...
相关推荐
NULL 博文链接:https://qimo601.iteye.com/blog/1536464
然而,简单基础部件的委托可以继承QItemDelegate而不是QAbstractItemDelegate,并使用这些函数的默认实现。 委托编辑器可以通过使用小工具来管理编辑过程或直接处理事件来实现。 使用现有委托 Qt提供的标准视图中...
简单基础部件的委托可以继承QItemDelegate,并使用这些函数的默认实现,委托编辑器可以通过使用小工具来管理编辑过程或直接处理事件来实现。 使用Delegate的原因:Qt中当用到QTreeView和QTableView等用于显示item的...
Qt开发的通讯录小程序。 学习QT时,参照网上的例子完成的小程序,主要是练习之用,不当之处在所难免...2.QSqlTableModel / QDataWidgetMapper / QItemDelegate等类的使用 3.Windows环境下摄像头的使用 4.图像的表示方法
《C++ Qt开发:QItemDelegate自定义代理组件》文章课件
自定义委托类,重写QItemDelegate中的5个函数并用于模型实例.
QT中代理Delegates使用实例代码,可运行,多种编辑方式,有下列框,日期、QSpinBox,界面显示一看就懂!代码无buge,如果对你有帮助请给好评,有问题可以私信联系我
QTableView 中单元格添加控件的实例代码