gpt4 book ai didi

c++ - 我如何只使一个 QComboBox 项目可编辑?

转载 作者:行者123 更新时间:2023-11-28 04:13:33 25 4
gpt4 key购买 nike

我的 Qt 应用程序中有 QComboBox,我希望它有一些默认的、不可编辑的项目,但我也希望它有一个可编辑的项目,它有默认文本,但被替换了一旦编辑并按下一些确认按钮(默认输入)。

这是我尝试过的:

QComboBox* combo_1 = new QComboBox();
combo_1->setEditable(true);
combo_1->addItems(QStringList()<<"Option_1."<<"Option_2."<<"Option_3."<<"Option_4."<<"Other...");

这样所有项目都是可编辑的,一旦我编辑任何项目并按回车键,它保持不变,但带有编辑文本的新项目被插入到框中。

我怎样才能实现我想要的行为?感谢您的帮助!

P.s. 我表示我的目标是只有一个可编辑的项目,但我也想知道如何以相同的方式插入无限(有条件)数量的新项目。

最佳答案

解决方案是对组合框使用模型 View 模式并子类化 QComboBox

1:实现自定义模型。在我的例子中,我在 row = 2 (QString m_strEditableValue) 中有可编辑项目,在 0,1 行中有固定项目。

class MyModel : public QAbstractItemModel
{
Q_OBJECT
public:
explicit MyModel(QObject *parent = nullptr);

QModelIndex index(int row, int column, const QModelIndex &parent) const;
QModelIndex parent(const QModelIndex &child) const;
int rowCount(const QModelIndex &parent) const;
int columnCount(const QModelIndex &parent) const;
QVariant data(const QModelIndex &index, int role) const;
bool setData(const QModelIndex &index, const QVariant &value, int role);
Qt::ItemFlags flags(const QModelIndex &index) const;

private:
QString m_strEditableValue;

};

MyModel::MyModel(QObject *parent) : QAbstractItemModel(parent)
{
m_strEditableValue = "default value";
}

QModelIndex MyModel::index(int row, int column, const QModelIndex &parent) const
{
return createIndex(row, column);
}

QModelIndex MyModel::parent(const QModelIndex &child) const
{
return QModelIndex();
}

int MyModel::rowCount(const QModelIndex &parent) const
{
return 3;
}

int MyModel::columnCount(const QModelIndex &parent) const
{
return 1;
}

QVariant MyModel::data(const QModelIndex &index, int role) const
{
if (role == Qt::DisplayRole || role == Qt::EditRole)
{
if (index.row() == 0) {
return tr("First fixed value");
}
if (index.row() == 1) {
return tr("Second fixed value");
}

if (index.row() == 2) {
return m_strEditableValue;
}
}

return QVariant();
}

bool MyModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
if (role == Qt::EditRole) {
if (index.row() == 2) {
m_strEditableValue = value.toString();
return true;
}
}

return false;
}

Qt::ItemFlags MyModel::flags(const QModelIndex &index) const
{
Qt::ItemFlags f = Qt::ItemIsSelectable | Qt::ItemIsEnabled;
if (index.row() == 2) {
//mark editable only for row 2
f = f | Qt::ItemIsEditable;
}
return f;
}

2:子类QComboBox改变标准行为

class MyCombobox : public QComboBox
{
Q_OBJECT
public:
explicit MyCombobox(QWidget *parent = nullptr);

private slots:
void OnEditTextChanged(const QString& text);
void OnCurrentIndexChanged(int index);

public slots:
};

MyCombobox::MyCombobox(QWidget *parent) : QComboBox(parent)
{
connect(this, &QComboBox::editTextChanged, this, &MyCombobox::OnEditTextChanged);
connect(this, static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged), this, &MyCombobox::OnCurrentIndexChanged);
}

void MyCombobox::OnEditTextChanged(const QString &text)
{
if (model()) {
//set data to model immediately
model()->setData(model()->index(currentIndex(), 0), text);
}
}

void MyCombobox::OnCurrentIndexChanged(int index)
{
if (model())
{
//disable editing if model disable it
Qt::ItemFlags flags = model()->flags(model()->index(index, 0));
if (flags & Qt::ItemIsEditable) {
lineEdit()->setReadOnly(false);
} else {
lineEdit()->setReadOnly(true);
}
}
}

3:使用

MyModel *cbModel = new MyModel(this);
ui->cbEditable->setModel(cbModel);

关于c++ - 我如何只使一个 QComboBox 项目可编辑?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57147242/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com