简体   繁体   English

如何使用 QLineEdit 将 QString 添加到 QListView

[英]How to add a QString to a QListView using a QLineEdit

I want to use a QLineEdit to write a QString, then using a QPushButton, I want to add an item (a string) to a listView我想使用 QLineEdit 编写 QString,然后使用 QPushButton,我想将一个项目(字符串)添加到 listView

Here is what I got:这是我得到的:

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);

    model = new QStringListModel(this);

    QStringList list;

    list << "Some Item";

    model->setStringList(list);

    ui->listView->setModel(model);
    ui->listView->setEditTriggers(QAbstractItemView::NoEditTriggers);
}

void MainWindow::on_pushButton_3_clicked()
{
    //add
    int row = model->rowCount();   // model = new QStringListModel
    model->insertRow(row);

    QModelIndex index = model->index(row);
    ui->listView->setCurrentIndex(index);
    ui->listView->edit(index);  // instead of edit, I'd like to ... add a QString
}

Problem is I don't want to be able to edit (this was all I was manage to do by myself).问题是我不想编辑(这是我自己设法做到的)。 I want now to instead add an item at CurrentIndex, and that item to be the text field of lineEdit .我现在想改为在 CurrentIndex 处添加一个项目,并将该项目作为lineEdit的文本字段。 How do I get access to that field?我如何获得该字段的访问权限? is it lineEdit->text() ?lineEdit->text()吗? and how do I add that to the list view?以及如何将其添加到列表视图中?

I simply do not know how to add anything to a list.我根本不知道如何向列表中添加任何内容。 I found edit by mistake, and google has not helped so far.我错误地找到了编辑,到目前为止谷歌没有帮助。 I'm hoping Stack Overflow can, and will.我希望 Stack Overflow 能够而且将会这样做。

EDIT: I have decided to try this, but it doesn't seem to be the best solution编辑:我决定试试这个,但它似乎不是最好的解决方案

void MainWindow::on_pushButton_3_clicked()
{
    //add

    QStringList list;
    list = model->stringList();
    list.append(ui->lineEdit->text());
    model->setStringList(list);
}

But this seems to be an odd way of doing things.但这似乎是一种奇怪的做事方式。 Also it seems to include a newline for some reason.由于某种原因,它似乎还包含一个换行符。

There is already an example of how to use a QStringListModel here: https://stackoverflow.com/a/5825113/496445这里已经有一个如何使用 QStringListModel 的例子: https ://stackoverflow.com/a/5825113/496445

model->insertRow(model->rowCount());
QModelIndex index = model->index(model->rowCount()-1)
model->setData(index, str);

Note that in this suggested approach, you do not need the QStringList, unless you already had one for another reason and want to initialize with it.请注意,在这个建议的方法中,您不需要 QStringList,除非您出于其他原因已经有了一个 QStringList 并且想用它进行初始化。

When you use a Q*View instead of a Widget, you will be dealing with the model directly for data instead of the view.当您使用Q*View而不是 Widget 时,您将直接为数据而不是视图处理模型。 The view will be notified when the model changes.模型更改时将通知视图。 In this case, you would probably be accessing your lineEdit like this:在这种情况下,您可能会像这样访问您的 lineEdit:

QString str = ui->lineEdit->text();

Another way;其它的办法; right-click to listView and select "morph into" -> "QListWidget"右键单击 listView 并选择“morph into” -> “QListWidget”

At this time you can see this function "lst-> addItem ("str");"这时候可以看到这个函数“lst-> addItem ("str");”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM