QT tableWidget的簡單使用


1、不能對表格內容進行修改

ui->tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);

設置某一列不可編輯

QTableWidgetItem *item1 = new QTableWidgetItem(“xxx”);   //設置第1列不可編輯
item1->setFlags(item1->flags() & (~Qt::ItemIsEditable));
ui->tableWidget->setItem(i, 0, item1);  //第0列不可編輯

 

2、設置表格為整行選擇

ui->tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows);

3、設置隱藏表頭和列頭

ui->tableWidget->verticalHeader()->setVisible(false); //
ui->tableWidget->horizontalHeader()->setVisible(false);  //

隱藏列

ui->tableWidget->setColumnHidden(i, true);     // i表示第幾列

 

4、加入控件

ui->tableWidget->setShowGrid(false);    //關閉網格
ui->tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);//選中一行

 

5、列可擴展

ui->tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);    // 列可擴展

6、單元格字體居中

ui->tableWidget->item(i,j)->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter); // i代表行,j代表列
void Mymethod::setTableAligCenter(QTableWidget *tw)
{
    int rowCount = tw->rowCount();
    int columCount = tw->columnCount();
    for(int row=0;row<rowCount;row++)
    {
        for(int colum=0;colum<columCount;colum++)
        {
            QTableWidgetItem* item = tw->item(row, colum);
            if(item != nullptr)
            {
                item->setTextAlignment(Qt::AlignCenter);//感覺效率不高
            }
        }
    }
}

7、獲取行數和列數

ui->tableWidget->rowCount();
ui->tableWidget->columnCount();

8、右鍵使能,在給widget加菜單時,需要加入此行代碼

ui->tableWidget->setContextMenuPolicy(Qt::CustomContextMenu);

9、給QTableWidget添加控件

 

ui->tableWidget->setCellWidget(i, j , widget);    //其中i代表行號,j代表列號,widget代表控件
例1:
QPushButton *pBtn = new QPushButton("查看");
ui->tableWidget->setCellWidget(0,0,pBtn);
例2:
QWidget *widget = new QWidget;
widget->setMaximumSize(ui->tableWidget->width(),ui->tableWidget->height());
QPushButton *pBtn = new QPushButton("查看");
QPushButton *pBtn1 = new QPushButton("修改");
QPushButton *pBtn2 = new QPushButton("刪除");
QHBoxLayout *hbox = new QHBoxLayout(widget);
hbox->addWidget(pBtn);
hbox->addStretch(10);
hbox->addWidget(pBtn1);
hbox->addStretch(10);
hbox->addWidget(pBtn2);
hbox->setMargin(0);
widget->setLayout(hbox);
ui->tableWidget->setCellWidget(0, 1, widget);

10、獲取當前點擊的控件在表的那個位置

//一個單元格內單個控件,以pushButton為例

QPushButton *btn = (QPushButton *)sender();
int x = btn ->mapToParent(QPoint(0,0)).x();
int y = btn ->mapToParent(QPoint(0,0)).y();
QModelIndex index = ui->tableWidget->indexAt(QPoint(x,y));
int row = index.row();
int column = index.column();

//一個單元格內多個控件

QPushButton *btn = (QPushButton *)sender();
QWidget *w = (QWidget *)btn->parent();
int x = w->mapToParent(QPoint(0,0)).x();
int y = w->mapToParent(QPoint(0,0)).y();
QModelIndex index = ui->tableWidget->indexAt(QPoint(x,y));
int row = index.row();
int column = index.column();

 

11、例:

.h文件

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QMenu>
#include <QAction>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = nullptr);
    ~MainWindow();
    void initTableWidget();

private slots:
    void MenuSlot(QAction *action);
    void showMenuPos(QPoint);

private:
    Ui::MainWindow *ui;
    QMenu *menu;
    QAction *addColumn, *reColumn, *addRow, *reRow;
};

#endif // MAINWINDOW_H

