Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Fine-tune initial editor state after QTableWidget::edit?

Fine-tune initial editor state after QTableWidget::edit?

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 3 Posters 708 Views 3 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • NathanielN Offline
    NathanielN Offline
    Nathaniel
    wrote on last edited by
    #1

    Suppose I programmatically set the text in a QTableWidget cell with a particular QModelIndex, then call edit() on this index, making the cell editable. If I understand correctly, QTableWidget creates a temporary QLineEdit for the editing but there is no direct way to access this widget (I don't want to bother with delegates or QTableView). I don't want to substantially change the behavior of the QLineEdit but I'd like to tweak its default state: it seems that the editor always starts with its contents selected and the cursor at the end. I'd like to manually position the cursor (say, just before the programmatically added text) and unselect everything. Is this doable? And is there a good reason why edit() defaults to selecting the whole cell contents? I can see scenarios where this is desired as default behavior but there are others when a cell may be edited repeatedly with text added more often than entirely replacing the previous text, so I'm hoping there's some flag to "turn off" selecting in this case.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      AFAIK, the simplest way to do that is with a minimal QStyledItemDelegate. There's not much code to implement for what you want so it will likely be cleaner and quicker to do.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      3
      • mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by VRonin
        #3

        Hi
        For a quick test i did

        #include <QStyledItemDelegate>
        class LineEditDelegate : public QStyledItemDelegate
        {
            Q_OBJECT
        
        public:
            LineEditDelegate(QObject *parent)
                : QStyledItemDelegate(parent) {}
        
            QWidget *createEditor(QWidget *parent,
                                  const QStyleOptionViewItem &/* option */,
                                  const QModelIndex &/* index */) const
            {
                QLineEdit *editor = new QLineEdit(parent);
                return editor;
            }
        
            void setEditorData(QWidget *editor, const QModelIndex &index) const
            {
                QVariant value = index.data(Qt::EditRole);
                QLineEdit *lineEdit= static_cast<QLineEdit *>(editor);
                lineEdit->setText(value.toString());
                lineEdit->deselect();
            } 
        
        };
        
        

        But it still select the text so i am wondering what did i miss ?

        1 Reply Last reply
        1
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          You can replace the deselect call by QTimer::singleShot(0, lineEdit, &QLineEdit::deselect);

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          2

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved