QTreeWidget、QDockWidget使用

实验

  1. QTreeWidget遍历树节点
  2. QTreeWidget增加、删除树节点
  3. QDockWidget的显示,停靠
  4. 图片的加载显示,放大、缩小、横向自适应,竖向自适应

 .h文件

#ifndef TREEWIDGETEXAMPLE_H
#define TREEWIDGETEXAMPLE_H

#include <QMainWindow>
#include <QTreeWidgetItem>
#include <QLabel>
#include <QSpinBox>
#include <QPixMap>


namespace Ui {
class TreeWidgetExample;
}

class TreeWidgetExample : public QMainWindow
{
    Q_OBJECT
private:
    //
    enum treeItemType
    {
        itTopItem=1001,//顶层节点
        itGroupItem,//组节点  有叶子
        itImageItem};//图片  叶子

    enum treeColNum
    {
        colItem=0,
        colItemType=1,
        colDate
    };
    QLabel *labFileName;//
    QLabel *labNodeText;
    QSpinBox *spinRatio;
    QLabel *labInfo;

    QPixmap m_pixmap;//当前图片
    float m_ratio;//当前图片缩放比例

    void buildTreeHeader();//构建表头
    void iniTree();//目录树初始化
    //添加目录节点
    void addFolderItem(QTreeWidgetItem *parItem,QString dirName);
    //从目录全名获取最后的文件夹名
    QString getFinalFolderName(const QString &fullPathName);

    void addImageItem(QTreeWidgetItem *parItem,QString aFileName);

    void displayImage(QTreeWidgetItem *item);

    void changeItemCaption(QTreeWidgetItem *item);
    //删除节点
    void deleteItem(QTreeWidgetItem *parItem,QTreeWidgetItem *item);


public:
    explicit TreeWidgetExample(QWidget *parent = nullptr);
    ~TreeWidgetExample();

private slots:
    void on_treeFiles_currentItemChanged(QTreeWidgetItem *current, QTreeWidgetItem *previous);

    void on_actAddFolder_triggered();

    void on_actAddFiles_triggered();

    void on_actDeleteItem_triggered();

    void on_actScanItems_triggered();

    void on_actZoomIn_triggered();

    void on_actZoomOut_triggered();

    void on_actZoomRealSize_triggered();

    void on_actZoomFitW_triggered();

    void on_actZoomFitH_triggered();

    void on_actDockFloat_triggered(bool checked);

    void on_actDockVisible_triggered(bool checked);

    void on_dockWidget_visibilityChanged(bool visible);

    void on_dockWidget_topLevelChanged(bool topLevel);

    void on_treeFiles_itemChanged(QTreeWidgetItem *item, int column);

    void on_treeFiles_itemCollapsed(QTreeWidgetItem *item);

    void on_treeFiles_itemExpanded(QTreeWidgetItem *item);

    void on_treeFiles_itemSelectionChanged();

    void on_treeFiles_itemClicked(QTreeWidgetItem *item, int column);

    void on_treeFiles_itemPressed(QTreeWidgetItem *item, int column);

private:
    Ui::TreeWidgetExample *ui;
};

#endif // TREEWIDGETEXAMPLE_H

.cpp文件

#include "treewidgetexample.h"
#include "ui_treewidgetexample.h"
#include <QFileDialog>
#include <QPixmap>

void TreeWidgetExample::buildTreeHeader()
{
    ui->treeFiles->clear();
    //创建节点
    QTreeWidgetItem *header = new QTreeWidgetItem();
    header->setText(TreeWidgetExample::colItem,"目录和文件");
    header->setText(TreeWidgetExample::colItemType,"节点类型");
    header->setText(TreeWidgetExample::colDate,"最后修改日期");

    header->setTextAlignment(colItem,Qt::AlignHCenter|Qt::AlignVCenter);
    header->setTextAlignment(colItemType,Qt::AlignHCenter|Qt::AlignVCenter);

    ui->treeFiles->setHeaderItem(header);
}