.cpp文件

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QString>
#include <QDebug>
#include <QPoint>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    initTableWidget();
    QObject::connect(ui->tableWidget, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(showMenuPos(QPoint)));
    QObject::connect(menu, SIGNAL(triggered(QAction*)), this, SLOT(MenuSlot(QAction*)));
}

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

/*****************************************************************/
//作者:
//函數名稱:initTableWidget
//函數參數:NULL
//函數返回值:NULL
//函數作用:初始化表格
//備注:NULL
/*****************************************************************/
void MainWindow::initTableWidget()
{
    ui->tableWidget->setRowCount(3);
    ui->tableWidget->setColumnCount(2);
    QStringList strList ;
    strList << "時間" << "年限";
    ui->tableWidget->setHorizontalHeaderLabels(strList);
    ui->tableWidget->verticalHeader()->setVisible(false);
    ui->tableWidget->setContextMenuPolicy(Qt::CustomContextMenu);       // 右鍵使能
    ui->tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);
    ui->tableWidget->setItem(0,0,new QTableWidgetItem("張飛"));
    ui->tableWidget->setItem(1,0,new QTableWidgetItem("關羽"));
    ui->tableWidget->setItem(2,0,new QTableWidgetItem("呂布"));

    ui->tableWidget->setItem(0,1,new QTableWidgetItem("35"));
    ui->tableWidget->setItem(1,1,new QTableWidgetItem("36"));
    ui->tableWidget->setItem(2,1,new QTableWidgetItem("32"));


    menu = new QMenu(ui->tableWidget);
    addColumn = new QAction(menu);
    addColumn->setText("新增一列");
    reColumn = new QAction(menu);
    reColumn->setText("刪除一列");
    addRow = new QAction(menu);
    addRow->setText("新增一行");
    reRow = new QAction(menu);
    reRow->setText("刪除一行");
    menu->addAction(addRow);
    menu->addAction(addColumn);
    menu->addAction(reRow);
    menu->addAction(reColumn);
    int column = ui->tableWidget->columnCount();
    for(int i = 0; i<column;i++)
    {
        ui->comboBox->addItem(ui->tableWidget->horizontalHeaderItem(i)->text());
    }
}

/*****************************************************************/
//作者:
//函數名稱:MenuSlot
//函數參數:action
//函數返回值:NULL
//函數作用:根據行為判斷當前所做的事
//備注:NULL
/*****************************************************************/
void MainWindow::MenuSlot(QAction *action)
{
    int column = ui->tableWidget->columnCount();
    int row = ui->tableWidget->rowCount();
    if(action == addRow)
    {
        ui->tableWidget->insertRow(row);
        for(int i = 0; i<row;i++)
        {
            ui->tableWidget->setItem(row,i,new QTableWidgetItem("張飛"));
        }
    }
    else if(action == addColumn)
    {
        if(!ui->lineEdit->text().isEmpty())
        {
            ui->tableWidget->insertColumn(column);
            ui->tableWidget->setHorizontalHeaderItem(column, new QTableWidgetItem(ui->lineEdit->text()));
            ui->comboBox->addItem(ui->lineEdit->text());
        }
        else {
            qDebug() << "添加失敗!!!";
            return ;
        }

    }
    else if (action == reRow) {
        ui->tableWidget->removeRow(ui->tableWidget->currentRow());
    }
    else if (action == reColumn) {
        for(int i = 0; i<column;i++)
        {
            if(ui->tableWidget->horizontalHeaderItem(i)->text() == ui->comboBox->currentText())
            {
                ui->tableWidget->removeColumn(i);
                ui->comboBox->removeItem(i);
            }
        }
    }
}

/*****************************************************************/
//作者:
//函數名稱:showMenuPos
//函數參數:NULL
//函數返回值:NULL
//函數作用:右鍵菜單顯示位置
//備注:NULL
/*****************************************************************/
void MainWindow::showMenuPos(QPoint)
{
    menu->exec(QCursor::pos());
}

.ui

 

 運行結果:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM