// delegate.h
#ifndef DELEGATE_H
#define DELEGATE_H
#include <QStyledItemDelegate>
#include <QComboBox>
#include <QLineEdit>
#include <QPushButton>
class Delegate : public QStyledItemDelegate
{
Q_OBJECT
public:
explicit Delegate(QObject *parent = nullptr);
QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const override;
void setEditorData(QWidget *editor, const QModelIndex &index) const override;
void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const override;
void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const override;
private slots:
void comboBoxIndexChanged(int index) const;
void pushButtonClicked() const;
};
#endif // DELEGATE_H
// delegate.cpp
#include "delegate.h"
Delegate::Delegate(QObject *parent)
: QStyledItemDelegate(parent)
{
}
QWidget *Delegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
if (index.column() == 1) {
QCheckBox *checkBox = new QCheckBox(parent);
return checkBox;
} else if (index.column() == 2) {
QComboBox *comboBox = new QComboBox(parent);
comboBox->addItem("Option 1");
comboBox->addItem("Option 2");
connect(comboBox, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &Delegate::comboBoxIndexChanged);
return comboBox;
} else if (index.column() == 3) {
QLineEdit *lineEdit = new QLineEdit(parent);
return lineEdit;
} else if (index.column() == 4) {
QPushButton *pushButton = new QPushButton("Click Me", parent);
connect(pushButton, &QPushButton::clicked, this, &Delegate::pushButtonClicked);
return pushButton;
} else {
return QStyledItemDelegate::createEditor(parent, option, index);
}
}
void Delegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
if (QCheckBox *checkBox = qobject_cast<QCheckBox *>(editor)) {
checkBox->setChecked(index.data(Qt::EditRole).toBool());
} else if (QComboBox *comboBox = qobject_cast<QComboBox *>(editor)) {
int idx = comboBox->findText(index.data(Qt::EditRole).toString());
comboBox->setCurrentIndex(idx);
} else if (QLineEdit *lineEdit = qobject_cast<QLineEdit *>(editor)) {
lineEdit->setText(index.data(Qt::EditRole).toString());
} else {
QStyledItemDelegate::setEditorData(editor, index);
}
}
void Delegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{
if (QCheckBox *checkBox = qobject_cast<QCheckBox *>(editor)) {
model->setData(index, checkBox->isChecked(), Qt::EditRole);
} else if (QComboBox *comboBox = qobject_cast<QComboBox *>(editor)) {
model->setData(index, comboBox->currentText(), Qt::EditRole);
} else if (QLineEdit *lineEdit = qobject_cast<QLineEdit *>(editor)) {
model->setData(index, lineEdit->text(), Qt::EditRole);
} else {
QStyledItemDelegate::setModelData(editor, model, index);
}
}
void Delegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
editor->setGeometry(option.rect);
}
void Delegate::comboBoxIndexChanged(int index) const
{
QComboBox *comboBox = qobject_cast<QComboBox *>(sender());
// 处理QComboBox控件的索引变化
}
void Delegate::pushButtonClicked() const
{
QPushButton *pushButton = qobject_cast<QPushButton *>(sender());
// 处理QPushButton控件的点击事件
}
// mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QTableView>
#include <QStandardItemModel>
#include <QPushButton>
#include <QVBoxLayout>
#include "delegate.h"
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private slots:
void addRow();
private:
QTableView *tableView;
QStandardItemModel *model;
QPushButton *addButton;
Delegate *delegate;
};
#endif // MAINWINDOW_H
// mainwindow.cpp
#include "mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
tableView = new QTableView(this);
model = new QStandardItemModel(this);
model->setColumnCount(5);
model->setHorizontalHeaderItem(0, new QStandardItem("Index"));
model->setHorizontalHeaderItem(1, new QStandardItem("Checkbox"));
model->setHorizontalHeaderItem(2, new QStandardItem("ComboBox"));
model->setHorizontalHeaderItem(3, new QStandardItem("LineEdit"));
model->setHorizontalHeaderItem(4, new QStandardItem("PushButton"));
tableView->setModel(model);
delegate = new Delegate(this);
tableView->setItemDelegate(delegate);
addButton = new QPushButton("Add Row", this);
connect(addButton, &QPushButton::clicked, this, &MainWindow::addRow);
QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(tableView);
layout->addWidget(addButton);
QWidget *centralWidget = new QWidget(this);
centralWidget->setLayout(layout);
setCentralWidget(centralWidget);
}
MainWindow::~MainWindow()
{
}
void MainWindow::addRow()
{
int row = model->rowCount();
model->insertRow(row);
model->setData(model->index(row, 0), row + 1);
}
在界面布局中添加Qtableview控件,其中Qtableview控件做如下设置:
1.在第一列显示数字,第二列委托checkbox控件,第三列委托增加Qcombox控件,第四列委托Qlineedit控件,第五列委托是Qpushbutton控件 要求:
1.保证当前要上移或下移行中的控件在行上下移动过程中控件跟随当前行移动,被调换的行对应的控件也要跟随被调换的行移动且不会消失,其次保证当前要上移或下移行中的控件在行上下移动过程中控件跟随当前行移动,这个不是鼠标拖动而是点击上移按钮对应选中行上移,点击下移按钮对应选行中行下移
2.当每次点击Qpushbutton控件中会获取当前行中对应Qcombox控件中当前的选用的内容并发送内容当另外一个界面
3.在第一列显示数字,第一行为1,第二行为2,依次完成,当行变换时,该数字随当前行一起移动,
4.第二列委托checkbox控件,当该控件勾选后则控件所在行的第三列Qcombox控件/第四列Qlineedit控件的处于激活状态,当未勾选则第三列Qcombox控件/第四列Qlineedit控件的处于未激活状态;
追加:
// delegate.cpp
#include "delegate.h"
#include <QComboBox>
#include <QLineEdit>
#include <QCheckBox>
#include <QPushButton>
Delegate::Delegate(QObject *parent) : QItemDelegate(parent)
{
}
QWidget *Delegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
if (index.column() == 1) {
QCheckBox *checkBox = new QCheckBox(parent);
return checkBox;
} else if (index.column() == 2) {
QComboBox *comboBox = new QComboBox(parent);
// 从XML文件中获取数据并添加到comboBox中
// ...
return comboBox;
} else if (index.column() == 3) {
QLineEdit *lineEdit = new QLineEdit(parent);
return lineEdit;
} else if (index.column() == 4) {
QPushButton *pushButton = new QPushButton("Send", parent);
// 连接按钮的点击信号到槽函数
// ...
return pushButton;
} else {
return QItemDelegate::createEditor(parent, option, index);
}
}
void Delegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
if (QCheckBox *checkBox = qobject_cast<QCheckBox *>(editor)) {
// 设置checkBox的状态
// ...
} else if (QComboBox *comboBox = qobject_cast<QComboBox *>(editor)) {
// 设置comboBox的内容
// ...
} else if (QLineEdit *lineEdit = qobject_cast<QLineEdit *>(editor)) {
// 设置lineEdit的内容
// ...
}
}
void Delegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{
if (QCheckBox *checkBox = qobject_cast<QCheckBox *>(editor)) {
// 获取checkBox的状态并更新模型数据
// ...
} else if (QComboBox *comboBox = qobject_cast<QComboBox *>(editor)) {
// 获取comboBox的内容并更新模型数据
// ...
} else if (QLineEdit *lineEdit = qobject_cast<QLineEdit *>(editor)) {
// 获取lineEdit的内容并更新模型数据
// ...
}
}
void Delegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
editor->setGeometry(option.rect);
}