View Full Version : How to add the text items directly to the QListWidget on the "run time"?

23rd February 2012, 08:28
How to add the text items directly to the QListWidget on the "run time"?

Is there a way to do it?

23rd February 2012, 08:40
See QListWidget Class Reference in Qt docs. The following methods should help you:

void addItem ( const QString & label )
void addItem ( QListWidgetItem * item )
void addItems ( const QStringList & labels )

23rd February 2012, 08:43
Thanks, I had seen those functions before too, but is it possible to use them of "run time"?

I mean, the addItem function "receives" text then pushes it in the list? But, I want to directly start typing in the list box.

23rd February 2012, 08:49
Perhaps, this helps

23rd February 2012, 09:28
That was indeed extremely helpful for "editing" the text on the fly.

Here is what I did exactly:

int main(int argc, char *argv[])
QApplication app (argc, argv);
QMainWindow *window = new QMainWindow ();
QWidget *centralWidget = new QWidget (window);

QListWidget *genericTodoList = new QListWidget (window);
genericTodoList->setFixedSize (445, 445);

QListWidgetItem *defaultText = new QListWidgetItem ("Double click here to compose the task");
defaultText->setFlags (defaultText->flags () | Qt::ItemIsEditable);
genericTodoList->insertItem (0, defaultText);

QGridLayout *layout = new QGridLayout();
centralWidget->setLayout (layout);
layout->addWidget (genericTodoList, 0, 0);

window->setCentralWidget (centralWidget);
return app.exec();