2019-06-12

QTextEdit动态显示gif动画

原理:使用QMovie播放gif并更新当前帧到QTextEdit中Document的Resource。
相关信号:QMovie::frameChanged(int frameNo)
实例代码:


初始化QMovie对象:

[code lang="cpp"]
QMovie*movie=newQMovie(this);
movie->setFileName(img_path);
movie->setCacheMode(QMovie::CacheNone);
connect(movie,SIGNAL(frameChanged(int)),this,SLOT(frameChanged(int)));
movie->start();[/code]


槽函数:

[code lang="cpp"]
voidMyObject::frameChanged(intframeNumber)
if(frameNumber==-1)
return;

QMovie*movie=(QMovie*)this->sender();
QStringname=movie->fileName().toUtf8().toBase64();
ui->view->document()->addResource(QTextDocument::ImageResource,
name,
QVariant(movie->currentPixmap()));
ui->view->setLineWrapColumnOrWidth(ui->view->lineWrapColumnOrWidth());
[/code]

关键点:


QMovie对象在播放gif动画时发送frameChanged信号至槽,此处使用QOjbect::sender()方法来得到具体的对象指针,然后调用document的addResource方法更新当前的帧到其图像资源,最后调用setLineWrapColumnOrWidth来刷新显示。这样就实现了gif图片的不断刷新。

[转]Creating Local Mirrors for Updates or Installs

原文链接:https://wiki.centos.org/HowTos/CreateLocalMirror

If you have a lot of systems you may want to consider setting up a local mirror for updates or network installs. Lets assume we want to set up the share for CentOS 5.8 as /share/CentOS/5/... The $releasever variable in yum repo definitions always maps to the current major release number, in this case "5", that is typically a symbolic link pointing to the current minor release directory tree, now "5.8".


On the server machine become root and create the share:


mkdir -p /share/CentOS/5.8


cd /share/CentOS


ln -s 5.8 5


Adjust the path to a filesystem that has enough space, or mount a new disk/partition under /share or your preferred mountpoint. On my current system with a full mirror including ISO images and some local packages (see HowTos/CreateLocalRepos), this requires ~28GB.


Find a Current Mirror near you that supports rsync, set up a directory structure already populated with content from the 5.8 DVDs or CDs. Can add ISO images and any updates you already have downloaded.


The structure should look like this (or you may choose to only use a subset, such as "os" just for installs):


addons:


i386  x86_64


centosplus:


i386  x86_64


contrib:


i386  x86_64


cr:


i386  x86_64


extras:


i386  x86_64


fasttrack:


i386  x86_64


isos:


i386  x86_64


os:


i386  x86_64


updates:


i386  x86_64


(The above is current for 5.8.)


Populate according to your preferred architecture, or do both i386 and x86_64:


mkdir /tmp/mnt


mount -ro loop /share/CentOS/5.8/isos/x86_64/CentOS-5.8-x86_64-bin-DVD-1of2.iso /tmp/mnt


rsync -avHPS /tmp/mnt/ /share/CentOS/5.8/os/x86_64/


umount /tmp/mnt


mount -ro loop /share/CentOS/5.8/isos/x86_64/CentOS-5.8-x86_64-bin-DVD-2of2.iso /tmp/mnt


rsync -avHPS /tmp/mnt/ /share/CentOS/5.8/os/x86_64/


umount /tmp/mnt


If using CD ISOs then copy the contents of all CDs to the same directories for each architecture, overwriting files of the same names.


Then for CentOS 5.8 use a script like:


#!/bin/bash


if [ -d /share/CentOS/5.8 ] ; then


    rsync  -avSHP --delete --exclude "local*" --exclude "isos" nearby.rsync.centos.net::CentOS/5.8/ /share/CentOS/5.8/


else


    echo "Target directory /share/CentOS/5.8 not present."


fi


This can be run in cron to keep current. If you only want i386 or x86_64, or want to exclude things like [extras] or [centosplus] adapt as required. If running in cron one should probably be a bit more robust and use a lock file to assure that a job is not already running.


#!/bin/bash


if [ -f /var/lock/subsys/rsync_updates ]; then


    echo "Updates via rsync already running."


    exit 0


fi


if [ -d /share/CentOS/5.8 ] ; then


    touch /var/lock/subsys/rsync_updates


    rsync  -avSHP --delete --exclude "local*" --exclude "isos" nearby.rsync.centos.net::CentOS/5.8/ /share/CentOS/5.8/


    /bin/rm -f /var/lock/subsys/rsync_updates


else


    echo "Target directory /share/CentOS/5.8 not present."


fi


Create a link pointing to the current release, if it has changed.


cd /share/CentOS/


ln -fs 5.8 5


On the server you can use NFS to export the directory. NFS has the advantage over HTTP or FTP that updates will be used "in place" rather than copies to the cache being made by yum.


