Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8159935
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T18:05:00+00:00 2026-06-06T18:05:00+00:00

I want to use a QLineEdit to write a QString, then using a QPushButton,

  • 0

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

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 ?

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.

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T18:05:02+00:00Added an answer on June 6, 2026 at 6:05 pm

    There is already an example of how to use a QStringListModel here: 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.

    When you use a Q*View instead 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:

    QString str = ui->lineEdit->text();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want use $.ajax get a url string from a xml file ,then with
I want use regex to find something in string (or QString) that is between
I have a easy select tag: <select> <option></option> <option></option> </select> Then I want use
Example I want use the_excerpt in two locations. On functions.php I add function new_excerpt_length($length)
I want use $.ajax to read some infomation from xml file,here is my js
i want use XmlSampleGenerator API in my project. i don't know how to add
My question here is why should i want use the entity framework instead of
i want use single quotations inside a transact sql statement, then execute that statement.
I want use MySQL database in R environment. I am using Ubuntu OS. To
I want use this 1 for using Bar code or QR code scanner. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.