日期控件返回为空只要按一下Delete键就可以将DateTimePicker的值设置为空。根据查询相关信息QDatetimeEdit是常用的日期编辑控件,官方的该控件并不能显示为空值,近期测试那边新提需求希望日期编辑控件默认为空值。
QDate, QDateTime, QTime; QDate应该是你需要的。QDate有重载> < = >= <= != *** 作符。不过好像没有+ - *** 作符。你可以将目前日期用addDays函数加5天,再最个比较日期是否小于等于计算出的时间。
QDate xCurDate = QDate::currentDate();
QDate xFutureDate = xCurDateaddDays(5);
QList<QDate> v;
for(int i = 0; i < vsize(); i++){
QDate xDate = v[i];
if(xDate <= xFutureDate){
qDebug() << "找到:" << xDate;
}
}
给你列举一下
QT中常用的类
QApplication 应用程序类 管理图形用户界面应用程序的控制流和主要设置
QLabel
标签类 提供文本或者图像的显示
QPushButton
按钮类 提供了命令按钮 按钮的一种
QButtonGroup 按钮组合类 按钮组
相关按钮的组合
QGroupBox 群组类 一个有标题的组合框
QDateTimeEdit
日期时间编辑框类
QLineEdit 行编辑框类 单行文本编辑器
QTextEdit
文本编辑框类 单页面多信息编辑器对象
QComboBox 组合框类
QProgressBar 进度条类
QLCDNumber 数字显示框类
QScrollBar 滚动条类
QSpinBox 微调框类
QSlider 滑动条类
QIconView 图标视图类
QListView 列表视图类
QListBox 列表框类
QTable 表格类
QValidator 有效性检查类
QImage 图像类
QMainWindow 主窗口类
QPopupMenu d出性菜单类
QMenuBar 菜单栏类
QToolButton 工具按钮类
QToolTip 提示类
QWhatsThis 这是什么类
QAction 动作类
QHBoxLayout 水平布局类
QVBoxLayout 垂直布局类
QGridLayout
表格布局类
QT对话框类
QMessageBox 消息对话框类
QProgressDialog 进度条对话框类
QWizard
向导对话框类
QFileDialog 文件对话框类
QColorDialog 颜色对话框类
QFontDialog 字体对话框类
QPrintDialog 打印对话框类
QFilefile("texttxt");if(!fileopen(QIODevice::ReadOnly)){qDebug("cannotopen!");return;}else{QTextStreamin(&file);QStringtext=inreadAll();ui->textEdit->setText(text);}这只是个简单的测试程序,要是实际使用还要考虑很多情况!
委托类和变量
DateDelegate m_dateDelegate;
ComboDelegate m_comboDelegate;
SpinDelegate m_spinDelegate;
CheckBoxDelegate m_CheckBoxDelegate;
//ttableview添加委托
m_modle = new QStandardItemModel(this);
setModel(m_modle);
m_modle->setColumnCount(6);
m_modle->setHeaderData(0,Qt::Horizontal,"Name");//
m_modle->setHeaderData(1,Qt::Horizontal,"Birthday");
m_modle->setHeaderData(2,Qt::Horizontal,"Job");
m_modle->setHeaderData(3,Qt::Horizontal,"Income");
m_modle->setHeaderData(4,Qt::Horizontal,"yes1");
m_modle->setHeaderData(5,Qt::Horizontal,"yes2");
this->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
setItemDelegateForColumn(1, &m_dateDelegate);
setItemDelegateForColumn(2, &m_comboDelegate);
setItemDelegateForColumn(3, &m_spinDelegate);
setItemDelegateForColumn(4, &m_CheckBoxDelegate);//双击或选中才能显示,不使用
m_modle->setItem(0,0, new QStandardItem("Tom"));
m_modle->setItem(0,1, new QStandardItem("1977-01-05"));
m_modle->setItem(0,2, new QStandardItem("工人"));
m_modle->setItem(0,3, new QStandardItem("1500"));
//m_modle->indexFromItem()
setIndexWidget(m_modle->index(0, 5), new QCheckBox(this));
//类原形
#ifndef DATEDELEGATE_H
#define DATEDELEGATE_H
#include
#include
class DateDelegate : public QItemDelegate
{
Q_OBJECT
public:
DateDelegate(QObject parent = 0) : QItemDelegate(parent) {}
QWidget 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(this));
return editor;
}
void setEditorData(QWidget editor, const QModelIndex &index) const
{
QString dateStr= indexmodel()->data(index)toString();
QDate date = QDate::fromString(dateStr,Qt::ISODate);
QDateTimeEdit edit=static_cast(editor);
edit->setDate(date);
}
void setModelData(QWidget editor, QAbstractItemModel model, const QModelIndex &index) const
{
QDateTimeEdit edit=static_cast(editor);
QDate date = edit->date();
model->setData(index,QVariant(datetoString(Qt::ISODate)));
}
void updateEditorGeometry(QWidget editor, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
editor->setGeometry(optionrect);
}
};
#endif // DATEDELEGATE_H
#ifndef COMBODELEGATE_H
#define COMBODELEGATE_H
#include <QComboBox>
#include <QItemDelegate>
class ComboDelegate : public QItemDelegate
{
Q_OBJECT
public :
ComboDelegate(QObject parent = 0): QItemDelegate(parent) { }
QWidget createEditor(QWidget parent, const QStyleOptionViewItem &option,
const QModelIndex &index) const
{
QStringList list;
list << "工人" << "农民" << "军人" << "律师";
QComboBox editor = new QComboBox(parent);
editor->addItems(list);
editor->installEventFilter(const_cast(this));
return editor;
}
void setEditorData(QWidget editor, const QModelIndex &index) const
{
QString dateStr = indexmodel()->data(index)toString();
QComboBox box = static_cast(editor);
int i = box->findText(dateStr);
box->setCurrentIndex(i);
}
void setModelData(QWidget editor, QAbstractItemModel model,
const QModelIndex &index) const
{
QComboBox box = static_cast(editor);
QString str = box->currentText();
model->setData(index,str);
}
void updateEditorGeometry(QWidget editor,
const QStyleOptionViewItem &option, const QModelIndex &index) const
{
editor->setGeometry(optionrect);
}
};
#endif // COMBODELEGATE_H
#ifndef SPINDELEGATE_H
#define SPINDELEGATE_H
#include
#include
class SpinDelegate : public QItemDelegate
{
Q_OBJECT
public:
SpinDelegate(QObject parent = 0) : QItemDelegate(parent){}
QWidget createEditor(QWidget parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
QSpinBox editor = new QSpinBox(parent);
editor->setRange(0,10000);
editor->installEventFilter(const_cast(this));
return editor;
}
void setEditorData(QWidget editor, const QModelIndex &index) const
{
int value =indexmodel()->data(index)toInt();
QSpinBox box = static_cast(editor);
box->setValue(value);
}
void setModelData(QWidget editor, QAbstractItemModel model, const QModelIndex &index) const
{
QSpinBox box = static_cast(editor);
int value = box->value();
model->setData(index,value);
}
void updateEditorGeometry(QWidget editor, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
editor->setGeometry(optionrect);
}
};
#endif // SPINDELEGATE_H
#ifndef CHECKBOXDELEGATE_H
#define CHECKBOXDELEGATE_H
#include
#include
class CheckBoxDelegate : public QItemDelegate
{
Q_OBJECT
public:
CheckBoxDelegate(QObject parent = 0) : QItemDelegate(parent){}
QWidget createEditor(QWidget parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
QCheckBox editor = new QCheckBox(parent);
editor->installEventFilter(const_cast(this));
return editor;//双击或选中才能显示
}
void setEditorData(QWidget editor, const QModelIndex &index) const
{
bool value =indexmodel()->data(index)toBool();
QCheckBox box = static_cast(editor);
box->setChecked(value);
}
void setModelData(QWidget editor, QAbstractItemModel model, const QModelIndex &index) const
{
QCheckBox box = static_cast(editor);
bool value = box->isChecked();
model->setData(index,value);
}
void updateEditorGeometry(QWidget editor, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
editor->setGeometry(optionrect);
}
};
#endif // CHECKBOXDELEGATE_H
以上就是关于日期控件怎么返回为空全部的内容,包括:日期控件怎么返回为空、关于QT的问题、初学者,想问下Qt中什么是类,部件,组件,以及他们之间的关系等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)