Qt浅谈之四十四动态显示日志(QGraphicsItem)

2024-03-11 10:18

本文主要是介绍Qt浅谈之四十四动态显示日志(QGraphicsItem),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

一、简介

 

        在QGraphicsItem中使用QGraphicsItemAnimation和QTimeLine来实现动画的显示。其中的数据都是测试数据,仅为显示动画效果。

 

二、详解

1、部分代码

(1)LogMessagesItem.h

#ifndef _LOGMESSAGEITEM_H_
#define _LOGMESSAGEITEM_H_
#include <QtGui>
#include <QtCore>
#include "clusterpagenumber.h"
#include "calendarselected.h"class LogContentItem;
class LogMessagesItem : public QObject, public QGraphicsItem
{Q_OBJECT
public:explicit LogMessagesItem(QGraphicsItem *parent = 0);QRectF boundingRect() const;void setSize(const int width, const int height);protected:void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);bool eventFilter(QObject *obj, QEvent *event);private:void getLogContents();void showLogs();void hideLogItem();private:int _width;int _height;int totalLogs;int totalPageNumber;int currentPageNumber;ClusterPageNumber *pageNumber;CalendarSelected *calendar;QList<LogContentItem *>logItem;QGraphicsRectItem *rectItem;bool leftToRight;private slots:void slotCalendarFinish();void slotLogPageChange(int index);
};class LogContentItem : public QGraphicsItem
{
public:explicit LogContentItem(QGraphicsItem *parent = 0);~LogContentItem();QRectF boundingRect() const;void setData(const QList<QString> &data);protected:void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
private:QGraphicsTextItem *timeText;QGraphicsTextItem *typeText;QGraphicsTextItem *stateText;QGraphicsTextItem *describeText;
};
#endif

(2)LogMessagesItem.cpp

