Aquarius_Girl
Aquarius_Girl

Reputation: 22946

How to edit the text in the QListWidget on the run time by single clicking them?

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.

Upvotes: 1

Views: 4318

Answers (1)

Exa
Exa

Reputation: 4110

You can use QAbstractItemView::CurrentChanged as the edit trigger for your list widget. This means that if you click an item it will be editable. This is the case not only for clicking on it, but also for switching among the items with the arrow keys for example. Unfortunately there is no flag for "edit on single click" or something like that...

setEditTriggers( QAbstractItemView::CurrentChanged );

Overview of all triggers

Upvotes: 3

Related Questions