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. How to avoid scrollbar and move cursur to the click position when editing the text in tableview?
Forum Updated to NodeBB v4.3 + New Features

How to avoid scrollbar and move cursur to the click position when editing the text in tableview?

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 2 Posters 405 Views 1 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.
  • P Offline
    P Offline
    PaulHuang
    wrote on last edited by
    #1

    I've tried to develop an excel like desktop application with Qt.

    I used the delegate and customized textedit in order to gain the excel-like effects by clicking directly and edit the characters.

    However, when I clicked into the cell, QT automatically add scrollbar to the cell. Is there a way to avoid it? I tried to manipulate the rect, but I cannot get the desired effects.

    First, I customize the updateEditorGeometry to gain the x and y pos in order to move the cursur position when the user click into the cell. But I failed to get the accurate position, always before or after the original click position.

    Code for Delegate

    void Tueditordelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
    {
        // Get the value via index of the Model
        QString value = index.model()->data(index, Qt::EditRole).toString();
    
        // Put the value into the SpinBox
        AlignTextEdit *textEditor = static_cast<AlignTextEdit*>(editor);
        textEditor->setPlainText(value);
        textEditor->moveCursorToPosition();
    
    }
    
    void Tueditordelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const
    {
    //    editor->setGeometry(option.rect);
        QRect rect = option.rect;
    //    rect.setY(rect.y() -4);
    //    rect.setX(rect.x() -1);
    ////    rect.setY(rect.y() -2);
    ////    rect.setWidth(rect.width() + 4);
    //    rect.setHeight(rect.height()+ rect.height()/8);
        editor->setGeometry(rect);
    
        AlignTextEdit  *plaineditor = static_cast<AlignTextEdit* >(editor);
       int x = editor->pos().x();
       int y = editor->pos().y();
       plaineditor->curX = tableView->xPos -x ;
       plaineditor->curY = tableView->yPos - y;
    //    editor->setFixedWidth(rect.width()+ editor->contentsMargins().left() +
    //                          editor->contentsMargins().right());
    }
    

    Code for editor

    void AlignTextEdit::moveCursorToPosition()
    {
        QFont f = font();
        QFontMetrics m (f) ;
        int RowHeight = m.lineSpacing() ;
        int times = curY/RowHeight;
        int width = m.averageCharWidth();
        int lineChars = this->width()/width;
        int totalChars = lineChars*times + curX/width;
        QTextCursor tmpCursor = textCursor();
        tmpCursor.movePosition(QTextCursor::NextCharacter, QTextCursor::MoveAnchor, totalChars);
        setTextCursor(tmpCursor);
    }
    

    I've worked on the problems for nearly one month, but I cannot get workable solutions.
    Thanks a lot.

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

      Hi
      If you been hacking at it for a month you most likely already tried this but my first thought was its your
      AlignTextEdit that gives you scrollbars as the normal cell does not get a scrollbar.

      So give it Qt::ScrollBarAlwaysOff in CreateEditor
      like
      textEditor ->setHorizontalScrollBarPolicy( Qt::ScrollBarAlwaysOff );
      textEditor ->setVerticalScrollBarPolicy( Qt::ScrollBarAlwaysOff );

      P 1 Reply Last reply
      0
      • mrjjM mrjj

        Hi
        If you been hacking at it for a month you most likely already tried this but my first thought was its your
        AlignTextEdit that gives you scrollbars as the normal cell does not get a scrollbar.

        So give it Qt::ScrollBarAlwaysOff in CreateEditor
        like
        textEditor ->setHorizontalScrollBarPolicy( Qt::ScrollBarAlwaysOff );
        textEditor ->setVerticalScrollBarPolicy( Qt::ScrollBarAlwaysOff );

        P Offline
        P Offline
        PaulHuang
        wrote on last edited by
        #3

        @mrjj said in How to avoid scrollbar and move cursur to the click position when editing the text in tableview?:

        AlignTextEdit

        I've tried this option.
        However, when I clicked the cell, the position of the editor was slightly different from the one displayed in the tableview. So, for some long text, sometimes some characters cannot be displayed on the texteditor.
        How can I place the texteditor at the exact position of the cell in the tableview? I‘ve used the QTableView::resizeRowsToContents () to control the cell height.
        Thanks a lot.

        P 1 Reply Last reply
        0
        • P PaulHuang

          @mrjj said in How to avoid scrollbar and move cursur to the click position when editing the text in tableview?:

          AlignTextEdit

          I've tried this option.
          However, when I clicked the cell, the position of the editor was slightly different from the one displayed in the tableview. So, for some long text, sometimes some characters cannot be displayed on the texteditor.
          How can I place the texteditor at the exact position of the cell in the tableview? I‘ve used the QTableView::resizeRowsToContents () to control the cell height.
          Thanks a lot.

          P Offline
          P Offline
          PaulHuang
          wrote on last edited by
          #4

          @PaulHuang said in How to avoid scrollbar and move cursur to the click position when editing the text in tableview?:

          H
          Like the picture below:
          ec80fb1b-36c9-4dc1-ae11-ddf5530c2dbb-image.png

          mrjjM 1 Reply Last reply
          0
          • P PaulHuang

            @PaulHuang said in How to avoid scrollbar and move cursur to the click position when editing the text in tableview?:

            H
            Like the picture below:
            ec80fb1b-36c9-4dc1-ae11-ddf5530c2dbb-image.png

            mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @PaulHuang
            Well, the QTextEdit also has a little offset to where it draw its text so to have draw text in same place
            as when editing then you should maybe use the AlignTextEdit to draw the text in cell also.
            Via its render function.

            1 Reply Last reply
            0

            • Login

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