Skip to content

Commit

Permalink
add qt demo
Browse files Browse the repository at this point in the history
  • Loading branch information
yiminyangguang520 committed Aug 28, 2015
1 parent 26d4133 commit 2cf014a
Show file tree
Hide file tree
Showing 123 changed files with 5,606 additions and 0 deletions.
25 changes: 25 additions & 0 deletions DateDelegateDemo/DateDelegate.pro
Original file line number Diff line number Diff line change
@@ -0,0 1,25 @@
#-------------------------------------------------
#
# Project created by QtCreator 2013-06-04T09:41:42
#
#-------------------------------------------------

QT = core gui

greaterThan(QT_MAJOR_VERSION, 4): QT = widgets

TARGET = DateDelegate
TEMPLATE = app


SOURCES = main.cpp\
datedelegate.cpp \
combodelegate.cpp \
spindelegate.cpp \
proxystyle.cpp

HEADERS = \
datedelegate.h \
combodelegate.h \
spindelegate.h \
proxystyle.h
58 changes: 58 additions & 0 deletions DateDelegateDemo/combodelegate.cpp
Original file line number Diff line number Diff line change
@@ -0,0 1,58 @@
#include "combodelegate.h"
#include <QComboBox>
#include <QCompleter>
#include <QLineEdit>

ComboDelegate::ComboDelegate(QObject *parent)
: QItemDelegate(parent)
{

}

QWidget *ComboDelegate::createEditor(QWidget *parent,const QStyleOptionViewItem &/*option*/,const QModelIndex & index) const
{
QComboBox *editor = new QComboBox(parent);
editor->addItem(QStringLiteral("工人"));
editor->addItem(QStringLiteral("工1"));
editor->addItem(QStringLiteral("工2"));
editor->addItem(QStringLiteral("农民"));
editor->addItem(QStringLiteral("医生"));
editor->addItem(QStringLiteral("律师"));
editor->addItem(QStringLiteral("军人"));
if (index.data().isValid() && "" != index.data().toString())
{
editor->addItem(index.data().toString());
}
editor->setEditable(true);
// 设置自动补全功能
QCompleter *pCompleter = new QCompleter(editor);
pCompleter->setModel(editor->model());
pCompleter->setCaseSensitivity(Qt::CaseInsensitive);
editor->setCompleter(pCompleter);

// editor->lineEdit()->setReadOnly(true);
editor->installEventFilter(const_cast<ComboDelegate*>(this));
return editor;
}

void ComboDelegate::setEditorData(QWidget *editor,const QModelIndex &index) const
{
QString str = index.model()->data(index).toString();

QComboBox *box = static_cast<QComboBox*>(editor);
box->lineEdit()->setText(str);
// int currentIndex = box->findText(str);
// box->setCurrentIndex(currentIndex);
}

void ComboDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{
QComboBox *box = static_cast<QComboBox*>(editor);
QString str = box->currentText();
model->setData(index,str);
}

void ComboDelegate::updateEditorGeometry(QWidget *editor,const QStyleOptionViewItem &option, const QModelIndex &/*index*/) const
{
editor->setGeometry(option.rect);
}
18 changes: 18 additions & 0 deletions DateDelegateDemo/combodelegate.h
Original file line number Diff line number Diff line change
@@ -0,0 1,18 @@
#ifndef COMBODELEGATE_H
#define COMBODELEGATE_H

#include <QItemDelegate>

class ComboDelegate : public QItemDelegate
{
Q_OBJECT
public:
ComboDelegate(QObject *parent = 0);

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;
void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const;
};

#endif // COMBODELEGATE_H
38 changes: 38 additions & 0 deletions DateDelegateDemo/datedelegate.cpp
Original file line number Diff line number Diff line change
@@ -0,0 1,38 @@
#include "datedelegate.h"
#include <QDateTimeEdit>

DateDelegate::DateDelegate(QObject *parent) :
QItemDelegate(parent)
{
}

QWidget *DateDelegate::createEditor(QWidget *parent,const QStyleOptionViewItem &/*option*/,const QModelIndex &/*index*/) const
{
QDateTimeEdit *editor = new QDateTimeEdit(parent);
editor->setDisplayFormat("yyyy-MM-dd");
editor->setCalendarPopup(true);
editor->installEventFilter(const_cast<DateDelegate*>(this));
return editor;
}

void DateDelegate::setEditorData(QWidget *editor,const QModelIndex &index) const
{
QString dateStr = index.model()->data(index).toString();
QDate date = QDate::fromString(dateStr, Qt::ISODate);

QDateTimeEdit *edit = static_cast<QDateTimeEdit*>(editor);
edit->setDate(date);
}

void DateDelegate::setModelData(QWidget *editor,QAbstractItemModel *model, const QModelIndex &index) const
{
QDateTimeEdit *edit = static_cast<QDateTimeEdit*>(editor);
QDate date = edit->date();
model->setData(index, QVariant(date.toString(Qt::ISODate)));
}

void DateDelegate::updateEditorGeometry(QWidget *editor,const QStyleOptionViewItem &option,const QModelIndex &index) const
{
Q_UNUSED(index);
editor->setGeometry(option.rect);
}
17 changes: 17 additions & 0 deletions DateDelegateDemo/datedelegate.h
Original file line number Diff line number Diff line change
@@ -0,0 1,17 @@
#ifndef DATEDELEGATE_H
#define DATEDELEGATE_H

#include <QItemDelegate>

class DateDelegate : public QItemDelegate
{
Q_OBJECT
public:
DateDelegate(QObject *parent = 0);
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;
void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const;
};

