I have a QTextEdit , 2 QPushButtons (Add n Remove Buttons) and a QListView. When i am entering the text in the Text Edit and click the Add Button, the Text should be added in the List View. Then, if I select any one of the added text from the List View & Click the Remove Button, the Text should be removed from the ListView. I dont know how to achieve this. Plz Help me to solve this. Thanks in Advance.
Asked
Active
Viewed 4,601 times
2 Answers
2
Assuming you are using a QStandardItemModel and you have the following variables
QPushButton* addButton;
QPushButton* removeButton;
QTextEdit* textEdit;
QStandardItemModel* model;
MyObject* this;
the following code should do it:
connect(addButton, SIGNAL(clicked()), this, SLOT(onAddButtonClicked()));
connect(removeButton, SIGNAL(clicked()), this, SLOT(onRemoveButtonClicked()));
Then the two slots in your class MyObject you define do the following:
void MyObject::onAddButtonClicked() {
model->appendRow(new QStandardItem(textEdit->plainText());
}
void MyObject::onRemoveButtonClicked() {
if (model->rowCount() == 0)
return;
delete model->takeItem(model->rowCount() - 1);
}
Updating the view gets handled by QStandardItemModel

ar31
- 3,576
- 1
- 25
- 22
-
Hi.. Now i want to scroll this list widget items in a widget. How can i do that? plz help.. – New Moon Sep 13 '12 at 08:07
-
2I guess simply putting it into a [QScrollArea](http://qt-project.org/doc/qt-4.8/qscrollarea.html) should do (using `setWidget()`). Not sure if I understand the question correctly though. – ar31 Sep 13 '12 at 21:12
-
Hi.. If i use this void MyObject::onAddButtonClicked() { model->appendRow(new QStandardItemModel(textEdit->plainText()); } it throws me an error:'QStandardItemModel::QStandardItemModel(QObject *)' : cannot convert parameter 1 from 'QString' to 'QObject *' No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called. How can i solve this. – New Moon Sep 23 '12 at 08:16
-
Oops seems this is a typo in my answer. It should be `QStandardItem` instead – ar31 Sep 24 '12 at 18:45
1
if you don't know how to use model/view/controller pattern, I'd recommend you to use QListWidget instead of QListView. Adding to QListWidget is way to simplier. You should create a slot where goes the signal from add button click, and a slot for delete button click.
Code for first slot:
m_pListWidget->addItem( m_pTextEdit->toPlainText() );
Code for second slot:
if ( QListWidgetItem* plwiCurrent = m_pListWidget->currentItem() )
{
m_pListWidget->takeItem( m_pListWidget->row( plwiCurrent ) );
delete plwiCurrent;
}

Pie_Jesu
- 1,894
- 3
- 16
- 30
-
1Hi.. The code for Second Slot throws me an error: 'QListWidget::takeItem': Cannot convert parameter 1 from 'QListWidgetItem *' to 'int'. why this is happening? – New Moon Sep 12 '12 at 10:14
-
1Okay, my bad, changing the answer. Use `m_pListWidget->takeItem( m_pListWidget->row( plwiCurrent ) );` instead. – Pie_Jesu Sep 12 '12 at 10:59
-
Hi.. Now i want to scroll this list widget items in a widget. How can i do this? plz help... – New Moon Sep 13 '12 at 08:08
-
I guess I don't understand you. What do you mean by `scroll this list widget items in a widget`? – Pie_Jesu Sep 13 '12 at 08:11
-
when i'm clicking the addbutton the text in QTextEdit will be added in the QListView. i had did this using QStandardItemModel. Now I want this items to Scroll in a separate widget. eg. Hello World is my item in the QListView. This Hello World has to scroll from right to left in a separate Widget like a Ticker. – New Moon Sep 14 '12 at 22:35