Results 1 to 4 of 4

Thread: treating each line of a qtextedit seperatly?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Oct 2010
    Posts
    58
    Thanks
    26
    Qt products
    Qt4
    Platforms
    Windows

    Default treating each line of a qtextedit seperatly?

    Hi,

    Is it possible to treat each line of a qtextedit individually and attach them to a slot? I know I can get the text from each line and treat it separately but what I want to do is be able to push a shortcut key (like control+r) when the curser is on a line and have that call a function.

    Specifically what I want is this: have a qtextedit with a list of file names that I plot. When the cursor is on one of the lines and i press control+t I want that file to be plotted on the right Y-axis.

    Right now I am getting the text and plotting it like this

    Qt Code:
    1. QString textEntered = this->textedit->toPlainText();
    2. QStringList lines = textEntered.split( "\n", QString::SkipEmptyParts );
    3. foreach( QString line, lines ) {
    4. line = line.trimmed();
    5. Filepath = qstrudup(line.toLatin1() );
    6. myPlot->plotFile(Filename)
    7. }
    8. }
    To copy to clipboard, switch view to plain text mode 

    I'm not sure if I'd use this same kind of thing or not.

    And I know, I know, it would be much easier to use individual qlineedits, but my boss wants the textedit.

    Thanks!

  2. #2
    Join Date
    Nov 2007
    Location
    Italy
    Posts
    694
    Thanks
    59
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: treating each line of a qtextedit seperatly?

    Quote Originally Posted by kja View Post
    Hi,

    Is it possible to treat each line of a qtextedit individually and attach them to a slot? I know I can get the text from each line and treat it separately but what I want to do is be able to push a shortcut key (like control+r) when the curser is on a line and have that call a function.

    Specifically what I want is this: have a qtextedit with a list of file names that I plot. When the cursor is on one of the lines and i press control+t I want that file to be plotted on the right Y-axis.

    Right now I am getting the text and plotting it like this

    Qt Code:
    1. QString textEntered = this->textedit->toPlainText();
    2. QStringList lines = textEntered.split( "\n", QString::SkipEmptyParts );
    3. foreach( QString line, lines ) {
    4. line = line.trimmed();
    5. Filepath = qstrudup(line.toLatin1() );
    6. myPlot->plotFile(Filename)
    7. }
    8. }
    To copy to clipboard, switch view to plain text mode 

    I'm not sure if I'd use this same kind of thing or not.

    And I know, I know, it would be much easier to use individual qlineedits, but my boss wants the textedit.

    Thanks!
    Hi what's about implementing the mouse move event and doing your check inside it?
    Regards
    Franco Amato

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: treating each line of a qtextedit seperatly?

    Quote Originally Posted by kja View Post
    Hi,

    Is it possible to treat each line of a qtextedit individually and attach them to a slot? I know I can get the text from each line and treat it separately but what I want to do is be able to push a shortcut key (like control+r) when the curser is on a line and have that call a function.

    Specifically what I want is this: have a qtextedit with a list of file names that I plot. When the cursor is on one of the lines and i press control+t I want that file to be plotted on the right Y-axis.

    Right now I am getting the text and plotting it like this

    Qt Code:
    1. QString textEntered = this->textedit->toPlainText();
    2. QStringList lines = textEntered.split( "\n", QString::SkipEmptyParts );
    3. foreach( QString line, lines ) {
    4. line = line.trimmed();
    5. Filepath = qstrudup(line.toLatin1() );
    6. myPlot->plotFile(Filename)
    7. }
    8. }
    To copy to clipboard, switch view to plain text mode 

    I'm not sure if I'd use this same kind of thing or not.
    There are two separate problems here. One is to trigger the shortcut - this should be relatively easy if you know how to use QAction and QShortcut so I won't go into details here. The other problem is to get the right line of text. Instead of reading the text and cutting it to pieces, you can do that directly within the document being edited - use QTextEdit::textCursor() to get the cursor then ask the cursor for the block (line) it is in right now - QTextCursor::block() and then fetch the text from the block.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  4. #4
    Join Date
    Jul 2010
    Posts
    41
    Thanks
    6
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: treating each line of a qtextedit seperatly?

    Quote Originally Posted by kja View Post
    Hi,

    Is it possible to treat each line of a qtextedit individually and attach them to a slot? I know I can get the text from each line and treat it separately but what I want to do is be able to push a shortcut key (like control+r) when the curser is on a line and have that call a function.

    Specifically what I want is this: have a qtextedit with a list of file names that I plot. When the cursor is on one of the lines and i press control+t I want that file to be plotted on the right Y-axis.

    Right now I am getting the text and plotting it like this

    Qt Code:
    1. QString textEntered = this->textedit->toPlainText();
    2. QStringList lines = textEntered.split( "\n", QString::SkipEmptyParts );
    3. foreach( QString line, lines ) {
    4. line = line.trimmed();
    5. Filepath = qstrudup(line.toLatin1() );
    6. myPlot->plotFile(Filename)
    7. }
    8. }
    To copy to clipboard, switch view to plain text mode 

    I'm not sure if I'd use this same kind of thing or not.

    And I know, I know, it would be much easier to use individual qlineedits, but my boss wants the textedit.

    Thanks!
    Hi,
    I don't answer your question but I give you another idea that is very easier to implementing,
    Use a QTableWidget with one column and enable stretch last section for horizontal header, every row of table contains one of your document block/line ( file name ).
    Now, you're ready to go, just use "itemEntered( QTableWidgetItem * item )" signal, like this,
    Qt Code:
    1. //table initialization
    2. QTableWidget *table = new QTableWidget(this);
    3. table->setColumnCount(1);
    4. table->setRowCount( Number of lines );
    5. QHeaderView *horHeader = table->horizontalHeader();
    6. //horHeader->hide();
    7. horHeader->setStretchLastSection(true);
    8. connect(table, SIGNAL(itemEntered( QTableWidgetItem * item )), this, mouseOverLine( QTableWidgetItem * item ));
    9. ////////////////////////////////////////
    10. void MainWindow::mouseOverLine( QTableWidgetItem * item )
    11. {
    12. QString Filename= item->text();
    13. myPlot->plotFile(Filename)
    14. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. QTextEdit End-of-Line character
    By smhall316 in forum Newbie
    Replies: 1
    Last Post: 29th September 2010, 18:28
  2. Single line QTextEdit
    By Arthur in forum Qt Programming
    Replies: 1
    Last Post: 30th August 2010, 21:04
  3. Modifying a line in a QTextEdit
    By elcuco in forum Qt Programming
    Replies: 1
    Last Post: 15th March 2008, 16:50
  4. Line Number - QTextEdit...???
    By deepusrp in forum Qt Programming
    Replies: 2
    Last Post: 15th May 2007, 16:34
  5. How to get text of last line only in QTextEdit?
    By rajesh in forum Qt Programming
    Replies: 2
    Last Post: 12th June 2006, 13:37

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.