如何在运行时通过单击 QListWidget 中的文本来编辑它们?

发布于 2025-01-08 15:28:12 字数 2418 浏览 3 评论 0原文

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);
            }
        }
};

这里的问题是,当且仅当我双击文本时,我才能编辑文本。单击或其他任何操作都不起作用。我尝试将该信号从双击更改为单击,但没有帮助。

请指导 - 一直双击很痛苦。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情绪失控 2025-01-15 15:28:12

您可以使用QAbstractItemView::CurrentChanged作为列表小部件的编辑触发器。这意味着如果您单击某个项目,它将是可编辑的。不仅是单击它的情况,而且例如使用箭头键在项目之间切换时也是如此。不幸的是,没有“单击编辑”或类似的标志...

setEditTriggers( QAbstractItemView::CurrentChanged );

所有触发器概述

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文