Unsolved How to avoid scrollbar and move cursur to the click position when editing the text in tableview?
-
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. -
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 ); -
@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. -
@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:
-
@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.