void TreeWidgetExample::iniTree()
{
    QIcon icon("://treeIcons/15.ico");
    QTreeWidgetItem *item = new QTreeWidgetItem(TreeWidgetExample::itTopItem);
    item->setIcon(TreeWidgetExample::colItem,icon);
    item->setText(TreeWidgetExample::colItem,"图片");
    item->setText(TreeWidgetExample::colItemType,"Top Item");
    item->setFlags(Qt::ItemIsSelectable
                   |Qt::ItemIsUserCheckable
                   |Qt::ItemIsEnabled
                   |Qt::ItemIsAutoTristate//三态
                   );
    item->setCheckState(TreeWidgetExample::colItem
                        ,Qt::Checked);

    QString dataStr="用户数据";
    item->setData(TreeWidgetExample::colItem
                  ,Qt::UserRole
                  ,QVariant(dataStr)
                  );
    //添加顶层节点
    ui->treeFiles->addTopLevelItem(item);
}

QString TreeWidgetExample::getFinalFolderName(const QString &fullPathName)
{
    int len= fullPathName.length();
    int i= fullPathName.lastIndexOf("/");
    QString str= fullPathName.right(len-(i+1));
    return str;
}

void TreeWidgetExample::addFolderItem(QTreeWidgetItem *parItem, QString dirName)
{
    QIcon icon("://treeIcons/open3.bmp");
    QString NodeText = this->getFinalFolderName(dirName);

    QTreeWidgetItem *item = new QTreeWidgetItem(TreeWidgetExample::itGroupItem);
    item->setIcon(TreeWidgetExample::colItem,icon);
    item->setText(TreeWidgetExample::colItem,NodeText);
    item->setText(TreeWidgetExample::colItemType,"Group Item");

    item->setCheckState(TreeWidgetExample::colItem,Qt::Checked);
    item->setFlags(Qt::ItemIsSelectable
                   |Qt::ItemIsUserCheckable
                   |Qt::ItemIsEnabled
                   |Qt::ItemIsAutoTristate//三态
                   );
    item->setData(TreeWidgetExample::colItem,Qt::UserRole,QVariant(dirName));
    parItem->addChild(item);
}

void TreeWidgetExample::addImageItem(QTreeWidgetItem *parItem, QString aFileName)
{
    QIcon icon("://treeIcons/31.ico");
    QFileInfo file(aFileName);
    QString NodeText = file.fileName();
    QDateTime birthDate = file.lastModified();
    QTreeWidgetItem *item = new QTreeWidgetItem(TreeWidgetExample::itImageItem);
    item->setIcon(TreeWidgetExample::colItem,icon);
    item->setText(TreeWidgetExample::colItem,NodeText);
    item->setText(TreeWidgetExample::colItemType,"Image Item");

    item->setCheckState(TreeWidgetExample::colItem,Qt::Checked);
    item->setFlags(Qt::ItemIsSelectable
                   |Qt::ItemIsUserCheckable
                   |Qt::ItemIsEnabled
                   |Qt::ItemIsAutoTristate//三态
                   );
    item->setData(TreeWidgetExample::colItem,Qt::UserRole,QVariant(aFileName));
    parItem->addChild(item);

}

void TreeWidgetExample::displayImage(QTreeWidgetItem *item)
{
    QString fileName = item->data(colItem,Qt::UserRole).toString();
    labFileName->setText(fileName);
    labNodeText->setText(item->text(colItem));
    m_pixmap.load(fileName);
    // ui->labPic->setPixmap(m_pixmap);
    ui->actZoomFitH->triggered();
    ui->actZoomFitH->setEnabled(true);
    ui->actZoomFitW->setEnabled(true);
    ui->actZoomIn->setEnabled(true);
    ui->actZoomOut->setEnabled(true);
    ui->actZoomRealSize->setEnabled(true);
    labInfo->setText(QString("显示:%1").arg(fileName));
    labFileName->setText(QString("显示:%1").arg(fileName));
}
void TreeWidgetExample::changeItemCaption(QTreeWidgetItem *item)
{
    QString str ="*"+item->text(colItem);
    item->setText(colItem,str);
    if(item->childCount()>0)
    {
        for(int i=0;i<item->childCount();i++)
        {
            changeItemCaption(item->child(i));//递归子节点 设置caption
        }
    }
}

