• QT实现点击表格控件获取位置



    前言

    上一章讲了使用setIndexWidget可以在表格中添加控件,当我们需要获取到点QCheckBox 控件在表格中的位置


    一、核心代码

    在生成控件的时候绑定信号

    	QCheckBox *tempComboBox1 = new QCheckBox(this);
        tempComboBox1->setText("Yes");
        ui->tableView->setIndexWidget(m_modelProxy.index(1, 3), tempComboBox1);
        connect(tempComboBox1 , SIGNAL(stateChanged(int)) , this , SLOT(changTableButton(int)));
    
    • 1
    • 2
    • 3
    • 4

    槽函数中获取坐标

    void Widget::changTableButton(int state)
    {
       QCheckBox *senderObj = qobject_cast<QCheckBox*>(sender());
       if(senderObj == nullptr){
           return;
       }
       if(Qt::Checked == state){
           qDebug() << "checked";
       }else if(Qt::Unchecked == state){
           qDebug() << "Unchecked";
       }
    
    
       int x = senderObj->frameGeometry().x();
       int y = senderObj->frameGeometry().y();
       QModelIndex index = ui->tableView->indexAt(QPoint(x, y));
       int row = index.row();
       int column = index.column();
       qDebug() << "row:" << row << "column:" << column;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    二、全部代码

    头文件

    #ifndef WIDGET_H
    #define WIDGET_H
    
    #include 
    #include 
    #include 
    #include 
    #include 
    
    namespace Ui {
    class Widget;
    }
    
    class Widget : public QWidget
    {
        Q_OBJECT
        
    public:
        explicit Widget(QWidget *parent = 0);
        ~Widget();
        
        void initHearder();
        void addData();
        void rmData(int count);
    
    private slots:
        void findData();
        void changTableButton(int state);
    private:
        Ui::Widget *ui;
    
        QStringList m_headerList;
        QStandardItemModel m_tableModel;
        QSortFilterProxyModel m_modelProxy;
    };
    
    #endif // WIDGET_H
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38

    源文件

    #include 
    #include 
    #include "widget.h"
    #include "ui_widget.h"
    
    Widget::Widget(QWidget *parent) :
        QWidget(parent),
        ui(new Ui::Widget)
    {
        ui->setupUi(this);
        initHearder();
        addData();
    
        connect(ui->lineEdit, SIGNAL(textChanged(QString)),
                this, SLOT(findData()));
    }
    
    Widget::~Widget()
    {
        delete ui;
    }
    
    void Widget::initHearder()
    {
        m_headerList << "name" << "class" << "gender" <<  "key" << "go" << " ";
        m_modelProxy.setSourceModel(&m_tableModel);
        ui->tableView->setModel(&m_modelProxy);
        m_tableModel.setHorizontalHeaderLabels(m_headerList);
    }
    
    void Widget::addData()
    {
        m_tableModel.setItem(0, 0, new QStandardItem("jin"));
        m_tableModel.setItem(0, 1, new QStandardItem("1.2"));
        m_tableModel.setItem(0, 2, new QStandardItem("1"));
    
        m_tableModel.setItem(1, 0, new QStandardItem("hu"));
        m_tableModel.setItem(1, 1, new QStandardItem("1.1"));
        m_tableModel.setItem(1, 2, new QStandardItem("0"));
    
        m_tableModel.setItem(2, 0, new QStandardItem("wang"));
        m_tableModel.setItem(2, 1, new QStandardItem("1.1"));
        m_tableModel.setItem(2, 2, new QStandardItem("0"));
    
        m_tableModel.setItem(3, 0, new QStandardItem("li"));
        m_tableModel.setItem(3, 1, new QStandardItem("1.1"));
        m_tableModel.setItem(3, 2, new QStandardItem("1"));
    
    
        QCheckBox *tempComboBox = new QCheckBox(this);
        tempComboBox->setText("Yes");
        ui->tableView->setIndexWidget(m_modelProxy.index(0, 3), tempComboBox);
        //connect(tempComboBox , SIGNAL(clicked()) , this , SLOT(changTableButton()));
        connect(tempComboBox , SIGNAL(stateChanged(int)) , this , SLOT(changTableButton(int)));
    
        QCheckBox *tempComboBox1 = new QCheckBox(this);
        tempComboBox1->setText("Yes");
        ui->tableView->setIndexWidget(m_modelProxy.index(1, 3), tempComboBox1);
        connect(tempComboBox1 , SIGNAL(stateChanged(int)) , this , SLOT(changTableButton(int)));
    }
    
    void Widget::rmData(int count)
    {
        m_tableModel.removeColumn(count);
    }
    
    void Widget::findData()
    {
        m_modelProxy.setFilterFixedString(ui->lineEdit->text());
        return;
    }
    
    void Widget::changTableButton(int state)
    {
       QCheckBox *senderObj = qobject_cast<QCheckBox*>(sender());
       if(senderObj == nullptr){
           return;
       }
       if(Qt::Checked == state){
           qDebug() << "checked";
       }else if(Qt::Unchecked == state){
           qDebug() << "Unchecked";
       }
    
    
       int x = senderObj->frameGeometry().x();
       int y = senderObj->frameGeometry().y();
       QModelIndex index = ui->tableView->indexAt(QPoint(x, y));
       int row = index.row();
       int column = index.column();
       qDebug() << "row:" << row << "column:" << column;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93

    结果

    在这里插入图片描述

  • 相关阅读:
    【杂项笔记】Linux使用相关指令(持续更新)
    cumsum() R函数:用于产生随机变量的累积和
    Postman-APIs是干什么的?
    MSDC 4.3 接口规范(16)
    GitHub操作之远程仓库基本操作(团队内创建,推送,拉取等)
    面试问JUC(java.util.concurrent)的常见类你能答出来几句?
    R语言 利用tmap绘制分级色彩地图
    Docker | 发布镜像到镜像仓库
    nlp学习笔记
    latex给文字添加阴影--链接中转站
  • 原文地址:https://blog.csdn.net/qq_40062917/article/details/126783116