PDA

View Full Version : How to edit the text in the QListWidget on the run time, by "SINGLE" clicking them?



TheIndependentAquarius
23rd February 2012, 12:58
class genericTaskList : public QListWidget
{
Q_OBJECT
public:
unsigned int rowCounter;

genericTaskList (QWidget *parentWidget)
{
setParent (parentWidget);
setFixedSize (445, 445);

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

rowCounter = 0;
insertItem (rowCounter, defaultText);

QObject :: connect (this, SIGNAL (itemDoubleClicked (QListWidgetItem*)), this, SLOT (addDefaultText (QListWidgetItem*)));
QObject :: connect (this, SIGNAL (itemChanged (QListWidgetItem*)), this, SLOT (addDefaultText (QListWidgetItem*)));
}

public slots:
void addDefaultText (QListWidgetItem*f)
{
// Returns the current row number.
unsigned int currentRow = row (f);
// Returns the current row text.
QString textOfCurrentRow = f->text ();

// The new default row should get inserted if and only if, the last row created has been double clicked and its default text has been changed.
if ((currentRow == rowCounter)
&& (textOfCurrentRow.toStdString () != "Double click here to compose the new task.")
&& (textOfCurrentRow.toStdString () != ""))
{
++rowCounter;

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

insertItem (rowCounter, defaultText);
setCurrentRow (rowCounter);
}
else if (textOfCurrentRow.toStdString () == "")
{
takeItem (rowCounter);

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



The problem here is that I can edit the text if and only if I double click the text. Single click or anything else doesn't work. I tried changing that signal from double click to single click, didn't help.

Please guide - Double clicking all the time is a pain.

mentalmushroom
23rd February 2012, 13:15
Take a look at setEditTriggers(EditTriggers triggers) method

EditTriggers could be one of the following values:


QAbstractItemView::NoEditTriggers 0 No editing possible.
QAbstractItemView::CurrentChanged 1 Editing start whenever current item changes.
QAbstractItemView::DoubleClicked 2 Editing starts when an item is double clicked.
QAbstractItemView::SelectedClicked 4 Editing starts when clicking on an already selected item.
QAbstractItemView::EditKeyPressed 8 Editing starts when the platform edit key has been pressed over an item.
QAbstractItemView::AnyKeyPressed 16 Editing starts when any key is pressed over an item.
QAbstractItemView::AllEditTriggers 31 Editing starts for all above actions.

TheIndependentAquarius
3rd March 2012, 06:59
I'll try that and get back soon, thanks.