void TreeWidgetExample::deleteItem(QTreeWidgetItem *parItem, QTreeWidgetItem *item)
{
    int count = item->childCount();
    if(count>0)
    {
        for(int i=0;i<count;i++)
        {
            deleteItem(item,item->child(i));
        }
    }
    parItem->removeChild(item);
    delete item;
}


TreeWidgetExample::TreeWidgetExample(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::TreeWidgetExample)
{
    ui->setupUi(this);
    //创建状态栏上的组件
    labNodeText = new QLabel("节点标题",this);
    labNodeText->setMinimumWidth(200);
    ui->statusbar->addWidget(labNodeText);

    spinRatio= new QSpinBox(this);
    spinRatio->setRange(0,2000);
    spinRatio->setValue(100);
    spinRatio->setSuffix(" %");
    spinRatio->setReadOnly(true);
    spinRatio->setMinimumWidth(100);
    spinRatio->setButtonSymbols(QAbstractSpinBox::NoButtons);
    ui->statusbar->addPermanentWidget(spinRatio);

    labFileName = new QLabel("文件名",this);
    ui->statusbar->addPermanentWidget(labFileName);

    labInfo = new QLabel("qDebug信息",this);
    labInfo->setMinimumWidth(300);
    ui->statusbar->addWidget(labInfo);

    buildTreeHeader();
    iniTree();
    setCentralWidget(ui->scrollArea);
}

TreeWidgetExample::~TreeWidgetExample()
{
    delete ui;
}


void TreeWidgetExample::on_treeFiles_currentItemChanged(QTreeWidgetItem *current, QTreeWidgetItem *previous)
{
    if(current==nullptr)
        return;
    if(current== previous)
        return;
    int nodeType = current->type();



    switch(nodeType)
    {
    case itTopItem:
        ui->actAddFolder->setEnabled(true);
        ui->actAddFiles->setEnabled(true);
        ui->actDeleteItem->setEnabled(false); //顶层节点不可删除
        break;
    case itGroupItem:
        ui->actAddFolder->setEnabled(true);
        ui->actAddFiles->setEnabled(true);
        ui->actDeleteItem->setEnabled(true);
        break;
    case itImageItem:
        ui->actAddFolder->setEnabled(false);  //叶子节点不可以添加文件夹
        ui->actAddFiles->setEnabled(true);
        ui->actDeleteItem->setEnabled(true);

        displayImage(current);
        break;

    }
}


void TreeWidgetExample::on_actAddFolder_triggered()
{
    QString dir= QFileDialog::getExistingDirectory();
    if(dir.isEmpty())
        return;
    QTreeWidgetItem *parItem = ui->treeFiles->currentItem();
    if(parItem==nullptr)
        return;
    if(parItem->type()!= itImageItem)
        addFolderItem(parItem,dir);
}


void TreeWidgetExample::on_actAddFiles_triggered()
{//批量添加图片
    QStringList files = QFileDialog::getOpenFileNames(this,"选择文件","","Images(*.jpg)");// *.png *.bmp
    if(files.isEmpty())
        return;
    QTreeWidgetItem *parItem,*item;
    item=ui->treeFiles->currentItem();
    if(item==nullptr)
        item=ui->treeFiles->topLevelItem(0);
    if(item->type()== itImageItem)
        parItem= item->parent();
    else
        parItem = item;

    for(int i=0;i<files.size();++i)  //size
    {
        QString str = files.at(i);
        addImageItem(parItem,str);
    }
    parItem->setExpanded(true);
}


