#include "widget.h"
#include "ui_widget.h"
#include <QDebug>
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this)
}
Widget::~Widget()
{
delete ui
}
void Widget::on_pushButton_clicked()//+
{
int i = ui->lineEdit->cursorPosition()
qDebug() <<i
ui->lineEdit->setCursorPosition(++i)
ui->pushButton->setFocusPolicy(Qt::NoFocus)
ui->pushButton_2->setFocusPolicy(Qt::NoFocus)
ui->lineEdit->setFocus()
}
void Widget::on_pushButton_2_clicked()//-
{
int i = ui->lineEdit->cursorPosition()
qDebug() <<i
ui->lineEdit->setCursorPosition(--i)
ui->pushButton->setFocusPolicy(Qt::NoFocus)
ui->pushButton_2->setFocusPolicy(Qt::NoFocus)
ui->lineEdit->setFocus()
}
刚给你试了下可以的,还要就是注意焦点,让QLineEdit一直获得焦点,代码都给你了,分多点吧
QLINEEDIT 类里的方法int cursorPosition() const
void setSelection(int, int)
QString selectedText() const
找到想取出的字符串,设置选中,再用selectedText取出,
或者可以研究一下QTextCursor
QTextCursor cursor(ui->textEdit->textCursor())//
可以获取光标位置,移动光标等等
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)