Hi all
I'm working on my first app using pyqt and i need some help
I want to be able to click on a line of text in a QtextEdit box, and be able to load that line into a string.
How would i do this...?
Thanks![]()
Hi all
I'm working on my first app using pyqt and i need some help
I want to be able to click on a line of text in a QtextEdit box, and be able to load that line into a string.
How would i do this...?
Thanks![]()
Hi,
if you click on a line in a text edit, then also the cursor jumps to that line. Get that cursor via QTextEdit::textCursor() and use QTextCursor::movePosition() with QTextCursor::StartOfBlock and QTextCursor::EndOfBlock to select the line. Then grab the text via QTextCursor::selectedText().
That's all!
Thanks for the reply
maybe you could help out with a demo code.
I've had a play, but no luck. I had thought that to move to a function after a click would be the first step, I was thinking something like this but it didn't work.
what am i doing wrong?Qt Code:
To copy to clipboard, switch view to plain text mode
Last edited by benlyboy; 25th April 2010 at 19:15.
Hi, I am very beginner myself but I tested out what Lykurg said and it works.
I created QTextEdit and QLabel with QTCreator UI editor (based on QDialog).
Added this to dialog class in header
Qt Code:
private slots: void on_textEdit_clicked();To copy to clipboard, switch view to plain text mode
Then I added this into constructor in dialog.cpp.
Qt Code:
connect(ui->textEdit, SIGNAL(cursorPositionChanged()), this, SLOT(on_textEdit_clicked()));To copy to clipboard, switch view to plain text mode
and into dialog.cpp:
Qt Code:
void Dialog::on_textEdit_clicked() { QTextCursor cursor; QString text; cursor = ui->textEdit->textCursor(); text = cursor.selectedText(); ui->label->setText(text); }To copy to clipboard, switch view to plain text mode
Sorry, I dont use python. So you've got to translate it yourself.
Works great. I however expected the line to turn blue to indicate selection but this did not happen.
yes thanks that works great
thanks all for the help
Of course, because I said it
That is because textCursor() gives a copy of the original (="displayed") text cursor. All things you do with it don't show up in the editor. If you wish so, you have to set it back via setTextCursor(). But be aware that this will in your case end up in a never ending loop because of your connect statement.I however expected the line to turn blue to indicate selection but this did not happen.
In the example, I wouldn't use the clicked() signal. Only use cursorPositionChanged() and check if the line was changed (with a local member variable). Only if it changed, do all the cursor stuff.
Bookmarks