C++ 根据QComboBox的选择更改QTableView中的数据

C++ 根据QComboBox的选择更改QTableView中的数据,c++,qt,model-view,C++,Qt,Model View,我在QTableView的一列中有一个QComboBox。如何根据在组合框中选择的内容更改其他列?我将使用QComboBox作为代表 至少有两种方法 对Qt的型号itemChanged信号使用natural 从代理发出信号,并在主窗口内捕捉它 如果您的委托是标准的,这意味着在setModelData()方法中,您有如下内容: QComboBox *line = static_cast<QComboBox*>(editor); QString data = line->cur

我在
QTableView
的一列中有一个
QComboBox
。如何根据在
组合框中选择的内容更改其他列?我将使用
QComboBox
作为代表

至少有两种方法

  • 对Qt的型号
    itemChanged
    信号使用natural
  • 从代理发出信号,并在主窗口内捕捉它
如果您的委托是标准的,这意味着在
setModelData()
方法中,您有如下内容:

QComboBox *line = static_cast<QComboBox*>(editor);
QString data = line->currentText();
//...
model->setData(index, data);
createEditor()中创建连接
方法:

QWidget *ItemDelegate::createEditor(QWidget *parent,
                                    const QStyleOptionViewItem &option,
                                    const QModelIndex &index) const
{
    QComboBox *editor = new QComboBox(parent);
    connect(editor,SIGNAL(currentIndexChanged(QString)),this,SIGNAL(boxDataChanged(QString)));
    return editor;
}
并使用它

ItemDelegate *del = new ItemDelegate;
ui->tableView->setItemDelegate( del);
ui->tableView->setModel(model);
    connect(del,&ItemDelegate::boxDataChanged,[=](const QString & str) {
            //you found, just get data and use it as you want
            qDebug() << str;
    });
