向QTableView插入数据c++
时间: 2023-12-29 18:54:11 浏览: 39
可以使用QStandardItemModel作为QTableView的数据模型,然后向模型中插入数据。以下是一个简单的例子:
```cpp
QStandardItemModel *model = new QStandardItemModel(0, 2); // 创建模型,0行2列
model->setHeaderData(0, Qt::Horizontal, "姓名"); // 设置表头
model->setHeaderData(1, Qt::Horizontal, "年龄");
QList<QStandardItem *> row; // 创建一行数据
row << new QStandardItem("张三") << new QStandardItem("18"); // 添加数据
model->appendRow(row); // 将行数据添加到模型中
ui->tableView->setModel(model); // 设置模型
```
在上面的例子中,我们创建了一个包含两列的QStandardItemModel,然后设置了表头。接着,我们创建了一行数据,并将数据添加到这一行中。最后,通过调用appendRow()函数,将这一行数据添加到模型中。最后,我们将模型设置为QTableView的数据模型。
相关问题
C++代码实现Qtableview 插入一列Qradiobutton
要在QTableView中的特定列中插入QRadioButton,您需要使用QItemDelegate。以下是一个示例代码,其中第2列中的每个单元格都包含一个QRadioButton:
```cpp
#include <QApplication>
#include <QMainWindow>
#include <QStandardItemModel>
#include <QTableView>
#include <QItemDelegate>
#include <QRadioButton>
#include <QHBoxLayout>
class RadioButtonDelegate : public QItemDelegate
{
public:
RadioButtonDelegate(QObject *parent = nullptr) : QItemDelegate(parent) {}
QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const override
{
if (index.column() == 1) // Only create editor for second column
{
QWidget *editor = new QWidget(parent);
QHBoxLayout *layout = new QHBoxLayout(editor);
layout->setContentsMargins(0, 0, 0, 0);
layout->addWidget(new QRadioButton("Option 1", editor));
editor->setLayout(layout);
return editor;
}
else
{
return QItemDelegate::createEditor(parent, option, index);
}
}
void setEditorData(QWidget *editor, const QModelIndex &index) const override
{
if (index.column() == 1) // Only set data for second column
{
QRadioButton *radioButton = qobject_cast<QRadioButton *>(editor->layout()->itemAt(0)->widget());
radioButton->setChecked(index.data().toBool());
}
else
{
QItemDelegate::setEditorData(editor, index);
}
}
void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const override
{
if (index.column() == 1) // Only set data for second column
{
QRadioButton *radioButton = qobject_cast<QRadioButton *>(editor->layout()->itemAt(0)->widget());
model->setData(index, radioButton->isChecked());
}
else
{
QItemDelegate::setModelData(editor, model, index);
}
}
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QStandardItemModel model(4, 2); // 4 rows, 2 columns
model.setHeaderData(0, Qt::Horizontal, "Column 1");
model.setHeaderData(1, Qt::Horizontal, "Column 2");
QTableView tableView;
tableView.setModel(&model);
tableView.setItemDelegate(new RadioButtonDelegate(&tableView));
QMainWindow mainWindow;
mainWindow.setCentralWidget(&tableView);
mainWindow.show();
return a.exec();
}
```
在这个例子中,我们创建了一个名为`RadioButtonDelegate`的自定义QItemDelegate。在`createEditor`函数中,我们创建一个QWidget,将QRadioButton添加到水平布局中,并将其设置为QWidget的布局。然后,我们返回QWidget作为编辑器。在`setEditorData`函数中,我们设置QRadioButton的选中状态,以匹配模型中的数据。在`setModelData`函数中,我们将QRadioButton的选中状态设置为模型中的数据。
在主函数中,我们创建了一个QStandardItemModel,设置了表头,并将其设置为QTableView的模型。我们还将RadioButtonDelegate设置为QTableView的项目委托。
运行程序后,您应该看到一个具有两列和四行的表格,其中第二列包含一个QRadioButton。您可以单击不同的单选按钮,并且模型中的数据应该相应地更新。
QTableView右键添加菜单栏,点击添加数据在QtableView末端插入一行数据,删除QtableView当前选中行,用C++回答
您可以通过以下步骤在QTableView上添加右键菜单栏,并实现在末尾插入一行数据和删除当前选中行的功能:
1. 创建右键菜单栏:
在QTableView的构造函数或初始化函数中,创建一个QMenu对象作为右键菜单栏,并添加"添加数据"和"删除行"的QAction项。
```cpp
QTableView* tableView = new QTableView(this);
QMenu* menu = new QMenu(tableView);
QAction* addAction = new QAction("添加数据", this);
QAction* deleteAction = new QAction("删除行", this);
menu->addAction(addAction);
menu->addAction(deleteAction);
tableView->setContextMenuPolicy(Qt::CustomContextMenu);
tableView->setContextMenu(menu);
```
2. 连接右键菜单的信号槽:
使用customContextMenuRequested信号连接到一个槽函数,以便在用户右键点击时显示菜单栏。
```cpp
connect(tableView, &QTableView::customContextMenuRequested, this, [=](const QPoint& pos) {
menu->exec(tableView->viewport()->mapToGlobal(pos));
});
```
3. 实现插入一行数据的功能:
在槽函数中,通过调用QTableView的model()函数获取数据模型,并使用insertRow()方法在末尾插入一行空数据。
```cpp
connect(addAction, &QAction::triggered, this, [=]() {
QAbstractItemModel* model = tableView->model();
model->insertRow(model->rowCount());
});
```
4. 实现删除当前选中行的功能:
在槽函数中,通过调用QTableView的selectionModel()函数获取选择模型,并使用selectedRows()方法获取当前选中的行号,然后使用removeRow()方法删除这些行。
```cpp
connect(deleteAction, &QAction::triggered, this, [=]() {
QItemSelectionModel* selectionModel = tableView->selectionModel();
QModelIndexList selectedRows = selectionModel->selectedRows();
if (!selectedRows.isEmpty()) {
QAbstractItemModel* model = tableView->model();
for (const QModelIndex& index : selectedRows) {
model->removeRow(index.row());
}
}
});
```
这样,您就可以在QTableView上右键点击,选择添加数据来在末尾插入一行数据,或者选择删除行来删除当前选中的行。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)