#include "LogMessagesItem.h"
#define MAXLOGSRPERPAGE 4LogMessagesItem::LogMessagesItem(QGraphicsItem *parent): QGraphicsItem(parent), totalLogs(0), totalPageNumber(1), currentPageNumber(1), leftToRight(true)
{_width = 633;_height = 437;logItem.clear();setFiltersChildEvents(true);calendar = new CalendarSelected;QGraphicsProxyWidget *calendarProxy = new QGraphicsProxyWidget(this);calendarProxy->setWidget(calendar);calendarProxy->setPos(10, 10);calendarProxy->show();calendarProxy->setZValue(1);connect(calendar, SIGNAL(finish()), this, SLOT(slotCalendarFinish()));pageNumber = new ClusterPageNumber;//pageNumber->setBackgroundColor(QColor("#FF0000"));pageNumber->resize(248, 18);pageNumber->setTitle(tr("合计日志条数"), 0);pageNumber->setCurrentAndTotalPage(1, 10);QGraphicsProxyWidget *pageNumberProxy = new QGraphicsProxyWidget(this);pageNumberProxy->setWidget(pageNumber);pageNumberProxy->setPos(633 - 248 -10, 437 - 35);pageNumberProxy->show();connect(pageNumber, SIGNAL(currentPageChanged(int)), this, SLOT(slotLogPageChange(int)));QGraphicsTextItem *title = new QGraphicsTextItem(this);title->setDefaultTextColor(QColor("#666666"));title->setFont(QFont("arial", 10, QFont::Normal));title->setPlainText(tr("按状态:"));title->setPos(460, 10);QComboBox *logListBox = new QComboBox;logListBox->resize(100, 23);logListBox->setStyleSheet("QComboBox{border:1px solid #d7d7d7; border-radius: 3px; padding: 1px 18px 1px 3px;} ""QComboBox:editable{ background: white; }""QComboBox:!editable{ background: #fbfbfb; color:#666666}""QComboBox::drop-down{ subcontrol-origin: padding; subcontrol-position: top right; width: 22px; border-left-width: 1px; border-left-color: #c9c9c9; border-left-style: solid; /* just a single line */ border-top-right-radius: 3px; /* same radius as the QComboBox */ border-bottom-right-radius: 3px; }""QComboBox::down-arrow { image: url(:/resources/images/input_drop_down.png); }""QComboBox::down-arrow:on { /* shift the arrow when popup is open */ top: 1px; left: 1px;}""QComboBox QAbstractItemView::item{max-width: 30px;min-height: 20px}");QListView *listView = new QListView;listView->setStyleSheet("QListView{font-size: 11px}"" QListView::item:!selected{color: #19649f}""QListView::item:selected:active{background-color: #1275c3}""QListView::item:selected{color: white}");logListBox->setView(listView);logListBox->addItem(tr("所有日志"));logListBox->addItem(tr("错误日志"));logListBox->addItem(tr("警告日志"));QGraphicsProxyWidget *listBoxrProxy = new QGraphicsProxyWidget(this);listBoxrProxy->setWidget(logListBox);listBoxrProxy->setPos(522, 12);listBoxrProxy->setZValue(1);listBoxrProxy->show();rectItem = new QGraphicsRectItem(QRectF(0, 0, 610, 85*4), this);rectItem->setPos(12, 45);rectItem->setPen(Qt::NoPen);rectItem->setFlag(QGraphicsItem::ItemClipsChildrenToShape,true);getLogContents();
}QRectF LogMessagesItem::boundingRect() const
{return QRectF(0,0, _width, _height);
}void LogMessagesItem::setSize(const int width, const int height)
{_width = width;_height = height;
}void LogMessagesItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
}bool LogMessagesItem::eventFilter(QObject *obj, QEvent *event)
{if (obj != calendar && event->type() == QEvent::MouseButtonPress) {this->clearFocus();calendar->setHideQDialog();return true;}if (event->type() == QEvent::Hide) {calendar->setHideQDialog();}if (event->type() == QEvent::Show) {}return QObject::eventFilter(obj, event);
}void LogMessagesItem::getLogContents()
{for (int i =0; i < 22; i++) {QList<QString> data;data.append("2015-02-13 12:08:57");data.append("新建系统");data.append("系统创建完毕");data.append(tr("成功创建系统(%1)").arg(i + 1));LogContentItem *contentItem = new LogContentItem(rectItem);contentItem->setData(data);contentItem->hide();logItem.append(contentItem);}
}void LogMessagesItem::showLogs()
{totalLogs = logItem.count();//totalLogs = 22;totalPageNumber = 6;//currentPageNumber = 1;hideLogItem();pageNumber->setTitle(tr("合计日志条数"), 22);pageNumber->setCurrentAndTotalPage(currentPageNumber, 6);LogContentItem *item = NULL;int index = (currentPageNumber - 1)*MAXLOGSRPERPAGE;for(int number = 0; index < totalLogs && index < currentPageNumber*MAXLOGSRPERPAGE; index++, number++) {item = logItem.at(index);item->setPos(0, number*85);item->show();QGraphicsItemAnimation *animation = new QGraphicsItemAnimation(this);animation->setItem(item);QTimeLine *timeLine = new QTimeLine(400);animation->setTimeLine(timeLine);if (leftToRight == true) {animation->setPosAt(0, QPointF( -608, number*85));animation->setPosAt(1, QPointF(0, number*85));}else {animation->setPosAt(0, QPointF( 608, number*85));animation->setPosAt(1, QPointF(0, number*85));}timeLine->start();}
}void LogMessagesItem::hideLogItem()
{for (int index = 0; index < logItem.count(); index++) {if (logItem.at(index)) {logItem.at(index)->hide();}}
}void LogMessagesItem::slotCalendarFinish()
{showLogs();
}void LogMessagesItem::slotLogPageChange(int index)
{if (currentPageNumber < index) {leftToRight = true;}else {leftToRight = false;}currentPageNumber = index;showLogs();
}/*****************************LogContentItem********************************/
LogContentItem::LogContentItem(QGraphicsItem *parent): QGraphicsItem(parent)
{timeText = new QGraphicsTextItem(this);timeText->setDefaultTextColor(QColor("#999999"));timeText->setFont(QFont("arial", 10, QFont::Normal));timeText->setPos(boundingRect().right() - 125, boundingRect().top());typeText = new QGraphicsTextItem(this);typeText->setDefaultTextColor(QColor("#666666"));typeText->setFont(QFont("arial", 10, QFont::Normal));typeText->setPos(boundingRect().left() + 15, boundingRect().top() + 10);stateText = new QGraphicsTextItem(this);stateText->setDefaultTextColor(QColor("#666666"));stateText->setFont(QFont("arial", 10, QFont::Normal));stateText->setPos(boundingRect().left() + 15, boundingRect().top() + 30);describeText = new QGraphicsTextItem(this);describeText->setDefaultTextColor(QColor("#666666"));describeText->setFont(QFont("arial", 10, QFont::Normal));describeText->setPos(boundingRect().left() + 15, boundingRect().top() + 50);
}LogContentItem::~LogContentItem()
{
}QRectF LogContentItem::boundingRect() const
{return QRectF(0, 0, 608, 80);
}void LogContentItem::setData(const QList<QString> &data)
{timeText->setPlainText(data.at(0));typeText->setPlainText(QObject::tr("类型:%1").arg(data.at(1)));stateText->setPlainText(QObject::tr("状态:%1").arg(data.at(2)));describeText->setPlainText(QObject::tr("描述:%1").arg(data.at(3)));
}void LogContentItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{painter->setPen(QPen(QColor("#BBBBBB")));painter->setBrush(QColor("#FFFFFF"));painter->drawRect(0, 0, 608, 80);
}

