• QTableWidget简单应用之文件浏览器


     实现下面这个简单的文件浏览功能,常用的QTableWidget设置都用到了。 
    这里写图片描述

    基本设置

        ui->tableWidget->setColumnCount(5);                               //设置表格列数
        ui->tableWidget->horizontalHeader()->setStretchLastSection(true); //设置表格是否充满,即行位不留空
        ui->tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows);//设置鼠标选择行为:一个单元格/一行/一列
        ui->tableWidget->horizontalHeader()->setFixedHeight(35);          //设置表头高度
    
        /*设置表头单元格内容*/
        QStringList header;
        header<<tr("File Name")<<tr("Size")<<tr("Double Click to Decode") << "Double Click to Close"<< tr("Decode Process");
        ui->tableWidget->setHorizontalHeaderLabels(header);
        /*设置表头字体加粗*/
        QFont font = ui->tableWidget->horizontalHeader()->font();
        font.setBold(true);
        ui->tableWidget->horizontalHeader()->setFont(font);
        ui->tableWidget->setFrameShape(QFrame::NoFrame);                  //设置无边框
        ui->tableWidget->verticalHeader()->hide();                        //设置垂直的头部隐藏,不隐藏默认会显示列表数字
        ui->tableWidget->horizontalHeader()->resizeSection(0,150);        //设置第0列的宽度为150px
        ui->tableWidget->setItemDelegate(new NoFocusDelegate());          //设置鼠标行时,不显示单元格虚框,NoFocusDelegate类实现如附录
        ui->tableWidget->setMouseTracking(true);                          //设置鼠标捕捉,不设置,如鼠标进入等动作检测不到
        ui->tableWidget->setStyleSheet("selection-");//设置行选中时背景颜色
        ui->tableWidget->horizontalHeader()->setStyleSheet("QHeaderView::section{background:greenyellow;}");//表头背景颜色
        /*信号槽,单元格双击*/
        connect(ui->tableWidget, SIGNAL(cellDoubleClicked(int,int)),
                this, SLOT(sdlog2Decode(int,int)));
        /*信号槽,鼠标进入单元格*/
        connect(ui->tableWidget,SIGNAL(itemEntered(QTableWidgetItem*)),
                this,SLOT(itemEnteredHover(QTableWidgetItem *)));
    
        /*添加构件到单元格,如按钮,进度条等*/
        QProgressBar *pgbar = new QProgressBar(ui->tableWidget);
        pgbar->setTextVisible(false);       
        ui->tableWidget->setCellWidget(row,column,pgbar);                      //替换当前widget
    
        /*设置单元格背景颜色*/
        QTableWidgetItem *itemtemp = ui->tableWidget->item(row, col);     
        itemtemp->setBackgroundColor(QColor(255,255,255,255));
    
        ui->tableWidget->removeCellWidget(row,col);                            //移除单元格中的构件
        ui->tableWidget->removeRow(row);                                       //移除表格某一行
        int col = ui->tableWidget->columnCount();                              //获取表格列数
        int row = ui->tableWidget->rowCount();                                 //获取行数
    
        /*插入第row行,以两列为例*/
        ui->tableWidget->insertRow(row);
        QTableWidgetItem *Item1 = new QTableWidgetItem("hello1");
        Item1->setTextAlignment(Qt::AlignCenter | Qt::AlignVCenter);
        Item1->setFlags(Item1->flags() ^ Qt::ItemIsEditable);
        QTableWidgetItem *Item2 = new QTableWidgetItem("hello2");              //设置单元格内容
        Item2->setTextAlignment(Qt::AlignCenter | Qt::AlignVCenter);           //设置单元格内容对其方式
        Item2->setFlags(Item2->flags() ^ Qt::ItemIsEditable);                  //设置单元格是否可编辑
        ui->tableWidget->setItem(row, 0, Item1);
        ui->tableWidget->setItem(row, 1, Item2);
    
        /*设置各种文件icon*/
        QFileInfo info;
        QFile file(currentDir.absoluteFilePath(filesname));
        info = QFileInfo(filesname);
        qint64 size = info.size();
        QFileIconProvider icon_provider;
        QIcon icon = icon_provider.icon(info);
        QTableWidgetItem *fileNameItem = new QTableWidgetItem(info.filePath());
        fileNameItem->setIcon(icon);
        fileNameItem->setTextAlignment(Qt::AlignCenter | Qt::AlignVCenter);
        fileNameItem->setFlags(fileNameItem->flags() ^ Qt::ItemIsEditable);

    附录程序

    NoFocusDelegate.h
    
    #ifndef NoFocusDelegate_H
    #define NoFocusDelegate_H
    #include <QPainter>
    #include <QStyledItemDelegate>
    class NoFocusDelegate :public QStyledItemDelegate
    {
    public:
        NoFocusDelegate();
        ~NoFocusDelegate();
    protected:
         void paint(QPainter* painter, const QStyleOptionViewItem & option, const QModelIndex &index) const;
    };
    #endif // SETHIGHLIGHTSECTIONS_H
    NoFocusDelegate.cpp
    
    #include "NoFocusDelegate.h"
    NoFocusDelegate::NoFocusDelegate()
    {
    }
    NoFocusDelegate::~NoFocusDelegate()
    {
    }
    void NoFocusDelegate::paint(QPainter* painter, const QStyleOptionViewItem & option, const QModelIndex &index) const
    {
       QStyleOptionViewItem itemOption(option);
       if (itemOption.state & QStyle::State_HasFocus)
       {
          itemOption.state = itemOption.state ^ QStyle::State_HasFocus;
       }
       QStyledItemDelegate::paint(painter, itemOption, index);
    } 

    http://blog.csdn.net/freeape/article/details/47404265

  • 相关阅读:
    Windows下安装MySQLdb(mysql-python)报错解决
    关于安装bettercap的ERROR: Failed to build gem native extension 报错解决
    Centos6 下启动httpd报错 Could not reliably determine the server's解决方法
    Linux安装mysql-python库时报错解决办法
    [Flask]学习Flask第三天笔记总结
    [原创+实战+钓鱼]setoolkit+映射
    [实战]挖掘CSRF姿势
    [实战]记一次PUT方法拿下网站shell
    JAVA二维数组小记
    技术杂谈
  • 原文地址:https://www.cnblogs.com/findumars/p/6294177.html
走看看 - 开发者的网上家园