void TreeWidgetExample::on_actDeleteItem_triggered()
{
    QTreeWidgetItem *parItem,*item;
    item = ui->treeFiles->currentItem();
    if(item==nullptr)
        return;
    parItem= item->parent();
    deleteItem(parItem,item);
}


void TreeWidgetExample::on_actScanItems_triggered()
{
    for(int i=0;i<ui->treeFiles->topLevelItemCount();i++)
    {
        QTreeWidgetItem *item = ui->treeFiles->topLevelItem(i);
        changeItemCaption(item);
    }
}


void TreeWidgetExample::on_actZoomIn_triggered()
{
    m_ratio= m_ratio*1.2;
    spinRatio->setValue(100*m_ratio);

    int w= m_ratio*m_pixmap.width();
    int h= m_ratio * m_pixmap.height();

    QPixmap pix = m_pixmap.scaled(w,h);
    ui->labPic->setPixmap(pix);
}


void TreeWidgetExample::on_actZoomOut_triggered()
{
    m_ratio= m_ratio*0.8;
    spinRatio->setValue(100*m_ratio);

    int w= m_ratio*m_pixmap.width();
    int h= m_ratio * m_pixmap.height();

    QPixmap pix = m_pixmap.scaled(w,h);
    ui->labPic->setPixmap(pix);
}


void TreeWidgetExample::on_actZoomRealSize_triggered()
{
    m_ratio= 1;
    spinRatio->setValue(100);//还原成初始值
    ui->labPic->setPixmap(m_pixmap);//重新加载图片
}


void TreeWidgetExample::on_actZoomFitW_triggered()
{
    int w= ui->scrollArea->width();
    int realW = m_pixmap.width();
    m_ratio = float(w)/realW;
    spinRatio->setValue(100*m_ratio);

    QPixmap pix = m_pixmap.scaledToWidth(w-30);
    ui->labPic->setPixmap(pix);
}


void TreeWidgetExample::on_actZoomFitH_triggered()
{
    int h= ui->scrollArea->height();
    int realH= m_pixmap.height();
    m_ratio = float(realH)/h;
    spinRatio->setValue(100*m_ratio);

    QPixmap pix = m_pixmap.scaledToHeight(h-30);
    ui->labPic->setPixmap(pix);
}


void TreeWidgetExample::on_actDockFloat_triggered(bool checked)
{
    ui->dockWidget->setFloating(checked);
}


void TreeWidgetExample::on_actDockVisible_triggered(bool checked)
{
    ui->dockWidget->setVisible(checked);
}

void TreeWidgetExample::on_dockWidget_topLevelChanged(bool topLevel)
{
    ui->actDockFloat->setChecked(topLevel);
}


void TreeWidgetExample::on_dockWidget_visibilityChanged(bool visible)
{
    ui->actDockVisible->setChecked(visible);
}

void TreeWidgetExample::on_treeFiles_itemChanged(QTreeWidgetItem *item, int column)
{
    Q_UNUSED(item);
    Q_UNUSED(column);

    labInfo->setText("itemChanged() is emited");
}


void TreeWidgetExample::on_treeFiles_itemCollapsed(QTreeWidgetItem *item)
{
    Q_UNUSED(item);
    labInfo->setText("itemCollapsed() is emited");
}


void TreeWidgetExample::on_treeFiles_itemExpanded(QTreeWidgetItem *item)
{
    Q_UNUSED(item);
    labInfo->setText("itemExpanded() is emited");
}


void TreeWidgetExample::on_treeFiles_itemSelectionChanged()
{
    labInfo->setText("itemSelectionChanged() is emited");
}


void TreeWidgetExample::on_treeFiles_itemClicked(QTreeWidgetItem *item, int column)
{
    Q_UNUSED(item);
    Q_UNUSED(column);
    labInfo->setText("itemClicked() is emited");
}


void TreeWidgetExample::on_treeFiles_itemPressed(QTreeWidgetItem *item, int column)
{
    Q_UNUSED(item);
    Q_UNUSED(column);
    labInfo->setText("itemPressed() is emited");
}

效果

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

castlooo

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值