GUI界面
限制输入的内容
QValidator *validator = new QIntValidator (100 , 999 , this );
ui->lineEdit->setValidator (validator);
显示格式控制
ui->textEdit->setWordWrapMode (QTextOption::WrapAnywhere);
ui->tableWidget->setTextElideMode (Qt::ElideMode);
Qt序列化
通过全局流操作运算符重载 实现,可以序列化到文件等设备中,也可以序列化到QByteArray
中。
class CmdInfo : public BaseInfo {
public :
friend QDataStream& operator >>(QDataStream&, CmdInfo&);
friend QDataStream& operator <<(QDataStream&, const CmdInfo&);
private :
QString command_;
CmdType type_;
};
QDataStream& operator >>(QDataStream &in, CmdInfo &data)
{
BaseInfo &base = data;
qint32 tmpInt;
in >> base >> data.command_ >> tmpInt;
data.setType ((CmdType)tmpInt);
return in;
}
QDataStream& operator <<(QDataStream &out, const CmdInfo &data)
{
const BaseInfo &base = data;
out << base << data.command_ << (qint32)data.type_;
return out;
}
序列化到文件中
QFile file ("object.dat" ) ;
file.open (QFile::ReadOnly);
QDataStream in (&file) ;
in >> infoObj;
file.close ();
QFile file (datFilename(filename)) ;
file.open (QFile::Truncate | QFile::WriteOnly);
QDataStream out (&file) ;
out << infoObj;
file.flush ();
file.close ();
序列化到QByteArray中
QByteArray byteArray;
QBuffer buffer (&byteArray) ;
buffer.open (QIODevice::WriteOnly);
QDataStream out (&buffer) ;
out << QApplication::palette ();
QDateTime
也重载了关于QDateTime
和QDebug
的全局流操作符
class QDateTime {
friend Q_CORE_EXPORT QDataStream &operator <<(QDataStream &, const QDateTime &);
friend Q_CORE_EXPORT QDataStream &operator >>(QDataStream &, QDateTime &);
friend Q_CORE_EXPORT QDebug operator <<(QDebug, const QDateTime &);
};
QByteArray的Copy-On-Write
QByteArray can be used to store both raw bytes (including '\0's) and traditional 8-bit '\0'-terminated strings. Using QByteArray is much more convenient than using const char *
. Behind the scenes, it always ensures that the data is followed by a '\0' terminator, and uses implicit sharing (copy-on-write) to reduce memory usage and avoid needless copying of data.
#include <QByteArray>
#include <QDebug>
int main ()
{
char buf[] = {'1' , '2' , 0 , '3' };
QByteArray ba1 = QByteArray::fromRawData (buf, 4 );
QByteArray ba2 = QByteArray (buf, 4 );
qDebug () << ba2.size ();
printf ("buf=%p, ba1=%p, ba2=%p\n" , buf, ba1.constData (), ba2.constData ());
ba1.data ();
printf ("buf=%p, ba1=%p, ba2=%p\n" , buf, ba1.constData (), ba2.constData ());
return 0 ;
}
底层原理
inline char *QByteArray::data ()
{ detach (); return d->data (); }
inline const char *QByteArray::data () const
{ return d->data (); }
inline const char *QByteArray::constData () const
{ return d->data (); }
inline void QByteArray::detach ()
{ if (d->ref.isShared () || (d->offset != sizeof (QByteArrayData))) reallocData (uint (d->size) + 1u , d->detachFlags ()); }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Ollama——大语言模型本地部署的极速利器
· 使用C#创建一个MCP客户端
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现