(3)LogMessagesView.h

#ifndef LOGMESSAGESVIEW_H
#define LOGMESSAGESVIEW_H#include <QGraphicsView>
#include <QTextCodec>
#include <QtGui>
#include <QtCore>
#include <QDebug>class LogMessagesView : public QGraphicsView
{Q_OBJECTpublic:LogMessagesView(QWidget *parent = 0);~LogMessagesView();protected:void mousePressEvent ( QMouseEvent * event);void mouseMoveEvent(QMouseEvent *event);void mouseReleaseEvent(QMouseEvent *event);private:QPoint dragPosition;bool bPressFlag;
};#endif // LOGMESSAGESVIEW_H

(4)LogMessagesView.cpp

#include "LogMessagesView.h"
#include "LogMessagesItem.h"LogMessagesView::LogMessagesView(QWidget *parent): QGraphicsView(parent), bPressFlag(false)
{QTextCodec *codec = QTextCodec::codecForName("System");QTextCodec::setCodecForLocale(codec);QTextCodec::setCodecForCStrings(codec);QTextCodec::setCodecForTr(codec);int width = 633;int height = 437;setWindowFlags(Qt::Widget |Qt::FramelessWindowHint);//setStyleSheet("background:transparent;border:0px");this->setBackgroundBrush(QBrush(QColor("#EEF7FD")));QGraphicsScene *scene = new QGraphicsScene(this);scene->setSceneRect(-width/2,-height/2,width,height);setSceneRect(2, 2, width - 4, height - 4);setScene(scene);setCacheMode(QGraphicsView::CacheBackground);setViewportUpdateMode(QGraphicsView::BoundingRectViewportUpdate);LogMessagesItem *item = new LogMessagesItem;this->installEventFilter(item);scene->addItem(item);
}LogMessagesView::~LogMessagesView()
{}void LogMessagesView::mousePressEvent ( QMouseEvent * event)
{bPressFlag = true;dragPosition = event->pos();QGraphicsView::mousePressEvent(event);
}void LogMessagesView::mouseMoveEvent(QMouseEvent *event)
{if (bPressFlag) {QPoint relaPos(QCursor::pos() - dragPosition);move(relaPos);}QGraphicsView::mouseMoveEvent(event);
}void LogMessagesView::mouseReleaseEvent(QMouseEvent *event)
{bPressFlag = false;QGraphicsView::mouseReleaseEvent(event);
}

三、总结

(1)因涉及一些项目内容,此处暂不提供源码,请谅解。读者需根据上述思路去完成自己的代码。
(2)可执行程序已上传到CSDN(centos6.6下运行):http://download.csdn.net/detail/taiyang1987912/9436720。
(3)若有问题或建议,请留言,在此感谢!