cat >> /etc/exports


/share  192.168.1.0/24(rw,mountpoint)


Substitute your local subnet or an explicit list of IP addresses.


If NFS is not already enabled:


exportfs -a


chkconfig nfs on


service nfs start


otherwise just


exportfs -r


Then on clients mount the share. Can use an entry in /etc/fstab like:


myserver.my.net:/share   /share   nfs   rw  0 0


Or if using autofs:


cd /


ln -s net/myserver/share


Set up /etc/yum.repos.d/CentOS-Base.repo like:


[base]


name=CentOS-$releasever - Base


baseurl=file:/share/CentOS/$releasever/os/$basearch/


gpgcheck=1


gpgkey=file:/etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-5


protect=1


priority=1


enabled=1


[updates]


name=CentOS-$releasever - Updates


baseurl=file:/share/CentOS/$releasever/updates/$basearch/


gpgcheck=1


gpgkey=file:/etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-5


protect=1


priority=1


enabled=1


[extras]


name=CentOS-$releasever - Extras


baseurl=file:/share/CentOS/$releasever/extras/$basearch/


gpgcheck=1


gpgkey=file:/etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-5


protect=1


priority=1


enabled=1


[centosplus]


name=CentOS-$releasever - Plus


baseurl=file:/share/CentOS/$releasever/centosplus/$basearch/


exclude=kernel*


gpgcheck=1


enabled=1


gpgkey=file:/etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-5


protect=0


priority=1


[contrib]


name=CentOS-$releasever - Contrib


baseurl=file:/share/CentOS/$releasever/contrib/$basearch/


gpgcheck=1


enabled=0


gpgkey=file:/etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-5


protect=0


priority=3


Be sure to set which repos are enabled and setup protectbase or priorities as you require for your needs.


Alternatively - use HTTP or FTP rather than, or in addition to, NFS to share the updates - left as an exercise for the reader, but some help for HTTP can be found here.


Variations on the Theme


If you prefer to maintain your own subset of locally tested updates, rather than just mirroring the released updates, you will need to generate the metadata for yum. Populate the updates directories as desired then run


createrepo /share/CentOS/5/updates/i386


createrepo /share/CentOS/5/updates/x86_64


If createrepo is not found (it is not installed by default)


yum install createrepo



If you have the bandwidth available, please consider becoming a public mirror.



Alternative approaches would be to use Dag Wieers' mrepo, lftp, or reposync from the yum-utils package that's hosted in the [os] repository.


Quoting Karanbir Singh from a centos-docs post:


reposync works for centos-3/4/5 and since it uses the internal yum setup, you can make it do quite nice things like share a cache for X local install setups and also use it to keep repo's in real sync without needing to get every package that is hosted in the repo. Also, works well for include / exclude segments so you can easily (and in a more yum friendly manner) manage the package set. With a bit more creativity, its possible to use that and squid to setup an avahi based lan area zeroconf yum mirror with failback to remote urls if required.


And Akemi Yagi on lftp:


You might want to add 'lftp' as an alternative method to rsync. I have been using lftp because rsync at work is capped at a miserably low speed. Also, there are more http / ftp sites available than rsync sites. It is also a one-liner. For example:


lftp -e 'open http:///centos/ && mirror -c --delete  5.8 && exit'


will mirror the whole 5.8 under the remote centos/ directory. The major shortcoming of lftp is that it is not as robust about preserving existing files and will often re-download perfectly good files if things are not exactly right with the time stamp, but ignoring time may prevent downloads of things like repodata that have the same names but new content and time stamps.


Migrating to a new Point Release


A new point release typically consists or a new [base] repo containing the latest files from the previous release with released updates rolled in plus a large set of new updates. To prepare for 5.9


cd /share/CentOS


cp -al 5.8 5.9