itemregate*del=新的itemregate;
ui->tableView->setItemDelegate(del);
ui->tableView->setModel(模型);
连接(del,&itemlegate::boxDataChanged,[=](const QString&str){
//你发现了,只需获取数据,并根据需要使用它

qDebug()至少有两种方法

  • 对Qt的型号
    itemChanged
    信号使用natural
  • 从代理发出信号,并在主窗口内捕捉它
如果您的委托是标准的,这意味着在
setModelData()
方法中,您有如下内容:

QComboBox *line = static_cast<QComboBox*>(editor);
QString data = line->currentText();
//...
model->setData(index, data);
createEditor()中创建连接
方法:

QWidget *ItemDelegate::createEditor(QWidget *parent,
                                    const QStyleOptionViewItem &option,
                                    const QModelIndex &index) const
{
    QComboBox *editor = new QComboBox(parent);
    connect(editor,SIGNAL(currentIndexChanged(QString)),this,SIGNAL(boxDataChanged(QString)));
    return editor;
}
并使用它

ItemDelegate *del = new ItemDelegate;
ui->tableView->setItemDelegate( del);
ui->tableView->setModel(model);
    connect(del,&ItemDelegate::boxDataChanged,[=](const QString & str) {
            //you found, just get data and use it as you want
            qDebug() << str;
    });
itemregate*del=新的itemregate;
ui->tableView->setItemDelegate(del);
ui->tableView->setModel(模型);
连接(del,&itemlegate::boxDataChanged,[=](const QString&str){
//你发现了,只需获取数据,并根据需要使用它

qDebug()至少有两种方法

  • 对Qt的型号
    itemChanged
    信号使用natural
  • 从代理发出信号,并在主窗口内捕捉它
如果您的委托是标准的,这意味着在
setModelData()
方法中,您有如下内容:

QComboBox *line = static_cast<QComboBox*>(editor);
QString data = line->currentText();
//...
model->setData(index, data);
createEditor()中创建连接
方法:

QWidget *ItemDelegate::createEditor(QWidget *parent,
                                    const QStyleOptionViewItem &option,
                                    const QModelIndex &index) const
{
    QComboBox *editor = new QComboBox(parent);
    connect(editor,SIGNAL(currentIndexChanged(QString)),this,SIGNAL(boxDataChanged(QString)));
    return editor;
}
并使用它

ItemDelegate *del = new ItemDelegate;
ui->tableView->setItemDelegate( del);
ui->tableView->setModel(model);
    connect(del,&ItemDelegate::boxDataChanged,[=](const QString & str) {
            //you found, just get data and use it as you want
            qDebug() << str;
    });
itemregate*del=新的itemregate;
ui->tableView->setItemDelegate(del);
ui->tableView->setModel(模型);
连接(del,&itemlegate::boxDataChanged,[=](const QString&str){
//你发现了,只需获取数据,并根据需要使用它

qDebug()至少有两种方法

  • 对Qt的型号
    itemChanged
    信号使用natural
  • 从代理发出信号,并在主窗口内捕捉它
如果您的委托是标准的,这意味着在
setModelData()
方法中,您有如下内容:

QComboBox *line = static_cast<QComboBox*>(editor);
QString data = line->currentText();
//...
model->setData(index, data);
createEditor()中创建连接
方法:

QWidget *ItemDelegate::createEditor(QWidget *parent,
                                    const QStyleOptionViewItem &option,
                                    const QModelIndex &index) const
{
    QComboBox *editor = new QComboBox(parent);
    connect(editor,SIGNAL(currentIndexChanged(QString)),this,SIGNAL(boxDataChanged(QString)));
    return editor;
}
并使用它

ItemDelegate *del = new ItemDelegate;
ui->tableView->setItemDelegate( del);
ui->tableView->setModel(model);
    connect(del,&ItemDelegate::boxDataChanged,[=](const QString & str) {
            //you found, just get data and use it as you want
            qDebug() << str;
    });
itemregate*del=新的itemregate;
ui->tableView->setItemDelegate(del);
ui->tableView->setModel(模型);
连接(del,&itemlegate::boxDataChanged,[=](const QString&str){
//你发现了,只需获取数据,并根据需要使用它

qDebug()要创建更多信息,请回答您使用的是哪种模型?QStandardItemModel还是您自己的自定义模型?@Chernobyl:QStandardItemModel。我已经复制了您的代码(使用组合框委派)如果我在combobox中选择某个项目并通过单击enter确认,则我的解决方案会起作用。但如果您希望获得数据将自动更改的解决方案,则在combobox中选择另一个项目时(无需按enter键)然后请告诉我,我会编辑我的答案。不幸的是,接下来的几个小时我将离线,但我不想留下我可能是半有用的答案给你,所以我决定写第二个案例,它更长,更复杂,但我测试了它,它有效,告诉我你是否会对此有一些问题。要创建更多信息的答案:d请问您使用的是哪种型号?QStandardItemModel还是您自己的定制型号?@Chernobyl:QStandardItemModel。我已经复制了您的代码(用组合框委托)如果我在combobox中选择某个项目并通过单击enter确认,则我的解决方案会起作用。但如果您希望获得数据将自动更改的解决方案,则在combobox中选择另一个项目时(无需按enter键)然后请告诉我,我会编辑我的答案。不幸的是,接下来的几个小时我将离线,但我不想留下我可能是半有用的答案给你,所以我决定写第二个案例,它更长,更复杂,但我测试了它,它有效,告诉我你是否会对此有一些问题。要创建更多信息的答案:d请问您使用的是哪种型号?QStandardItemModel还是您自己的定制型号?@Chernobyl:QStandardItemModel。我已经复制了您的代码(用组合框委托)如果我在combobox中选择某个项目并通过单击enter确认,则我的解决方案会起作用。但如果您希望获得数据将自动更改的解决方案,则在combobox中选择另一个项目时(无需按enter键)然后请告诉我,我会编辑我的答案。不幸的是,接下来的几个小时我将离线,但我不想留下我可能是半有用的答案给你,所以我决定写第二个案例,它更长,更复杂,但我测试了它,它有效,告诉我你是否会对此有一些问题。要创建更多信息的答案:d请问您使用的是哪种型号?QStandardItemModel还是您自己的定制型号?@Chernobyl:QStandardItemModel。我已经复制了您的代码(用组合框委托)如果我在combobox中选择某个项目并通过单击enter确认,则我的解决方案会起作用。但如果您希望获得数据将自动更改的解决方案,则在combobox中选择另一个项目时(无需按enter键)然后请告诉我,我会编辑我的答案。不幸的是,接下来的几个小时我将离线,但我不想留下我可能是半有用的答案给你,所以我决定写第二个案例,它更长,更复杂,但我测试了它,它是有效的,告诉我你是否需要