简体   繁体   English

QStyledItemDelegate部分选择默认QLineEdit编辑器的文本

[英]QStyledItemDelegate partially select text of default QLineEdit editor

I have a subclass of QStyledItemDelegate which at the moment does not reimplement any functions (for simplicity of the question). 我有一个QStyledItemDelegate的子类,该子类目前不重新实现任何功能(为简化问题)。

With default QStyledItemDelegate implementation, when the user begins to edit text in a QTableView , the delegate draws a QLineEdit with the text from the model, and selects all of it (highlights all for editing). 使用默认的QStyledItemDelegate实现,当用户开始在QTableView编辑文本时,委托将使用模型中的文本绘制QLineEdit并将其全部选中(突出显示所有内容以进行编辑)。

The text represents file names such as "document.pdf". 文本代表文件名,例如“ document.pdf”。 The user is allowed to edit this entire text, however, I only want to initially highlight the base name portion ("document") and not the suffix ("pdf"). 允许用户编辑整个文本,但是,我只想首先突出显示基本名称部分(“文档”)而不是后缀(“ pdf”)。 How can I do this? 我怎样才能做到这一点? (I don't need the logic of how to do this, I need to know how to get the QStyledItemDelegate to highlight a portion of text) (我不需要执行此操作的逻辑,我需要知道如何获取QStyledItemDelegate来突出显示文本的一部分)

I've tried: 我试过了:

Please help, and thanks in advance. 请帮助,并在此先感谢。 A code snippet with say selecting the first 3 characters of text would be greatly appreciated. 例如,选择文本的前三个字符的代码片段将不胜感激。

As noted in my comments to the question, the problem with subclassing QStyledItemDelegate and trying to set any default selection in setEditorData like this: 如我对问题的评论中所述,将QStyledItemDelegate子类化并尝试在setEditorData设置任何默认选择的问题如下:

void setEditorData(QWidget* editor, const QModelIndex &index)const{
    QStyledItemDelegate::setEditorData(editor, index);
    if(index.column() == 0){ //the column with file names in it
        //try to cast the default editor to QLineEdit
        QLineEdit* le= qobject_cast<QLineEdit*>(editor);
        if(le){
            //set default selection in the line edit
            int lastDotIndex= le->text().lastIndexOf("."); 
            le->setSelection(0,lastDotIndex);
        }
    }
}

is that (in Qt code) after the view calls our setEditorData here , it tries to call selectAll() here when the editor widget is a QLineEdit . 是(在Qt代码)视图调用我们的后setEditorData 在这里 ,它试图调用selectAll() 这里当编辑器部件是QLineEdit That means that whatever selection we provide in setEditorData will be changed afterwards. 这意味着我们在setEditorData提供的任何选择都将在之后更改。

The only solution I could come up with, was to provide our selection in a queued manner. 我唯一能想到的解决方案是以排队方式提供我们的选择。 So that, our selection is set when execution is back into the event loop. 因此,当执行返回到事件循环时,将设置我们的选择。 Here is working example: 这是工作示例:

截图

#include <QApplication>
#include <QtWidgets>

class FileNameDelegate : public QStyledItemDelegate{
public:
    explicit FileNameDelegate(QObject* parent= nullptr)
        :QStyledItemDelegate(parent){}
    ~FileNameDelegate(){}

    void setEditorData(QWidget* editor, const QModelIndex &index)const{
        QStyledItemDelegate::setEditorData(editor, index);
        //the column with file names in it
        if(index.column() == 0){
            //try to cast the default editor to QLineEdit
            QLineEdit* le= qobject_cast<QLineEdit*>(editor);
            if(le){
                QObject src;
                //the lambda function is executed using a queued connection
                connect(&src, &QObject::destroyed, le, [le](){
                    //set default selection in the line edit
                    int lastDotIndex= le->text().lastIndexOf(".");
                    le->setSelection(0,lastDotIndex);
                }, Qt::QueuedConnection);
            }
        }
    }
};

//Demo program

int main(int argc, char** argv){
    QApplication a(argc, argv);

    QStandardItemModel model;
    QList<QStandardItem*> row;
    QStandardItem item("document.pdf");
    row.append(&item);
    model.appendRow(row);
    FileNameDelegate delegate;
    QTableView tableView;
    tableView.setModel(&model);
    tableView.setItemDelegate(&delegate);
    tableView.show();

    return a.exec();
}

This may sound like a hack, but I decided to write this until someone has a better approach to the problem. 这听起来像是一个hack,但是我决定写这篇文章,直到有人对这个问题有了更好的解决办法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM