如何使 QTableWidget 中的列只读?

发布于 2024-08-27 06:40:32 字数 100 浏览 3 评论 0原文

我希望在 QTableWidget 中有一列不可编辑。
在论坛中,我读了很多关于一些标志的内容,但无法实现。

I would like to have one column in QTableWidget NOT editable.
In forums I have read a lot about some flags but could not manage to implement.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

枫林﹌晚霞¤ 2024-09-03 06:40:32

将以下类型的项目插入到 QTableWidget 中:

QTableWidgetItem *item = new QTableWidgetItem();
item->setFlags(Qt::ItemIsSelectable|Qt::ItemIsEnabled);

工作正常!

编辑:

QTableWidgetItem *item = new QTableWidgetItem();
item->setFlags(item->flags() ^ Qt::ItemIsEditable);

这是一个更好的解决方案。感谢@priomsrb。

Insert into the QTableWidget following kind of items:

QTableWidgetItem *item = new QTableWidgetItem();
item->setFlags(Qt::ItemIsSelectable|Qt::ItemIsEnabled);

Works fine!

EDIT:

QTableWidgetItem *item = new QTableWidgetItem();
item->setFlags(item->flags() ^ Qt::ItemIsEditable);

This is a better solution. Thanks to @priomsrb.

浊酒尽余欢 2024-09-03 06:40:32

使用 XOR 的结果取决于当前状态。我建议使用

item->setFlags(item->flags() &  ~Qt::ItemIsEditable);

来确保编辑已关闭,无论当前设置如何。

The result of using XOR depends on what the current state is. I'd suggest using

item->setFlags(item->flags() &  ~Qt::ItemIsEditable);

to make sure editing is turned off regardless of the current setting.

热风软妹 2024-09-03 06:40:32

我提出了一个更好的建议,只需用新的 SLOT 覆盖 cellDoubleClicked 信号即可。
这是,如果您不想修改任何单元格

I came to a better suggestion, just overwrite the cellDoubleClicked signal with a new SLOT.
This is, if you want none of the cells to be modified

寄离 2024-09-03 06:40:32

要将 @Narek 的代码应用于行或列,只需使用一个简单的 for 循环并放入一个条件以包含您不希望可编辑的行/列的标志。

以下代码将 csv 文件读取到 QTableWidget 中:

if(!rowOfData.isEmpty()){
for (int x = 0; x < rowOfData.size(); x++)
    {
        rowData = rowOfData.at(x).split(",");
        if(ui->table_Data->rowCount() <= x) ui->table_Data->insertRow(x);
        for (int y = 0; y < rowData.size(); y++)
        {
            QTableWidgetItem *item = new QTableWidgetItem(rowData[y],QTableWidgetItem::Type);
            if( y < 3 ) item->setFlags(item->flags() ^ Qt::ItemIsEditable);   // Only disables the first three columns for editing, but allows the rest of the columns to be edited
            ui->table_Data->setItem(x,y,item);
            ui->table_Data->repaint();
        }
    }
}

To apply @Narek's code to rows or columns, simply use a simple for loop and put a condition in to include the flags for rows/columns you do not want to be editable.

The following code reads a csv file into a QTableWidget:

if(!rowOfData.isEmpty()){
for (int x = 0; x < rowOfData.size(); x++)
    {
        rowData = rowOfData.at(x).split(",");
        if(ui->table_Data->rowCount() <= x) ui->table_Data->insertRow(x);
        for (int y = 0; y < rowData.size(); y++)
        {
            QTableWidgetItem *item = new QTableWidgetItem(rowData[y],QTableWidgetItem::Type);
            if( y < 3 ) item->setFlags(item->flags() ^ Qt::ItemIsEditable);   // Only disables the first three columns for editing, but allows the rest of the columns to be edited
            ui->table_Data->setItem(x,y,item);
            ui->table_Data->repaint();
        }
    }
}
清晰传感 2024-09-03 06:40:32

使用它使整个小部件只读:

tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);

Use this to make the whole widget read only:

tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文