这篇关于Qt浅谈之四十四动态显示日志(QGraphicsItem)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/797520

相关文章

Qt实现网络数据解析的方法总结

《Qt实现网络数据解析的方法总结》在Qt中解析网络数据通常涉及接收原始字节流,并将其转换为有意义的应用层数据,这篇文章为大家介绍了详细步骤和示例,感兴趣的小伙伴可以了解下... 目录1. 网络数据接收2. 缓冲区管理(处理粘包/拆包)3. 常见数据格式解析3.1 jsON解析3.2 XML解析3.3 自定义

C++如何通过Qt反射机制实现数据类序列化

《C++如何通过Qt反射机制实现数据类序列化》在C++工程中经常需要使用数据类,并对数据类进行存储、打印、调试等操作,所以本文就来聊聊C++如何通过Qt反射机制实现数据类序列化吧... 目录设计预期设计思路代码实现使用方法在 C++ 工程中经常需要使用数据类,并对数据类进行存储、打印、调试等操作。由于数据类

Java程序进程起来了但是不打印日志的原因分析

《Java程序进程起来了但是不打印日志的原因分析》:本文主要介绍Java程序进程起来了但是不打印日志的原因分析,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录Java程序进程起来了但是不打印日志的原因1、日志配置问题2、日志文件权限问题3、日志文件路径问题4、程序

Java使用SLF4J记录不同级别日志的示例详解

《Java使用SLF4J记录不同级别日志的示例详解》SLF4J是一个简单的日志门面,它允许在运行时选择不同的日志实现,这篇文章主要为大家详细介绍了如何使用SLF4J记录不同级别日志,感兴趣的可以了解下... 目录一、SLF4J简介二、添加依赖三、配置Logback四、记录不同级别的日志五、总结一、SLF4J

Qt中QGroupBox控件的实现

《Qt中QGroupBox控件的实现》QGroupBox是Qt框架中一个非常有用的控件,它主要用于组织和管理一组相关的控件,本文主要介绍了Qt中QGroupBox控件的实现,具有一定的参考价值,感兴趣... 目录引言一、基本属性二、常用方法2.1 构造函数 2.2 设置标题2.3 设置复选框模式2.4 是否

QT进行CSV文件初始化与读写操作

《QT进行CSV文件初始化与读写操作》这篇文章主要为大家详细介绍了在QT环境中如何进行CSV文件的初始化、写入和读取操作,本文为大家整理了相关的操作的多种方法,希望对大家有所帮助... 目录前言一、CSV文件初始化二、CSV写入三、CSV读取四、QT 逐行读取csv文件五、Qt如何将数据保存成CSV文件前言

Qt中QUndoView控件的具体使用

《Qt中QUndoView控件的具体使用》QUndoView是Qt框架中用于可视化显示QUndoStack内容的控件,本文主要介绍了Qt中QUndoView控件的具体使用,具有一定的参考价值,感兴趣的... 目录引言一、QUndoView 的用途二、工作原理三、 如何与 QUnDOStack 配合使用四、自

python logging模块详解及其日志定时清理方式

《pythonlogging模块详解及其日志定时清理方式》:本文主要介绍pythonlogging模块详解及其日志定时清理方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地... 目录python logging模块及日志定时清理1.创建logger对象2.logging.basicCo

Qt spdlog日志模块的使用详解

《Qtspdlog日志模块的使用详解》在Qt应用程序开发中,良好的日志系统至关重要,本文将介绍如何使用spdlog1.5.0创建满足以下要求的日志系统,感兴趣的朋友一起看看吧... 目录版本摘要例子logmanager.cpp文件main.cpp文件版本spdlog版本:1.5.0采用1.5.0版本主要

浅谈配置MMCV环境,解决报错,版本不匹配问题

《浅谈配置MMCV环境,解决报错,版本不匹配问题》:本文主要介绍浅谈配置MMCV环境,解决报错,版本不匹配问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录配置MMCV环境,解决报错,版本不匹配错误示例正确示例总结配置MMCV环境,解决报错,版本不匹配在col