PDA

View Full Version : treating each line of a qtextedit seperatly?



kja
25th November 2010, 00:00
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



QString textEntered = this->textedit->toPlainText();
QStringList lines = textEntered.split( "\n", QString::SkipEmptyParts );
foreach( QString line, lines ) {
line = line.trimmed();
Filepath = qstrudup(line.toLatin1() );
myPlot->plotFile(Filename)
}
}

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!

franco.amato
25th November 2010, 00:10
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



QString textEntered = this->textedit->toPlainText();
QStringList lines = textEntered.split( "\n", QString::SkipEmptyParts );
foreach( QString line, lines ) {
line = line.trimmed();
Filepath = qstrudup(line.toLatin1() );
myPlot->plotFile(Filename)
}
}

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

wysota
25th November 2010, 10:18
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



QString textEntered = this->textedit->toPlainText();
QStringList lines = textEntered.split( "\n", QString::SkipEmptyParts );
foreach( QString line, lines ) {
line = line.trimmed();
Filepath = qstrudup(line.toLatin1() );
myPlot->plotFile(Filename)
}
}

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.

srazi
25th November 2010, 17:57
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



QString textEntered = this->textedit->toPlainText();
QStringList lines = textEntered.split( "\n", QString::SkipEmptyParts );
foreach( QString line, lines ) {
line = line.trimmed();
Filepath = qstrudup(line.toLatin1() );
myPlot->plotFile(Filename)
}
}

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,


//table initialization
QTableWidget *table = new QTableWidget(this);
table->setColumnCount(1);
table->setRowCount( Number of lines );
QHeaderView *horHeader = table->horizontalHeader();
//horHeader->hide();
horHeader->setStretchLastSection(true);
connect(table, SIGNAL(itemEntered( QTableWidgetItem * item )), this, mouseOverLine( QTableWidgetItem * item ));
////////////////////////////////////////
void MainWindow::mouseOverLine( QTableWidgetItem * item )
{
QString Filename= item->text();
myPlot->plotFile(Filename)
}