目录
1.简介
本文主要介绍了如何实现Qt控件及子控件截图,重点内容包含
- 如何获取指定控件及其子控件的对象名称及对象类型;
- 如何对指定控件截图并显示。
本文还有动态效果图、重点讲解和源码,读者们可以方便查看学习和交流。
回目录
2.效果图
3.重点讲解
1)如何获取指定控件及其子控件的对象名称及对象类型
通过递归地方式依次遍历控件布局中包含的所有子布局、子控件的方式来获取指定控件及其子控件的对象信息(对象名称、对象类型名称)。
QStringList SnapShotTool::getWdtChildren(QLayout *wdtLay, QHash<QString/*控件名称*/, QWidget*/*控件指针*/> &hashNameWdt)
{
QStringList items;
if( wdtLay ){
int cnt = wdtLay->count();
for (int i=0; i<cnt; i++) {
QLayoutItem *it = wdtLay->itemAt(i);
QWidget *wdt = it->widget();
QLayout *layIt = it->layout();
if( wdt ){
QString objName = wdt->objectName();
QString objClassName = wdt->metaObject()->className();
QString strName = QString("%1:%2").arg(objClassName).arg(objName);
hashNameWdt.insert(strName, wdt);
items << strName;
items << getWdtChildren(wdt->layout(), hashNameWdt);
}else if( layIt ){
items << getWdtChildren(layIt, hashNameWdt);
}
}
}
return items;
}
获取控件的对象名称、对象类型名称的方法如下:
QString objName = wdt->objectName();
QString objClassName = wdt->metaObject()->className();
以下是“待截图控件信息(对象类型名称:对象名称)列表”的截图信息:
待截图控件信息(对象类型名称:对象名称)列表
2)如何对指定控件截图并显示
通过控件QWidget提供的grab方法可以对控件截图,截图数据类型为QPixmap:
QPixmap QWidget::grab(const QRect &rectangle = QRect( QPoint( 0, 0 ), QSize( -1, -1 ) ))
通过控件QLabel提供的setPixmap方法可以显示控件截图信息:
void QLabel::setPixmap(const QPixmap &)
也通过QPixmap提供的save方法保存控件截图信息为*.PNG等图片文件格式到本地电脑目录:
bool QPixmap::save(const QString &fileName, const char *format = Q_NULLPTR, int quality = -1) const
最后,通过SnapShotTool提供的setSnapShotWidget方法,我们就可以获取指定控件及子控件的截图信息了,大家可以去试一试:
void SnapShotTool::setSnapShotWidget(QWidget *wdtSnapShot);
4.源码
源码下载地址
https://download.csdn.net/download/Redboy_Crazy/12783401
SnapShotTool.h
#ifndef SNAPSHOTTOOL_H
#define SNAPSHOTTOOL_H
#include <QWidget>
class QLabel;
class QComboBox;
class SnapShotTool : public QWidget
{
Q_OBJECT
public:
explicit SnapShotTool(QWidget *parent = nullptr);
QStringList getWdtChildren(QLayout *wdtLay, QHash<QString/*控件名称*/, QWidget*/*控件指针*/> &hashNameWdt);
void setSnapShotWidget(QWidget *wdtSnapShot);
private slots:
void on_btnSnapShot_clicked();
signals:
private:
QHash<QString/*控件名称*/, QWidget*/*控件指针*/> m_hashNameWdt;
QLabel *m_labShowGrab; // 显示指定控件的截图
QComboBox *m_cBoxItems; // 待截图控件的项目控件
};
#endif // SNAPSHOTTOOL_H
SnapShotTool.cpp
#include "SnapShotTool.h"
#include <QLabel>
#include <QDebug>
#include <QHBoxLayout>
#include <QPushButton>
#include <QComboBox>
SnapShotTool::SnapShotTool(QWidget *parent) : QWidget(parent)
{
m_labShowGrab = new QLabel();
m_labShowGrab->hide();
QLabel *labName = new QLabel("待截图控件信息列表:", this);
QPushButton *m_btnSnapShot = new QPushButton("获取控件截图信息", this);
connect(m_btnSnapShot, &QPushButton::clicked,
this, &SnapShotTool::on_btnSnapShot_clicked);
m_cBoxItems = new QComboBox(this);
QHBoxLayout *lay = new QHBoxLayout(this);
lay->addWidget(labName);
lay->addWidget(m_cBoxItems);
lay->addWidget(m_btnSnapShot);
this->setLayout(lay);
this->setWindowTitle("控件截图工具");
}
QStringList SnapShotTool::getWdtChildren(QLayout *wdtLay, QHash<QString/*控件名称*/, QWidget*/*控件指针*/> &hashNameWdt)
{
QStringList items;
if( wdtLay ){
int cnt = wdtLay->count();
for (int i=0; i<cnt; i++) {
QLayoutItem *it = wdtLay->itemAt(i);
QWidget *wdt = it->widget();
QLayout *layIt = it->layout();
if( wdt ){
QString objName = wdt->objectName();
QString objClassName = wdt->metaObject()->className();
QString strName = QString("%1:%2").arg(objClassName).arg(objName);
hashNameWdt.insert(strName, wdt);
items << strName;
items << getWdtChildren(wdt->layout(), hashNameWdt);
}else if( layIt ){
items << getWdtChildren(layIt, hashNameWdt);
}
}
}
return items;
}
void SnapShotTool::setSnapShotWidget(QWidget *wdtSnapShot)
{
m_hashNameWdt.clear();
QString objName = wdtSnapShot->objectName();
QString objClassName = wdtSnapShot->metaObject()->className();
QString strName = QString("%1:%2").arg(objClassName).arg(objName);
m_hashNameWdt.insert(strName, wdtSnapShot);
QStringList items = getWdtChildren(wdtSnapShot->layout(), m_hashNameWdt);
m_cBoxItems->clear();
items.prepend(strName);
m_cBoxItems->addItems(items);
}
void SnapShotTool::on_btnSnapShot_clicked()
{
qDebug() << __FUNCTION__;
QString item = m_cBoxItems->currentText();
if ( !item.isEmpty() ){
qDebug() << item;
QWidget *wdt = m_hashNameWdt.value(item);
QPixmap pix = wdt->grab();
m_labShowGrab->setWindowTitle(item);
m_labShowGrab->setPixmap(pix);
m_labShowGrab->show();
}
}
加油,向未来!GO~
Come on!