#endif // DATEDELEGATE_H
63 changes: 63 additions & 0 deletions DateDelegateDemo/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 1,63 @@
#include <QApplication>
#include <QStandardItemModel>
#include <QTableView>
#include <QFile>
#include <QTextStream>
#include "datedelegate.h"
#include "combodelegate.h"
#include "spindelegate.h"
#include "proxystyle.h"

int main(int argc, char *argv[])
{
QApplication a(argc, argv);

QStandardItemModel model(4,4);
QTableView tableView;
tableView.setModel(&model);
DateDelegate dateDelegate;
tableView.setItemDelegateForColumn(1,&dateDelegate);
ComboDelegate comboDelegate;
tableView.setItemDelegateForColumn(2,&comboDelegate);
SpinDelegate spinDelegate;
tableView.setItemDelegateForColumn(3,&spinDelegate);

tableView.setStyle(new CustomProxyStyle);

model.setHeaderData(0, Qt::Horizontal, QStringLiteral("ÐÕÃû"));
model.setHeaderData(1, Qt::Horizontal, QStringLiteral("³öÉúÈÕÆÚ"));
model.setHeaderData(2, Qt::Horizontal, QStringLiteral("Ö°Òµ"));
model.setHeaderData(3, Qt::Horizontal, QStringLiteral("¹¤Áä"));

QFile file("test.txt");
if(file.open(QFile::ReadOnly|QFile::Text))
{
QTextStream stream(&file);
QString line;

model.removeRows(0, model.rowCount(QModelIndex()), QModelIndex());
int row = 0;
do{
line = stream.readLine();

if(!line.isEmpty())
{
model.insertRows(row, 1, QModelIndex());
QStringList pieces = line.split(",", QString::SkipEmptyParts);
model.setData(model.index(row, 0, QModelIndex()), pieces.value(0));
model.setData(model.index(row, 1, QModelIndex()), pieces.value(1));
model.setData(model.index(row, 2, QModelIndex()), pieces.value(2));
model.setData(model.index(row, 3, QModelIndex()), pieces.value(3));
row ;
}
}while(!line.isEmpty());

file.close();
}

tableView.setWindowTitle(QStringLiteral("Delegate"));
tableView.show();
tableView.resize(900, 600);

return a.exec();
}
25 changes: 25 additions & 0 deletions DateDelegateDemo/proxystyle.cpp
Original file line number Diff line number Diff line change
@@ -0,0 1,25 @@
#include "proxystyle.h"

CustomProxyStyle::CustomProxyStyle()
{

}

CustomProxyStyle::~CustomProxyStyle()
{

}

void CustomProxyStyle::drawPrimitive(QStyle::PrimitiveElement element, const QStyleOption *option,
QPainter *painter, const QWidget *widget) const
{
if (PE_FrameFocusRect == element)
{
/// do not draw focus rectangle
}
else
{
QProxyStyle::drawPrimitive(element, option,painter, widget);
}
}

22 changes: 22 additions & 0 deletions DateDelegateDemo/proxystyle.h
Original file line number Diff line number Diff line change
@@ -0,0 1,22 @@
///////////////////////////////////////////////////////////
// È¥µô¾ØÐεãÕóÐé¿ò
///////////////////////////////////////////////////////////

#ifndef PROXYSTYLE_H
#define PROXYSTYLE_H

#include <QProxyStyle>

class CustomProxyStyle : public QProxyStyle
{
Q_OBJECT

public:
CustomProxyStyle();
~CustomProxyStyle();

protected:
virtual void drawPrimitive(PrimitiveElement element, const QStyleOption *option,
QPainter *painter, const QWidget *widget = 0) const;
};
#endif // PROXYSTYLE_H
38 changes: 38 additions & 0 deletions DateDelegateDemo/spindelegate.cpp
Original file line number Diff line number Diff line change
@@ -0,0 1,38 @@
#include "spindelegate.h"
#include <QSpinBox>

SpinDelegate::SpinDelegate(QObject *parent):
QItemDelegate(parent)
{
}

QWidget *SpinDelegate::createEditor(QWidget *parent,const QStyleOptionViewItem &/*option*/,const QModelIndex &/*index*/) const
{
QSpinBox *editor = new QSpinBox(parent);
editor->setRange(0,10000);
editor->installEventFilter(const_cast<SpinDelegate*>(this));

return editor;
}

void SpinDelegate::setEditorData(QWidget *editor,const QModelIndex &index) const
{
int value =index.model()->data(index).toInt();
QSpinBox *box = static_cast<QSpinBox*>(editor);
box->setValue(value);
}

void SpinDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,const QModelIndex &index) const
{
QSpinBox *box = static_cast<QSpinBox*>(editor);
int value = box->value();

model->setData(index,value);
}

void SpinDelegate::updateEditorGeometry(QWidget *editor,const QStyleOptionViewItem &option, const QModelIndex &/*index*/) const
{
editor->setGeometry(option.rect);
}


18 changes: 18 additions & 0 deletions DateDelegateDemo/spindelegate.h
Original file line number Diff line number Diff line change
@@ -0,0 1,18 @@
#ifndef SPINDELEGATE_H
#define SPINDELEGATE_H

#include <QItemDelegate>

class SpinDelegate : public QItemDelegate
{
Q_OBJECT
public:
SpinDelegate(QObject *parent = 0);

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;
void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const;
};

#endif // SPINDELEGATE_H
Loading

0 comments on commit 2cf014a

Please sign in to comment.