C++ 如何使用 QLineEdit 将 QString 添加到 QListView

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11171123/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 14:54:46  来源:igfitidea点击:

How to add a QString to a QListView using a QLineEdit

c++qtlistviewqt4

提问by Kalec

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. How do I get access to that field ? is it lineEdit->text()? and how do I add that to the list view ?

问题是我不想能够编辑(这是我自己设法做到的)。我现在想在 CurrentIndex 中添加一个项目,该项目是lineEdit. 我如何访问该字段?是lineEdit->text()吗?以及如何将其添加到列表视图中?

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.

但这似乎是一种奇怪的做事方式。此外,由于某种原因,它似乎还包含一个换行符。

回答by jdi

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,除非您出于其他原因已经拥有一个并希望使用它进行初始化。

When you use a Q*Viewinstead of a Widget, you will be dealing with the model directly for data instead of the view. The view will be notified when the model changes. In this case, you would probably be accessing your lineEdit like this:

当您使用 aQ*View而不是 Widget 时,您将直接为数据而不是视图处理模型。当模型改变时,视图会得到通知。在这种情况下,您可能会像这样访问 lineEdit:

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

回答by Tuncay Ince

Another way; right-click to listView and select "morph into" -> "QListView"

其它的办法; 右键单击 listView 并选择“变形为”->“QListView”

At this time you can see this function "lst->addItem("str");"

这时候可以看到这个函数"lst-> addItem("str");"