mv 5.9/updates/i386/RPMS/* 5.9/os/i386/CentOS


mv 5.9/updates/x86_64/RPMS/* 5.9/os/x86_64/CentOS


When 5.9 is released and the mirrors are updated, then change the mirror scripts to reflect the 5.8 to 5.9 transition and sync with the mirrors. Then


cd /share/CentOS


/bin/rm -f 5


/bin/ln -s 5.9 5



 

Qt TextDocument内容组织方式

Qt的QTextEdit等控件在存储内容的时候是以QTextDocument & QTextBlock & QTextFragment组织内容的。

包含关系为QTextDocument QTextBlock QTextFragment


一般一个控件(如QTextEdit)只包含一个QTextDocument,文档对象可以很容易的保存为HTML或者ODF文件


其中,QTextBlock是以换行符分隔的,遇到一个换行符就新建一个QTextBlock,因此,一行内容就是一个QTextBlock


而QTextBlock中包含了一个或多个QTextFragment,这些fragment可以是文字,图片或其他多媒体元素,fragment以内容的类型区分。


例如如下内容:


[文本][图片][文本]


上面的内容生成了一个block,这个block包含了3个fragment。


而内容:


[文本]


[图片]


[文本]


则生成了3个block,因为一行内容生成一个block。


这个概念在使用网络发送图文混排的内容时尤其重要,如果不能正确理解document/block/fragment的组织方式,则会丢失原始格式。

Insert an image into a QTextEdit

1. Using html tag


[code lang="cpp"]
QTextEdit *textEditor = new QTextEdit(0);
QTextDocumentFragment fragment;
fragment = QTextDocumentFragment::fromHtml("");
textEditor->textCursor().insertFragment(fragment);
textEditor->setVisible(true);[/code]

2. Using QTextDocument's resource



[code lang="cpp"]
QString file = QFileDialog::getOpenFileName(this, tr("Select an image"),
".", tr("Bitmap Files (*.bmp)
"
"JPEG (*.jpg *jpeg)
"
"GIF (*.gif)
"
"PNG (*.png)
"));
QUrl Uri ( file );
QImage image = QImageReader ( file ).read();
QTextDocument * textDocument = m_textEdit->document();
textDocument->addResource( QTextDocument::ImageResource, Uri, QVariant ( image ) );
QTextCursor cursor = m_textEdit->textCursor();
QTextImageFormat imageFormat;
imageFormat.setWidth( image.width() );
imageFormat.setHeight( image.height() );
imageFormat.setName( Uri.toString() );
cursor.insertImage(imageFormat);
[/code]

3. Subclass QTextEdit (The example below also enabled drag & drop)



[code lang="cpp"]
class TextEdit : public QTextEdit

public:
bool canInsertFromMimeData(const QMimeData* source) const

return source->hasImage()
void insertFromMimeData(const QMimeData* source)

if (source->hasImage())

static int i = 1;
QUrl url(QString("dropped_image_%1").arg(i++));
dropImage(url, qvariant_cast(source->imageData()));

else if (source->hasUrls())

foreach (QUrl url, source->urls())

QFileInfo info(url.toLocalFile());
if (QImageReader::supportedImageFormats().contains(info.suffix().toLower().toLatin1()))
dropImage(url, QImage(info.filePath()));
else
dropTextFile(url);


else

QTextEdit::insertFromMimeData(source);


private:
void dropImage(const QUrl& url, const QImage& image)

if (!image.isNull())

document()->addResource(QTextDocument::ImageResource, url, image);
textCursor().insertImage(url.toString());


void dropTextFile(const QUrl& url)
QIODevice::Text))
textCursor().insertText(file.readAll());

;
[/code]

(抄)linux下批量查找/替换文本内容

一般在本地电脑上批量替换文本有许多工具可以做到,比如sublime text ,但大多服务器上都是无图形界面的,为此收集了几条针对linux命令行 实现批量替换文本内容的命令:

1.批量查找某个目下文件的包含的内容,例如: #   grep -rn "要找查找的文本" ./


2.批量查找并替换文件内容。



sed -i "s/要找查找的文本/替换后的文本/g" `grep -rl "要找查找的文本" ./`

Qt Model-View学习之手动更新Model数据

在使用基于QAbstractListModel的自定义Model时,遇到数据更新后Model不自动刷新数据的问题。通过手动发送更新信号即可解决。 方法,在更新数据时调用


[code lang="cpp"]
emit beginResetModel();
emit endResetModel();
[/code]

注意reset model需要成对使用。

[转]基于linux kernel的内存调优,附proc详解

原文出处:http://www.blogbus.com/ri0day-logs/48708807.html

内核关于内存的选项都在[code]/proc/sys/vm目录下.[/code]

1.pdflush,用于回写内存中的脏数据到硬盘。

可以通过 /proc/sys/vm/[code]vm.dirty_background_ratio调整。[/code] 首先查看这个值默认应该是10。

[[email protected] ~]# cat /proc/sys/vm/dirty_background_ratio

10

这个值是一个阀值,说明如果内存中的脏数据达到系统总内存的10%时,那么pdflush进程就会启动,将内存中的脏数据写回硬盘.这个值可适当调高.可获得更快的写入速度.

2.swappiness选项

[[email protected] ~]# cat /proc/sys/vm/swappiness

60

swappiness表示使用swap分区的使用程度,可以适当调整swappiness=0的时候表示尽可能使用物理内存swap空间.swappiness=100积极使用swap.

3.dirty_ratio

[[email protected] ~]# cat /proc/sys/vm/dirty_ratio

40

dirty_ratio的值是数据写进内存的阀值,40%是指当系统内存已经缓存了40%的数据以后,就不再往内存中缓存数据了.

附proc详解.. http://docs.google.com/View?id=dcbnv87x_22dc7hjkcn